Exemplo n.º 1
0
        internal void Write(Stream stream)
        {
            IExceptionWriter exceptionWriter = null;
            Stream output = null;
            Serializer serializer = null;
            System.Data.Services.ODataResponseMessage responseMessage = this.responseMessage as System.Data.Services.ODataResponseMessage;
            if (responseMessage != null)
            {
                responseMessage.SetStream(stream);
            }
            try
            {
                BinarySerializer serializer2;
                MetadataSerializer serializer7;
                switch (this.payloadKind)
                {
                    case ODataPayloadKind.Feed:
                    case ODataPayloadKind.Entry:
                    {
                        EntitySerializer serializer6 = new EntitySerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, this.service.OperationContext.Host.ResponseETag, this.messageWriter, this.contentFormat);
                        serializer = serializer6;
                        serializer6.WriteRequest(this.queryResults, this.hasMoved);
                        return;
                    }
                    case ODataPayloadKind.Property:
                    case ODataPayloadKind.EntityReferenceLink:
                    case ODataPayloadKind.EntityReferenceLinks:
                    case ODataPayloadKind.Collection:
                    {
                        NonEntitySerializer serializer5 = new NonEntitySerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, this.messageWriter);
                        serializer = serializer5;
                        serializer5.WriteRequest(this.queryResults, this.hasMoved);
                        return;
                    }
                    case ODataPayloadKind.Value:
                        new TextSerializer(this.messageWriter).WriteRequest(this.queryResults.Current);
                        return;

                    case ODataPayloadKind.BinaryValue:
                        output = this.responseMessage.GetStream();
                        serializer2 = new BinarySerializer(output);
                        exceptionWriter = serializer2;
                        if (this.requestDescription.TargetKind != RequestTargetKind.MediaResource)
                        {
                            break;
                        }
                        if (this.mediaResourceStream != null)
                        {
                            serializer2.WriteRequest(this.mediaResourceStream, this.service.StreamProvider.StreamBufferSize);
                        }
                        return;

                    case ODataPayloadKind.ServiceDocument:
                        new ServiceDocumentSerializer(this.messageWriter).WriteServiceDocument(this.service.Provider);
                        return;

                    default:
                        goto Label_0194;
                }
                serializer2.WriteRequest(this.queryResults.Current);
                return;
            Label_0194:
                serializer7 = new MetadataSerializer(this.messageWriter);
                serializer7.WriteMetadataDocument(this.service);
            }
            catch (Exception exception)
            {
                if (!CommonUtil.IsCatchableExceptionType(exception))
                {
                    throw;
                }
                if (serializer != null)
                {
                    serializer.Flush();
                }
                string contentType = this.responseMessage.GetHeader("Content-Type").StartsWith("application/json", StringComparison.OrdinalIgnoreCase) ? "application/json;odata=verbose" : "application/xml";
                if (this.messageWriter != null)
                {
                    ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, this.messageWriter, stream, this.encoding);
                }
                else
                {
                    ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, exceptionWriter);
                }
            }
            finally
            {
                WebUtil.Dispose(this.messageWriter);
                WebUtil.Dispose(this.queryResults);
                WebUtil.Dispose(this.mediaResourceStream);
                if ((output != null) && (responseMessage == null))
                {
                    output.Dispose();
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>Writes the request body to the specified <see cref="Stream"/>.</summary>
        /// <param name="stream">Stream to write to.</param>
        internal void Write(Stream stream)
        {
            Debug.Assert(stream != null, "stream != null");
            IExceptionWriter exceptionWriter = null;

            try
            {
                switch (this.responseFormat)
                {
                    case ContentFormat.Binary:
                        Debug.Assert(
                            this.requestDescription.TargetKind == RequestTargetKind.OpenPropertyValue ||
                            this.requestDescription.TargetKind == RequestTargetKind.PrimitiveValue ||
                            this.requestDescription.TargetKind == RequestTargetKind.MediaResource,
                            this.requestDescription.TargetKind + " is PrimitiveValue or OpenPropertyValue or StreamPropertyValue");

                        BinarySerializer binarySerializer = new BinarySerializer(stream);
                        exceptionWriter = binarySerializer;
                        if (this.requestDescription.TargetKind == RequestTargetKind.MediaResource)
                        {
                            Debug.Assert(this.mediaResourceStream != null, "this.mediaResourceStream != null");

                            binarySerializer.WriteRequest(this.mediaResourceStream, this.service.StreamProvider.StreamBufferSize);
                        }
                        else
                        {
                            binarySerializer.WriteRequest(this.queryResults.Current);
                        }

                        break;

                    case ContentFormat.Text:
                        Debug.Assert(
                            this.requestDescription.TargetKind == RequestTargetKind.OpenPropertyValue ||
                            this.requestDescription.TargetKind == RequestTargetKind.PrimitiveValue,
                            this.requestDescription.TargetKind + " is PrimitiveValue or OpenPropertyValue");

                        TextSerializer textSerializer = new TextSerializer(stream, this.encoding);
                        exceptionWriter = textSerializer;
                        textSerializer.WriteRequest(this.queryResults.Current);
                        break;

                    case ContentFormat.Atom:
                    case ContentFormat.Json:
                    case ContentFormat.PlainXml:
                        Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.PrimitiveValue, "this.requestDescription.TargetKind != RequestTargetKind.PrimitiveValue");
                        Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.OpenPropertyValue, "this.requestDescription.TargetKind != RequestTargetKind.OpenPropertyValue");
                        Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.Metadata, "this.requestDescription.TargetKind != RequestTargetKind.Metadata");

                        if (this.requestDescription.TargetKind == RequestTargetKind.ServiceDirectory)
                        {
                            if (this.responseFormat == ContentFormat.Json)
                            {
                                JsonServiceDocumentSerializer serviceSerializer = new JsonServiceDocumentSerializer(stream, this.Provider, this.encoding);
                                exceptionWriter = serviceSerializer;
                                serviceSerializer.WriteRequest();
                                break;
                            }
                            else
                            {
                                AtomServiceDocumentSerializer serviceSerializer = new AtomServiceDocumentSerializer(stream, this.AbsoluteServiceUri, this.Provider, this.encoding);
                                exceptionWriter = serviceSerializer;
                                serviceSerializer.WriteRequest(this.service);
                            }
                        }
                        else
                        {
                            Serializer serializer;
                            if (ContentFormat.Json == this.responseFormat)
                            {
                                serializer = new JsonSerializer(this.requestDescription, stream, this.AbsoluteServiceUri, this.service, this.encoding, this.Host.ResponseETag);
                            }
                            else if (ContentFormat.PlainXml == this.responseFormat)
                            {
                                serializer = new PlainXmlSerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, stream, this.encoding);
                            }
                            else
                            {
                                Debug.Assert(
                                    this.requestDescription.TargetKind == RequestTargetKind.OpenProperty ||
                                    this.requestDescription.TargetKind == RequestTargetKind.Resource,
                                    "TargetKind " + this.requestDescription.TargetKind + " == Resource || OpenProperty -- POX should have handled it otherwise.");
                                serializer = new SyndicationSerializer(
                                    this.requestDescription,
                                    this.AbsoluteServiceUri,
                                    this.service,
                                    stream,
                                    this.encoding,
                                    this.Host.ResponseETag,
                                    new Atom10FormatterFactory());
                            }

                            exceptionWriter = serializer;
                            Debug.Assert(exceptionWriter != null, "this.exceptionWriter != null");
                            serializer.WriteRequest(this.queryResults, this.hasMoved);
                        }

                        break;

                    default:
                        Debug.Assert(
                            this.responseFormat == ContentFormat.MetadataDocument,
                            "responseFormat(" + this.responseFormat + ") == ContentFormat.MetadataDocument -- otherwise exception should have been thrown before");
                        Debug.Assert(this.requestDescription.TargetKind == RequestTargetKind.Metadata, "this.requestDescription.TargetKind == RequestTargetKind.Metadata");
                        MetadataSerializer metadataSerializer = new MetadataSerializer(stream, this.AbsoluteServiceUri, this.Provider, this.encoding);
                        exceptionWriter = metadataSerializer;
                        metadataSerializer.WriteRequest(this.service);
                        break;
                }
            }
            catch (Exception exception)
            {
                if (!WebUtil.IsCatchableExceptionType(exception))
                {
                    throw;
                }

                // Only JSON and XML are supported.
                string contentType = (this.responseFormat == ContentFormat.Json) ? XmlConstants.MimeApplicationJson : XmlConstants.MimeApplicationXml;
                ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, exceptionWriter);
            }
            finally
            {
                WebUtil.Dispose(this.queryResults);
                WebUtil.Dispose(this.mediaResourceStream);
            }
        }