aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-09-16 10:55:10 -0500
committerrogerk <devnull@localhost>2011-09-16 10:55:10 -0500
commit2cf6e1b07749b98ff0191a51fa76f1fb3d970724 (patch)
tree10f57217f70ab02310fcca90b19849e208313eef /src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs
parentc86b504409f0b1b57764fd42376de28b13968a8f (diff)
downloadprotobuf-2cf6e1b07749b98ff0191a51fa76f1fb3d970724.tar.gz
protobuf-2cf6e1b07749b98ff0191a51fa76f1fb3d970724.tar.bz2
protobuf-2cf6e1b07749b98ff0191a51fa76f1fb3d970724.zip
Last change for http support, adding a simple reader for query strings and/or
url-encoded form data to support restful apis. Also exposed the mime to reader and mime to writer via dictionaries in the MessageFormatOptions structure.
Diffstat (limited to 'src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs')
-rw-r--r--src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs110
1 files changed, 39 insertions, 71 deletions
diff --git a/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs b/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs
index 2ee7eb4d..8d389f07 100644
--- a/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs
+++ b/src/ProtocolBuffers.Serialization/Http/MessageFormatFactory.cs
@@ -19,27 +19,14 @@ namespace Google.ProtocolBuffers.Serialization.Http
/// <returns>The ICodedInputStream that can be given to the IBuilder.MergeFrom(...) method</returns>
public static ICodedInputStream CreateInputStream(MessageFormatOptions options, string contentType, Stream input)
{
- FormatType inputType = ContentTypeToFormat(contentType, options.DefaultContentType);
+ ICodedInputStream codedInput = ContentTypeToInputStream(contentType, options, input);
- ICodedInputStream codedInput;
- if (inputType == FormatType.ProtoBuffer)
+ if (codedInput is XmlFormatReader)
{
- codedInput = CodedInputStream.CreateInstance(input);
- }
- else if (inputType == FormatType.Json)
- {
- JsonFormatReader reader = JsonFormatReader.CreateInstance(input);
- codedInput = reader;
- }
- else if (inputType == FormatType.Xml)
- {
- XmlFormatReader reader = XmlFormatReader.CreateInstance(input);
+ XmlFormatReader reader = (XmlFormatReader)codedInput;
reader.RootElementName = options.XmlReaderRootElementName;
reader.Options = options.XmlReaderOptions;
- codedInput = reader;
}
- else
- throw new NotSupportedException();
return codedInput;
}
@@ -69,30 +56,20 @@ namespace Google.ProtocolBuffers.Serialization.Http
/// <remarks> If you do not dispose of ICodedOutputStream some formats may yield incomplete output </remarks>
public static ICodedOutputStream CreateOutputStream(MessageFormatOptions options, string contentType, Stream output)
{
- FormatType outputType = ContentTypeToFormat(contentType, options.DefaultContentType);
+ ICodedOutputStream codedOutput = ContentTypeToOutputStream(contentType, options, output);
- ICodedOutputStream codedOutput;
- if (outputType == FormatType.ProtoBuffer)
- {
- codedOutput = CodedOutputStream.CreateInstance(output);
- }
- else if (outputType == FormatType.Json)
+ if (codedOutput is JsonFormatWriter)
{
- JsonFormatWriter writer = JsonFormatWriter.CreateInstance(output);
+ JsonFormatWriter writer = (JsonFormatWriter)codedOutput;
if (options.FormattedOutput)
{
writer.Formatted();
}
- codedOutput = writer;
}
- else if (outputType == FormatType.Xml)
+ else if (codedOutput is XmlFormatWriter)
{
- XmlFormatWriter writer;
- if (!options.FormattedOutput)
- {
- writer = XmlFormatWriter.CreateInstance(output);
- }
- else
+ XmlFormatWriter writer = (XmlFormatWriter)codedOutput;
+ if (options.FormattedOutput)
{
XmlWriterSettings settings = new XmlWriterSettings()
{
@@ -104,14 +81,12 @@ namespace Google.ProtocolBuffers.Serialization.Http
IndentChars = " ",
NewLineChars = Environment.NewLine,
};
- writer = XmlFormatWriter.CreateInstance(XmlWriter.Create(output, settings));
+ // Don't know how else to change xml writer options?
+ codedOutput = writer = XmlFormatWriter.CreateInstance(XmlWriter.Create(output, settings));
}
writer.RootElementName = options.XmlWriterRootElementName;
writer.Options = options.XmlWriterOptions;
- codedOutput = writer;
}
- else
- throw new NotSupportedException();
return codedOutput;
}
@@ -137,46 +112,39 @@ namespace Google.ProtocolBuffers.Serialization.Http
codedOutput.WriteMessageEnd();
}
- enum FormatType { ProtoBuffer, Json, Xml };
+ private static ICodedInputStream ContentTypeToInputStream(string contentType, MessageFormatOptions options, Stream input)
+ {
+ contentType = (contentType ?? String.Empty).Split(';')[0].Trim();
- private static FormatType ContentTypeToFormat(string contentType, string defaultType)
+ Converter<Stream, ICodedInputStream> factory;
+ if(!options.MimeInputTypesReadOnly.TryGetValue(contentType, out factory) || factory == null)
+ {
+ if(String.IsNullOrEmpty(options.DefaultContentType) ||
+ !options.MimeInputTypesReadOnly.TryGetValue(options.DefaultContentType, out factory) || factory == null)
+ {
+ throw new ArgumentOutOfRangeException("contentType");
+ }
+ }
+
+ return factory(input);
+ }
+
+ private static ICodedOutputStream ContentTypeToOutputStream(string contentType, MessageFormatOptions options, Stream output)
{
- switch ((contentType ?? String.Empty).Split(';')[0].Trim().ToLower())
+ contentType = (contentType ?? String.Empty).Split(';')[0].Trim();
+
+ Converter<Stream, ICodedOutputStream> factory;
+ if (!options.MimeOutputTypesReadOnly.TryGetValue(contentType, out factory) || factory == null)
{
- case "application/json":
- case "application/x-json":
- case "application/x-javascript":
- case "text/javascript":
- case "text/x-javascript":
- case "text/x-json":
- case "text/json":
- {
- return FormatType.Json;
- }
-
- case "text/xml":
- case "application/xml":
- {
- return FormatType.Xml;
- }
-
- case "application/binary":
- case "application/x-protobuf":
- case "application/vnd.google.protobuf":
- {
- return FormatType.ProtoBuffer;
- }
-
- case "":
- case null:
- if (!String.IsNullOrEmpty(defaultType))
- {
- return ContentTypeToFormat(defaultType, null);
- }
- break;
+ if (String.IsNullOrEmpty(options.DefaultContentType) ||
+ !options.MimeOutputTypesReadOnly.TryGetValue(options.DefaultContentType, out factory) || factory == null)
+ {
+ throw new ArgumentOutOfRangeException("contentType");
+ }
}
- throw new ArgumentOutOfRangeException("contentType");
+ return factory(output);
}
+
}
} \ No newline at end of file