Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Collections.Generic;
11 : using System.Linq;
12 : using System.Reflection;
13 : using System.Runtime.Serialization;
14 : using System.Web;
15 : using System.Xml;
16 : using Cqrs.Authentication;
17 : using Cqrs.Configuration;
18 : using Cqrs.Domain;
19 : using Cqrs.Events;
20 : using Cqrs.Messages;
21 : using Cqrs.Services;
22 :
23 : namespace Cqrs.WebApi
24 : {
25 : /// <summary>
26 : /// A start-up configuration class for the auto documenting features.
27 : /// </summary>
28 : /// <typeparam name="TAuthenticationToken">The <see cref="Type"/> of the authentication token.</typeparam>
29 : public static class HelpPageConfig<TAuthenticationToken>
30 1 : {
31 : /// <summary>
32 : /// The list of relevant XML files to use with the auto documenting feature.
33 : /// </summary>
34 : public static IList<string> AssemblyXmlFileNames { get; set; }
35 :
36 : static HelpPageConfig()
37 : {
38 : AssemblyXmlFileNames = new List<string>();
39 : }
40 :
41 : /// <summary>
42 : /// A sample <see cref="IEvent{TAuthenticationToken}"/> used for API documentation generation.
43 : /// </summary>
44 : [Serializable]
45 : [DataContract]
46 : public class UserCreatedEvent : IEvent<TAuthenticationToken>
47 1 : {
48 : #region Implementation of IMessageWithAuthenticationToken<TAuthenticationToken>
49 :
50 : /// <summary>
51 : /// The authentication token of the entity that triggered the event to be raised.
52 : /// </summary>
53 : [DataMember]
54 : public TAuthenticationToken AuthenticationToken { get; set; }
55 :
56 : #endregion
57 :
58 : #region Implementation of IMessage
59 :
60 : /// <summary>
61 : /// An identifier used to group together several <see cref="IMessage"/>. Any <see cref="IMessage"/> with the same <see cref="CorrelationId"/> were triggered by the same initiating request.
62 : /// </summary>
63 : [DataMember]
64 : public Guid CorrelationId { get; set; }
65 :
66 : /// <summary>
67 : /// The originating framework this message was sent from.
68 : /// </summary>
69 : [DataMember]
70 : public string OriginatingFramework { get; set; }
71 :
72 : /// <summary>
73 : /// The frameworks this <see cref="IMessage"/> has been delivered to/sent via already.
74 : /// </summary>
75 : [DataMember]
76 : public IEnumerable<string> Frameworks { get; set; }
77 :
78 : #endregion
79 :
80 : #region Implementation of IMessage
81 :
82 : /// <summary>
83 : /// The identifier of the command itself.
84 : /// In some cases this may be the <see cref="IAggregateRoot{TAuthenticationToken}"/> or <see cref="ISaga{TAuthenticationToken}"/> this command targets.
85 : /// </summary>
86 : [DataMember]
87 : public Guid Id { get; set; }
88 :
89 : /// <summary>
90 : /// The new version number the targeted <see cref="IAggregateRoot{TAuthenticationToken}"/> or <see cref="ISaga{TAuthenticationToken}"/> that raised this.
91 : /// </summary>
92 : [DataMember]
93 : public int Version { get; set; }
94 :
95 : /// <summary>
96 : /// The date and time the event was raised or published.
97 : /// </summary>
98 : [DataMember]
99 : public DateTimeOffset TimeStamp { get; set; }
100 :
101 : #endregion
102 :
103 : /// <summary>
104 : /// The identifier of the User to create.
105 : /// </summary>
106 : public Guid Rsn { get; set; }
107 :
108 : /// <summary>
109 : /// The name os the USer to create.
110 : /// </summary>
111 : public string Name { get; set; }
112 :
113 : /// <summary>
114 : /// The email address of the User to create.
115 : /// </summary>
116 : public string EmailAddress { get; set; }
117 : }
118 :
119 : static TAuthenticationToken GenerateAuthenticationToken()
120 : {
121 : string authenticationType;
122 : if (!DependencyResolver.Current.Resolve<IConfigurationManager>().TryGetSetting("Cqrs.AuthenticationTokenType", out authenticationType))
123 : authenticationType = "Guid";
124 :
125 : TAuthenticationToken token = default(TAuthenticationToken);
126 :
127 : if (authenticationType.ToLowerInvariant() == "int" || authenticationType.ToLowerInvariant() == "integer")
128 : token = (TAuthenticationToken)(object)123;
129 : else if (authenticationType.ToLowerInvariant() == "guid")
130 : token = (TAuthenticationToken)(object)Guid.NewGuid();
131 : else if (authenticationType.ToLowerInvariant() == "string" || authenticationType.ToLowerInvariant() == "text")
132 : token = (TAuthenticationToken)(object)"UserToken123";
133 : else if (authenticationType == "SingleSignOnToken" || authenticationType == "ISingleSignOnToken")
134 : token = (TAuthenticationToken)(object)new SingleSignOnToken
135 : {
136 : Token = Guid.NewGuid().ToString("N"),
137 : DateIssued = DateTime.Now,
138 : TimeOfExpiry = DateTime.Now.AddMinutes(20)
139 : };
140 : else if (authenticationType == "SingleSignOnTokenWithUserRsn" || authenticationType == "ISingleSignOnTokenWithUserRsn")
141 : token = (TAuthenticationToken)(object)new SingleSignOnTokenWithUserRsn
142 : {
143 : Token = Guid.NewGuid().ToString("N"),
144 : DateIssued = DateTime.Now,
145 : TimeOfExpiry = DateTime.Now.AddMinutes(20),
146 : UserRsn = Guid.NewGuid()
147 : };
148 : else if (authenticationType == "SingleSignOnTokenWithCompanyRsn" || authenticationType == "ISingleSignOnTokenWithCompanyRsn")
149 : token = (TAuthenticationToken)(object)new SingleSignOnTokenWithCompanyRsn
150 : {
151 : Token = Guid.NewGuid().ToString("N"),
152 : DateIssued = DateTime.Now,
153 : TimeOfExpiry = DateTime.Now.AddMinutes(20),
154 : CompanyRsn = Guid.NewGuid()
155 : };
156 : else if (authenticationType == "SingleSignOnTokenWithUserRsnAndCompanyRsn" || authenticationType == "ISingleSignOnTokenWithUserRsnAndCompanyRsn")
157 : token = (TAuthenticationToken)(object)new SingleSignOnTokenWithUserRsnAndCompanyRsn
158 : {
159 : Token = Guid.NewGuid().ToString("N"),
160 : DateIssued = DateTime.Now,
161 : TimeOfExpiry = DateTime.Now.AddMinutes(20),
162 : UserRsn = Guid.NewGuid(),
163 : CompanyRsn = Guid.NewGuid()
164 : };
165 :
166 : return token;
167 : }
168 :
169 : /// <summary>
170 : /// Get a collection of sample objects for the auto documenting features to use.
171 : /// </summary>
172 1 : public static IDictionary<Type, object> GetBasicSampleObjects()
173 : {
174 : var eventCorrelationId = Guid.NewGuid();
175 : var correlationId = Guid.NewGuid();
176 :
177 : var sameplEvent = new UserCreatedEvent
178 : {
179 : CorrelationId = correlationId,
180 : AuthenticationToken = GenerateAuthenticationToken(),
181 : EmailAddress = "john@smith.com",
182 : Frameworks = new List<string> { "Azure", "Amazon EC2" },
183 : Id = Guid.NewGuid(),
184 : Name = "John Smith",
185 : OriginatingFramework = "Azure",
186 : TimeStamp = DateTime.Now.AddMinutes(-3),
187 : Version = 1
188 : };
189 :
190 : return new Dictionary<Type, object>
191 : {
192 : {
193 : typeof(string),
194 : "sample string"
195 : },
196 : {
197 : typeof(IServiceResponse),
198 : new ServiceResponse
199 : {
200 : CorrelationId = correlationId,
201 : State = ServiceResponseStateType.Succeeded
202 : }
203 : },
204 : {
205 : typeof(IServiceRequestWithData<TAuthenticationToken, Guid>),
206 : new ServiceRequestWithData<TAuthenticationToken, Guid>
207 : {
208 : AuthenticationToken = GenerateAuthenticationToken(),
209 : CorrelationId = correlationId,
210 : Data = eventCorrelationId
211 : }
212 : },
213 : {
214 : typeof(IServiceResponseWithResultData<IEnumerable<EventData>>),
215 : new ServiceResponseWithResultData<IEnumerable<EventData>>
216 : {
217 : CorrelationId = correlationId,
218 : ResultData = new List<EventData>
219 : {
220 : new EventData
221 : {
222 : AggregateId = string.Format("{0}//{1}", typeof(UserCreatedEvent).FullName, sameplEvent.Rsn),
223 : CorrelationId = eventCorrelationId,
224 : Data = sameplEvent,
225 : AggregateRsn = sameplEvent.Rsn,
226 : EventId = Guid.NewGuid(),
227 : EventType = typeof(UserCreatedEvent).FullName,
228 : Timestamp = sameplEvent.TimeStamp.DateTime,
229 : Version = sameplEvent.Version
230 : }
231 : },
232 : Version = 0,
233 : State = ServiceResponseStateType.Succeeded
234 : }
235 : }
236 : };
237 : }
238 :
239 : /// <summary>
240 : /// Generate a list of relevant XML files to use with the auto documenting feature.
241 : /// </summary>
242 1 : public static void GenerateAssemblyXmlFileNames()
243 : {
244 : if (AssemblyXmlFileNames.Any())
245 : return;
246 :
247 : string webAssemblyName = Assembly.GetCallingAssembly().FullName;
248 : webAssemblyName = webAssemblyName.Substring(0, webAssemblyName.IndexOf(","));
249 : AssemblyXmlFileNames = new List<string> { webAssemblyName };
250 : try
251 : {
252 : string publicAssemblyName = webAssemblyName.Substring(0, webAssemblyName.Length - ".Domain.Host.Web".Length);
253 : AssemblyXmlFileNames.Add(publicAssemblyName);
254 : }
255 : catch (ArgumentOutOfRangeException) { }
256 : try
257 : {
258 : string domainAssemblyName = webAssemblyName.Substring(0, webAssemblyName.Length - ".Host.Web".Length);
259 : AssemblyXmlFileNames.Add(domainAssemblyName);
260 : }
261 : catch (ArgumentOutOfRangeException) { }
262 : }
263 :
264 : /// <summary>
265 : /// Generate the relevant XML file used by the auto documenting feature.
266 : /// </summary>
267 1 : public static void CreateXmlDocumentation()
268 : {
269 : GenerateAssemblyXmlFileNames();
270 : var assemblyXmlFileNames = new List<string>(AssemblyXmlFileNames) {"Cqrs", "Cqrs.WebApi"};
271 : var finalDocumentation = new XmlDocument();
272 : for (int i = 0; i < assemblyXmlFileNames.Count; i++)
273 : {
274 : string assemblyXmlFileName = assemblyXmlFileNames[i];
275 :
276 : XmlDocument documentation = new XmlDocument();
277 : if (i == 0)
278 : {
279 : finalDocumentation.Load(HttpContext.Current.Server.MapPath(string.Format("~/bin/{0}.XML", assemblyXmlFileName)));
280 : continue;
281 : }
282 : documentation.Load(HttpContext.Current.Server.MapPath(string.Format("~/bin/{0}.XML", assemblyXmlFileName)));
283 :
284 : foreach (XmlNode childNode in documentation.DocumentElement.ChildNodes)
285 : finalDocumentation.DocumentElement.AppendChild(finalDocumentation.ImportNode(childNode, true));
286 : }
287 :
288 : finalDocumentation.Save(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml"));
289 : }
290 : }
291 : }
|