1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
//
// WebMessageEncoder.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.IO;
using System.Runtime.Serialization.Json;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Xml;
namespace System.ServiceModel.Channels
{
internal class WebMessageEncoder : MessageEncoder
{
internal const string ScriptPropertyName = "618BC2B0-38AA-21A3-DB4A-404FC87B9B11"; // randomly generated
WebMessageEncodingBindingElement source;
public WebMessageEncoder (WebMessageEncodingBindingElement source)
{
this.source = source;
}
public override string ContentType {
#if MOBILE
get { return MediaType; }
#else
get { return MediaType + "; charset=" + source.WriteEncoding.HeaderName; }
#endif
}
// FIXME: find out how it can be customized.
public override string MediaType {
get { return "application/xml"; }
}
public override MessageVersion MessageVersion {
get { return MessageVersion.None; }
}
public override bool IsContentTypeSupported (string contentType)
{
if (contentType == null)
throw new ArgumentNullException ("contentType");
return true; // anything is accepted.
}
public override Message ReadMessage (ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
{
throw new NotImplementedException ();
}
public override Message ReadMessage (Stream stream, int maxSizeOfHeaders, string contentType)
{
if (stream == null)
throw new ArgumentNullException ("stream");
contentType = contentType ?? "application/octet-stream";
Encoding enc = Encoding.UTF8;
var ct = new System.Net.Mime.ContentType (contentType);
if (ct.CharSet != null)
enc = Encoding.GetEncoding (ct.CharSet);
WebContentFormat fmt = WebContentFormat.Xml;
if (source.ContentTypeMapper != null)
fmt = source.ContentTypeMapper.GetMessageFormatForContentType (contentType);
else {
switch (ct.MediaType) {
case "application/json":
fmt = WebContentFormat.Json;
break;
case "application/xml":
fmt = WebContentFormat.Xml;
break;
default:
fmt = WebContentFormat.Raw;
break;
}
}
Message msg = null;
WebBodyFormatMessageProperty wp = null;
switch (fmt) {
case WebContentFormat.Xml:
// FIXME: is it safe/unsafe/required to keep XmlReader open?
msg = Message.CreateMessage (MessageVersion.None, null, XmlReader.Create (new StreamReader (stream, enc)));
wp = new WebBodyFormatMessageProperty (WebContentFormat.Xml);
break;
case WebContentFormat.Json:
// FIXME: is it safe/unsafe/required to keep XmlReader open?
#if MOBILE
msg = Message.CreateMessage (MessageVersion.None, null, JsonReaderWriterFactory.CreateJsonReader (stream, source.ReaderQuotas));
#else
msg = Message.CreateMessage (MessageVersion.None, null, JsonReaderWriterFactory.CreateJsonReader (stream, enc, source.ReaderQuotas, null));
#endif
wp = new WebBodyFormatMessageProperty (WebContentFormat.Json);
break;
case WebContentFormat.Raw:
msg = new WebMessageFormatter.RawMessage (stream);
wp = new WebBodyFormatMessageProperty (WebContentFormat.Raw);
break;
default:
throw new SystemException ("INTERNAL ERROR: cannot determine content format");
}
if (wp != null)
msg.Properties.Add (WebBodyFormatMessageProperty.Name, wp);
msg.Properties.Encoder = this;
return msg;
}
WebContentFormat GetContentFormat (Message message)
{
string name = WebBodyFormatMessageProperty.Name;
if (message.Properties.ContainsKey (name))
return ((WebBodyFormatMessageProperty) message.Properties [name]).Format;
switch (MediaType) {
case "application/xml":
case "text/xml":
return WebContentFormat.Xml;
case "application/json":
case "text/json":
return WebContentFormat.Json;
case "application/octet-stream":
return WebContentFormat.Raw;
default:
return WebContentFormat.Default;
}
}
public override void WriteMessage (Message message, Stream stream)
{
if (message == null)
throw new ArgumentNullException ("message");
// Handle /js and /jsdebug as the special cases.
var script = message.Properties [ScriptPropertyName] as string;
if (script != null) {
var bytes = source.WriteEncoding.GetBytes (script);
stream.Write (bytes, 0, bytes.Length);
return;
}
if (!MessageVersion.Equals (message.Version))
throw new ProtocolException (String.Format ("MessageVersion {0} is not supported", message.Version));
if (stream == null)
throw new ArgumentNullException ("stream");
switch (GetContentFormat (message)) {
case WebContentFormat.Xml:
#if MOBILE
using (XmlWriter w = XmlDictionaryWriter.CreateDictionaryWriter (XmlWriter.Create (new StreamWriter (stream, source.WriteEncoding))))
message.WriteMessage (w);
#else
using (XmlWriter w = XmlDictionaryWriter.CreateTextWriter (stream, source.WriteEncoding, false))
message.WriteMessage (w);
#endif
break;
case WebContentFormat.Json:
using (XmlWriter w = JsonReaderWriterFactory.CreateJsonWriter (stream, source.WriteEncoding, false))
message.WriteMessage (w);
break;
case WebContentFormat.Raw:
var rmsg = (WebMessageFormatter.RawMessage) message;
var src = rmsg.Stream;
if (src == null) // null output
break;
int len = 0;
byte [] buffer = new byte [4096];
while ((len = src.Read (buffer, 0, buffer.Length)) > 0)
stream.Write (buffer, 0, len);
break;
case WebContentFormat.Default:
throw new SystemException ("INTERNAL ERROR: cannot determine content format");
}
}
public override ArraySegment<byte> WriteMessage (Message message, int maxMessageSize, BufferManager bufferManager,
int messageOffset)
{
throw new NotImplementedException ();
}
}
}
|