remove executable bit from changelogs
[mono.git] / mcs / class / Microsoft.Web.Services / Microsoft.Web.Services.Timestamp / Timestamp.cs
1 //\r
2 // Timestamp.cs: Handles WS-Security "Utility" Timestamp\r
3 //\r
4 // Author:\r
5 //      Sebastien Pouliot (spouliot@motus.com)\r
6 //\r
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)\r
8 //\r
9 // Licensed under MIT X11 (see LICENSE) with this specific addition:\r
10 //\r
11 // \93This source code may incorporate intellectual property owned by Microsoft \r
12 // Corporation. Our provision of this source code does not include any licenses\r
13 // or any other rights to you under any Microsoft intellectual property. If you\r
14 // would like a license from Microsoft (e.g. rebrand, redistribute), you need \r
15 // to contact Microsoft directly.\94 \r
16 //\r
17 \r
18 using System;\r
19 using System.Web.Services.Protocols;\r
20 using System.Xml;\r
21 using Microsoft.Web.Services;\r
22 #if !WSE1\r
23 using Microsoft.Web.Services.Xml;\r
24 #endif\r
25 \r
26 namespace Microsoft.Web.Services.Timestamp {\r
27 \r
28         // References\r
29         // a.   Web Services Security Addendum, Version 1.0, August 18, 2002\r
30         //      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnglobspec/html/ws-security.asp\r
31 \r
32         public class Timestamp : SoapHeader, IXmlElement {\r
33 \r
34                 private string id;\r
35                 private string idNS;\r
36                 private DateTime created;\r
37                 private string createdId;\r
38                 private string createdIdNS;\r
39                 private DateTime expired;\r
40                 private string expiredId;\r
41                 private string expiredIdNS;\r
42                 private ReceivedCollection rcoll;\r
43                 private long timeToLive;\r
44 \r
45                 public Timestamp () \r
46                 {\r
47                         created = DateTime.MinValue;\r
48                         expired = DateTime.MaxValue;\r
49                         timeToLive = 300000; // 5 minutes\r
50                         rcoll = new ReceivedCollection ();\r
51                 }\r
52 \r
53                 // we must be able to fix both creation and expiration\r
54                 internal void SetTimestamp (DateTime c) \r
55                 {\r
56                         created = c;\r
57                         expired = created.AddMilliseconds (timeToLive);\r
58                 }\r
59 \r
60                 public new string Actor {\r
61                         get { return base.Actor; }\r
62                 }\r
63 \r
64                 public DateTime Created {\r
65                         get { return created; }\r
66                 }\r
67 \r
68                 public DateTime Expires {\r
69                         get { return expired; }\r
70                 }\r
71 \r
72                 public ReceivedCollection Receivers {\r
73                         get { return rcoll; }\r
74                 }\r
75 \r
76                 public long Ttl {\r
77                         get { return timeToLive; }\r
78                         set {\r
79                                 if (value < 0)\r
80                                         throw new System.ArgumentException ("value");\r
81                                 if (value == 0)\r
82                                         expired = DateTime.MaxValue;\r
83                                 timeToLive = value; \r
84                         }\r
85                 }\r
86 \r
87                 // syntactically correct\r
88                 // no checks are done on the dates themselves !\r
89                 public void CheckValid () \r
90                 {\r
91                         if (created == DateTime.MinValue)\r
92                                 throw new TimestampFormatException (TimestampFormatException.MissingCreatedElement);\r
93                 }\r
94 \r
95                 public XmlElement GetXml (XmlDocument document) \r
96                 {\r
97                         if (document == null)\r
98                                 throw new System.ArgumentNullException ("document");\r
99 \r
100                         // much cleaner than using StringBuilder!\r
101                         XmlElement xel = document.CreateElement (WSTimestamp.Prefix, WSTimestamp.ElementNames.Timestamp, WSTimestamp.NamespaceURI);\r
102                         // FIXME: commented for WSE preview compatibility\r
103                         // if (id != null)\r
104                         //      xel.SetAttribute (WSTimestamp.AttributeNames.Id, idNS, id);\r
105 \r
106                         if (created != DateTime.MinValue) {\r
107                                 XmlElement xelCreated = document.CreateElement (WSTimestamp.Prefix, WSTimestamp.ElementNames.Created, WSTimestamp.NamespaceURI);\r
108                                 xelCreated.InnerText = created.ToString (WSTimestamp.TimeFormat);\r
109                                 if (createdId != null)\r
110                                         xelCreated.SetAttribute (WSTimestamp.AttributeNames.Id, createdIdNS, createdId);\r
111                                 xel.AppendChild (xelCreated);\r
112                         }\r
113                         if (expired != DateTime.MaxValue) {\r
114                                 XmlElement xelExpires = document.CreateElement (WSTimestamp.Prefix, WSTimestamp.ElementNames.Expires, WSTimestamp.NamespaceURI);\r
115                                 xelExpires.InnerText = expired.ToString (WSTimestamp.TimeFormat);\r
116                                 if (expiredId != null)\r
117                                         xelExpires.SetAttribute (WSTimestamp.AttributeNames.Id, expiredIdNS, expiredId);\r
118                                 xel.AppendChild (xelExpires);\r
119                         }\r
120                         for (int i=0; i < rcoll.Count ; i++) \r
121                         {\r
122                                 XmlElement received = rcoll[i].GetXml (document);\r
123                                 xel.AppendChild (received);\r
124                         }\r
125                         return xel;\r
126                 }\r
127 \r
128                 public void LoadXml (XmlElement element) \r
129                 {\r
130                         if (element == null)\r
131                                 throw new System.ArgumentNullException ("element");\r
132 \r
133                         if ((element.LocalName != WSTimestamp.ElementNames.Timestamp) || (element.NamespaceURI != WSTimestamp.NamespaceURI))\r
134                                 throw new System.ArgumentException ("invalid LocalName or NamespaceURI");\r
135 \r
136                         XmlAttribute xa = element.Attributes [WSTimestamp.AttributeNames.Id, WSTimestamp.NamespaceURI];\r
137                         id = ((xa == null) ? null : xa.Value);\r
138                         idNS = ((xa == null) ? null : xa.NamespaceURI);\r
139 \r
140                         XmlNodeList xnl = element.GetElementsByTagName (WSTimestamp.ElementNames.Created, WSTimestamp.NamespaceURI);\r
141                         if (xnl != null) {\r
142                                 switch (xnl.Count) {\r
143                                         case 0:\r
144 //                                              throw new TimestampFormatException (TimestampFormatException.MissingCreatedElement);\r
145                                                 break;\r
146                                         case 1:\r
147                                                 created = DateTime.ParseExact (xnl[0].InnerText, WSTimestamp.TimeFormat, null);\r
148                                                 created = created.ToUniversalTime ();\r
149                                                 xa = xnl[0].Attributes [WSTimestamp.AttributeNames.Id, WSTimestamp.NamespaceURI];\r
150                                                 createdId = ((xa == null) ? null : xa.Value);\r
151                                                 createdIdNS = ((xa == null) ? null : xa.NamespaceURI);\r
152                                                 break;\r
153                                         default:\r
154                                                 throw new TimestampFormatException (TimestampFormatException.DuplicateCreatedElement);\r
155                                 }\r
156                         }\r
157 \r
158                         xnl = element.GetElementsByTagName (WSTimestamp.ElementNames.Expires, WSTimestamp.NamespaceURI);\r
159                         if (xnl != null) {\r
160                                 switch (xnl.Count) {\r
161                                         case 0:\r
162 //                                              throw new TimestampFormatException (TimestampFormatException.MissingCreatedElement);\r
163                                                 break;\r
164                                         case 1:\r
165                                                 expired = DateTime.ParseExact (xnl[0].InnerText, WSTimestamp.TimeFormat, null);\r
166                                                 expired = expired.ToUniversalTime ();\r
167                                                 xa = xnl[0].Attributes [WSTimestamp.AttributeNames.Id, WSTimestamp.NamespaceURI];\r
168                                                 expiredId = ((xa == null) ? null : xa.Value);\r
169                                                 expiredIdNS = ((xa == null) ? null : xa.NamespaceURI);\r
170                                                 break;\r
171                                         default:\r
172                                                 throw new TimestampFormatException (TimestampFormatException.DuplicateCreatedElement);\r
173                                 }\r
174                         }\r
175 \r
176                         xnl = element.GetElementsByTagName (WSTimestamp.ElementNames.Received, WSTimestamp.NamespaceURI);\r
177                         if (xnl != null) {\r
178                                 for (int i=0; i < xnl.Count; i++) {\r
179                                         Received r = new Received ((XmlElement)xnl [i]);\r
180                                         rcoll.Add (r);\r
181                                 }\r
182                         }\r
183                 }\r
184         }\r
185 }\r