| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Runtime.Serialization.Json; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Xml; |
| | | 9 | | using System.Xml.Linq; |
| | | 10 | | using System.Xml.Serialization; |
| | | 11 | | using CoreWCF.Channels; |
| | | 12 | | using CoreWCF.Description; |
| | | 13 | | using CoreWCF.Dispatcher; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.Web |
| | | 16 | | { |
| | | 17 | | public class WebOperationContext : IExtension<OperationContext> |
| | | 18 | | { |
| | 0 | 19 | | internal static readonly string s_defaultTextMediaType = "text/plain"; |
| | 0 | 20 | | internal static readonly string s_defaultJsonMediaType = JsonGlobals.ApplicationJsonMediaType; |
| | 0 | 21 | | internal static readonly string s_defaultXmlMediaType = "application/xml"; |
| | 0 | 22 | | internal static readonly string s_defaultAtomMediaType = "application/atom+xml"; |
| | 0 | 23 | | internal static readonly string s_defaultStreamMediaType = WebHttpBehavior.s_defaultStreamContentType; |
| | | 24 | | |
| | | 25 | | private readonly OperationContext _operationContext; |
| | | 26 | | |
| | 34 | 27 | | public WebOperationContext(OperationContext operationContext) |
| | | 28 | | { |
| | 34 | 29 | | _operationContext = operationContext ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nam |
| | | 30 | | |
| | 34 | 31 | | if (operationContext.Extensions.Find<WebOperationContext>() == null) |
| | | 32 | | { |
| | 34 | 33 | | operationContext.Extensions.Add(this); |
| | | 34 | | } |
| | 34 | 35 | | } |
| | | 36 | | |
| | | 37 | | public static WebOperationContext Current |
| | | 38 | | { |
| | | 39 | | get |
| | | 40 | | { |
| | 56 | 41 | | if (OperationContext.Current == null) |
| | | 42 | | { |
| | 0 | 43 | | return null; |
| | | 44 | | } |
| | | 45 | | |
| | 56 | 46 | | WebOperationContext existing = OperationContext.Current.Extensions.Find<WebOperationContext>(); |
| | 56 | 47 | | if (existing != null) |
| | | 48 | | { |
| | 22 | 49 | | return existing; |
| | | 50 | | } |
| | | 51 | | |
| | 34 | 52 | | return new WebOperationContext(OperationContext.Current); |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | 11 | 56 | | public IncomingWebRequestContext IncomingRequest => new IncomingWebRequestContext(_operationContext); |
| | | 57 | | |
| | 41 | 58 | | public OutgoingWebResponseContext OutgoingResponse => new OutgoingWebResponseContext(_operationContext); |
| | | 59 | | |
| | | 60 | | public void Attach(OperationContext owner) |
| | | 61 | | { |
| | 34 | 62 | | } |
| | | 63 | | |
| | | 64 | | public void Detach(OperationContext owner) |
| | | 65 | | { |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | public Message CreateJsonResponse<T>(T instance) |
| | | 69 | | { |
| | 0 | 70 | | DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); |
| | | 71 | | |
| | 0 | 72 | | return CreateJsonResponse(instance, serializer); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public Message CreateJsonResponse<T>(T instance, DataContractJsonSerializer serializer) |
| | | 76 | | { |
| | 0 | 77 | | if (serializer == null) |
| | | 78 | | { |
| | 0 | 79 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer)); |
| | | 80 | | } |
| | | 81 | | |
| | 0 | 82 | | Message message = Message.CreateMessage(MessageVersion.None, null, instance, serializer); |
| | 0 | 83 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty); |
| | 0 | 84 | | AddContentType(s_defaultJsonMediaType, OutgoingResponse.BindingWriteEncoding); |
| | | 85 | | |
| | 0 | 86 | | return message; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public Message CreateXmlResponse<T>(T instance) |
| | | 90 | | { |
| | 0 | 91 | | System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContra |
| | | 92 | | |
| | 0 | 93 | | return CreateXmlResponse(instance, serializer); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public Message CreateXmlResponse<T>(T instance, System.Runtime.Serialization.XmlObjectSerializer serializer) |
| | | 97 | | { |
| | 0 | 98 | | if (serializer == null) |
| | | 99 | | { |
| | 0 | 100 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer)); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | Message message = Message.CreateMessage(MessageVersion.None, null, instance, serializer); |
| | 0 | 104 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); |
| | 0 | 105 | | AddContentType(s_defaultXmlMediaType, OutgoingResponse.BindingWriteEncoding); |
| | | 106 | | |
| | 0 | 107 | | return message; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public Message CreateXmlResponse<T>(T instance, XmlSerializer serializer) |
| | | 111 | | { |
| | 0 | 112 | | if (serializer == null) |
| | | 113 | | { |
| | 0 | 114 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(serializer)); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | Message message = Message.CreateMessage(MessageVersion.None, null, new XmlSerializerBodyWriter(instance, ser |
| | 0 | 118 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); |
| | 0 | 119 | | AddContentType(s_defaultXmlMediaType, OutgoingResponse.BindingWriteEncoding); |
| | | 120 | | |
| | 0 | 121 | | return message; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public Message CreateXmlResponse(XDocument document) |
| | | 125 | | { |
| | 0 | 126 | | if (document == null) |
| | | 127 | | { |
| | 0 | 128 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(document)); |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | Message message; |
| | 0 | 132 | | if (document.FirstNode == null) |
| | | 133 | | { |
| | 0 | 134 | | message = Message.CreateMessage(MessageVersion.None, null); |
| | | 135 | | } |
| | | 136 | | else |
| | | 137 | | { |
| | 0 | 138 | | message = Message.CreateMessage(MessageVersion.None, (string)null, document.CreateReader()); |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); |
| | 0 | 142 | | AddContentType(s_defaultXmlMediaType, OutgoingResponse.BindingWriteEncoding); |
| | | 143 | | |
| | 0 | 144 | | return message; |
| | | 145 | | } |
| | | 146 | | |
| | | 147 | | public Message CreateXmlResponse(XElement element) |
| | | 148 | | { |
| | 0 | 149 | | if (element == null) |
| | | 150 | | { |
| | 0 | 151 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element)); |
| | | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | Message message = Message.CreateMessage(MessageVersion.None, null, element.CreateReader()); |
| | 0 | 155 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); |
| | 0 | 156 | | AddContentType(s_defaultXmlMediaType, OutgoingResponse.BindingWriteEncoding); |
| | | 157 | | |
| | 0 | 158 | | return message; |
| | | 159 | | } |
| | | 160 | | |
| | 0 | 161 | | public Message CreateTextResponse(string text) => CreateTextResponse(text, s_defaultTextMediaType, Encoding.UTF8 |
| | | 162 | | |
| | 0 | 163 | | public Message CreateTextResponse(string text, string contentType) => CreateTextResponse(text, contentType, Enco |
| | | 164 | | |
| | | 165 | | public Message CreateTextResponse(string text, string contentType, Encoding encoding) |
| | | 166 | | { |
| | 0 | 167 | | if (text == null) |
| | | 168 | | { |
| | 0 | 169 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(text)); |
| | | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | if (contentType == null) |
| | | 173 | | { |
| | 0 | 174 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType)); |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | if (encoding == null) |
| | | 178 | | { |
| | 0 | 179 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encoding)); |
| | | 180 | | } |
| | | 181 | | |
| | 0 | 182 | | Message message = new HttpStreamMessage(ActionOfStreamBodyWriter.CreateStreamBodyWriter((stream) => |
| | 0 | 183 | | { |
| | 0 | 184 | | byte[] preamble = encoding.GetPreamble(); |
| | 0 | 185 | | if (preamble.Length > 0) |
| | 0 | 186 | | { |
| | 0 | 187 | | stream.Write(preamble, 0, preamble.Length); |
| | 0 | 188 | | } |
| | 0 | 189 | | byte[] bytes = encoding.GetBytes(text); |
| | 0 | 190 | | stream.Write(bytes, 0, bytes.Length); |
| | 0 | 191 | | stream.Flush(); |
| | 0 | 192 | | })); |
| | 0 | 193 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); |
| | 0 | 194 | | AddContentType(contentType, null); |
| | | 195 | | |
| | 0 | 196 | | return message; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | public Message CreateTextResponse(Action<TextWriter> textWriter, string contentType) |
| | | 200 | | { |
| | 0 | 201 | | Encoding encoding = OutgoingResponse.BindingWriteEncoding; |
| | 0 | 202 | | if (encoding == null) |
| | | 203 | | { |
| | 0 | 204 | | encoding = Encoding.UTF8; |
| | | 205 | | } |
| | | 206 | | |
| | 0 | 207 | | return CreateTextResponse(textWriter, contentType, encoding); |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | public Message CreateTextResponse(Action<TextWriter> textWriter, string contentType, Encoding encoding) |
| | | 211 | | { |
| | 0 | 212 | | if (textWriter == null) |
| | | 213 | | { |
| | 0 | 214 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(textWriter)); |
| | | 215 | | } |
| | | 216 | | |
| | 0 | 217 | | if (contentType == null) |
| | | 218 | | { |
| | 0 | 219 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType)); |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | if (encoding == null) |
| | | 223 | | { |
| | 0 | 224 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encoding)); |
| | | 225 | | } |
| | | 226 | | |
| | 0 | 227 | | Message message = new HttpStreamMessage(ActionOfStreamBodyWriter.CreateStreamBodyWriter((stream) => |
| | 0 | 228 | | { |
| | 0 | 229 | | using (TextWriter writer = new StreamWriter(stream, encoding)) |
| | 0 | 230 | | { |
| | 0 | 231 | | textWriter(writer); |
| | 0 | 232 | | } |
| | 0 | 233 | | })); |
| | 0 | 234 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); |
| | 0 | 235 | | AddContentType(contentType, null); |
| | | 236 | | |
| | 0 | 237 | | return message; |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | public Message CreateStreamResponse(Stream stream, string contentType) |
| | | 241 | | { |
| | 0 | 242 | | if (stream == null) |
| | | 243 | | { |
| | 0 | 244 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(stream)); |
| | | 245 | | } |
| | | 246 | | |
| | 0 | 247 | | if (contentType == null) |
| | | 248 | | { |
| | 0 | 249 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType)); |
| | | 250 | | } |
| | | 251 | | |
| | 0 | 252 | | Message message = ByteStreamMessage.CreateMessage(stream); |
| | 0 | 253 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); |
| | 0 | 254 | | AddContentType(contentType, null); |
| | | 255 | | |
| | 0 | 256 | | return message; |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | public Message CreateStreamResponse(StreamBodyWriter bodyWriter, string contentType) |
| | | 260 | | { |
| | 0 | 261 | | if (bodyWriter == null) |
| | | 262 | | { |
| | 0 | 263 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bodyWriter)); |
| | | 264 | | } |
| | | 265 | | |
| | 0 | 266 | | if (contentType == null) |
| | | 267 | | { |
| | 0 | 268 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType)); |
| | | 269 | | } |
| | | 270 | | |
| | 0 | 271 | | Message message = new HttpStreamMessage(bodyWriter); |
| | 0 | 272 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); |
| | 0 | 273 | | AddContentType(contentType, null); |
| | | 274 | | |
| | 0 | 275 | | return message; |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | public Message CreateStreamResponse(Action<Stream> streamWriter, string contentType) |
| | | 279 | | { |
| | 0 | 280 | | if (streamWriter == null) |
| | | 281 | | { |
| | 0 | 282 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(streamWriter)); |
| | | 283 | | } |
| | | 284 | | |
| | 0 | 285 | | if (contentType == null) |
| | | 286 | | { |
| | 0 | 287 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contentType)); |
| | | 288 | | } |
| | | 289 | | |
| | 0 | 290 | | Message message = new HttpStreamMessage(ActionOfStreamBodyWriter.CreateStreamBodyWriter(streamWriter)); |
| | 0 | 291 | | message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); |
| | 0 | 292 | | AddContentType(contentType, null); |
| | | 293 | | |
| | 0 | 294 | | return message; |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | public UriTemplate GetUriTemplate(string operationName) |
| | | 298 | | { |
| | 0 | 299 | | if (string.IsNullOrEmpty(operationName)) |
| | | 300 | | { |
| | 0 | 301 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(operationName)); |
| | | 302 | | } |
| | | 303 | | |
| | 0 | 304 | | if (!(OperationContext.Current.EndpointDispatcher.DispatchRuntime.OperationSelector is WebHttpDispatchOperat |
| | | 305 | | { |
| | 0 | 306 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(SR.O |
| | | 307 | | } |
| | | 308 | | |
| | 0 | 309 | | return selector.GetUriTemplate(operationName); |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | private void AddContentType(string contentType, Encoding encoding) |
| | | 313 | | { |
| | 0 | 314 | | if (string.IsNullOrEmpty(OutgoingResponse.ContentType)) |
| | | 315 | | { |
| | 0 | 316 | | if (encoding != null) |
| | | 317 | | { |
| | 0 | 318 | | contentType = WebMessageEncoderFactory.GetContentType(contentType, encoding); |
| | | 319 | | } |
| | | 320 | | |
| | 0 | 321 | | OutgoingResponse.ContentType = contentType; |
| | | 322 | | } |
| | 0 | 323 | | } |
| | | 324 | | |
| | | 325 | | internal class XmlSerializerBodyWriter : BodyWriter |
| | | 326 | | { |
| | | 327 | | private readonly object _instance; |
| | | 328 | | private readonly XmlSerializer _serializer; |
| | | 329 | | |
| | | 330 | | public XmlSerializerBodyWriter(object instance, XmlSerializer serializer) |
| | 0 | 331 | | : base(false) |
| | | 332 | | { |
| | 0 | 333 | | _instance = instance ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(instance |
| | 0 | 334 | | _serializer = serializer ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(seri |
| | 0 | 335 | | } |
| | | 336 | | |
| | | 337 | | protected override void OnWriteBodyContents(XmlDictionaryWriter writer) |
| | | 338 | | { |
| | 0 | 339 | | _serializer.Serialize(writer, _instance); |
| | 0 | 340 | | } |
| | | 341 | | } |
| | | 342 | | } |
| | | 343 | | } |