Merge pull request #487 from mayerwin/patch-1
[mono.git] / mcs / class / System.ServiceModel.Web / System.ServiceModel.Web / WebOperationContext.cs
1 //
2 // WebOperationContext.cs
3 //
4 // Author:
5 //      Atsushi Enomoto  <atsushi@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.IO;
30 using System.ServiceModel;
31 using System.ServiceModel.Channels;
32 using System.ServiceModel.Description;
33
34 #if NET_2_1 // Note that moonlight System.ServiceModel.Web.dll does not contain this class.
35 using IncomingWebRequestContext = System.Object;
36 using OutgoingWebResponseContext = System.Object;
37 #else
38 using System.Runtime.Serialization.Json;
39 using System.ServiceModel.Syndication;
40 using System.Xml;
41 using System.Xml.Serialization;
42 #endif
43
44 namespace System.ServiceModel.Web
45 {
46         public class WebOperationContext
47 #if !NET_2_1
48          : IExtension<OperationContext>
49 #endif
50         {
51 #if !NET_2_1
52                 public static WebOperationContext Current {
53                         get {
54                                 if (OperationContext.Current == null)
55                                         return null;
56                                 var ret = OperationContext.Current.Extensions.Find<WebOperationContext> ();
57                                 if (ret == null) {
58                                         ret = new WebOperationContext (OperationContext.Current);
59                                         OperationContext.Current.Extensions.Add (ret);
60                                 }
61                                 return ret;
62                         }
63                 }
64 #endif
65
66                 IncomingWebRequestContext incoming_request;
67                 IncomingWebResponseContext incoming_response;
68                 OutgoingWebRequestContext outgoing_request;
69                 OutgoingWebResponseContext outgoing_response;
70
71                 public WebOperationContext (OperationContext operation)
72                 {
73                         if (operation == null)
74                                 throw new ArgumentNullException ("operation");
75
76                         outgoing_request = new OutgoingWebRequestContext ();
77                         incoming_response = new IncomingWebResponseContext (operation);
78 #if !NET_2_1
79                         incoming_request = new IncomingWebRequestContext (operation);
80                         outgoing_response = new OutgoingWebResponseContext ();
81 #endif
82                 }
83
84 #if !NET_2_1
85                 public IncomingWebRequestContext IncomingRequest {
86                         get { return incoming_request; }
87                 }
88 #endif
89
90                 public IncomingWebResponseContext IncomingResponse {
91                         get { return incoming_response; }
92                 }
93
94                 public OutgoingWebRequestContext OutgoingRequest {
95                         get { return outgoing_request; }
96                 }
97
98 #if !NET_2_1
99                 public OutgoingWebResponseContext OutgoingResponse {
100                         get { return outgoing_response; }
101                 }
102 #endif
103
104                 public void Attach (OperationContext context)
105                 {
106                         // do nothing
107                 }
108
109                 public void Detach (OperationContext context)
110                 {
111                         // do nothing
112                 }
113
114 #if NET_4_0 && !MOBILE
115                 static readonly XmlWriterSettings settings = new XmlWriterSettings () { OmitXmlDeclaration = true, Indent = false };
116                 XmlSerializer document_serializer, feed_serializer, item_serializer;
117
118                 Message CreateAtom10Response<T> (T obj, ref XmlSerializer serializer)
119                 {
120                         if (serializer == null)
121                                 serializer = new XmlSerializer (typeof (T));
122                         var ms = new MemoryStream ();
123                         using (var xw = XmlWriter.Create (ms, settings))
124                                 serializer.Serialize (xw, obj);
125                         ms.Position = 0;
126                         return Message.CreateMessage (MessageVersion.None, null, XmlReader.Create (ms));
127                 }
128
129                 public Message CreateAtom10Response (ServiceDocument document)
130                 {
131                         return CreateAtom10Response<AtomPub10ServiceDocumentFormatter> (new AtomPub10ServiceDocumentFormatter (document), ref document_serializer);
132                 }
133
134                 public Message CreateAtom10Response (SyndicationFeed feed)
135                 {
136                         return CreateAtom10Response<Atom10FeedFormatter> (new Atom10FeedFormatter (feed), ref feed_serializer);
137                 }
138
139                 public Message CreateAtom10Response (SyndicationItem item)
140                 {
141                         return CreateAtom10Response<Atom10ItemFormatter> (new Atom10ItemFormatter (item), ref item_serializer);
142                 }
143
144                 public Message CreateJsonResponse<T> (T instance)
145                 {
146                         return CreateJsonResponse<T> (instance, new DataContractJsonSerializer (typeof (T)));
147                 }
148
149                 public Message CreateJsonResponse<T> (T instance, DataContractJsonSerializer serializer)
150                 {
151                         return Message.CreateMessage (MessageVersion.None, null, instance, serializer);
152                 }
153 #endif
154         }
155 }