Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
0
        internal void Write(Stream stream)
        {
            Stream     responseMessageStream = null;
            Serializer responseSerializer    = null;

            this.actualResponseMessageWhoseHeadersMayBeOverridden.SetStream(stream);

            try
            {
                switch (this.PayloadKind)
                {
                case ODataPayloadKind.BinaryValue:
                    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");
                    Debug.Assert(this.messageWriter == null, "No Message writer required for writing binary payload");

                    responseMessageStream = this.actualResponseMessageWhoseHeadersMayBeOverridden.GetStream();
                    BinarySerializer binarySerializer = new BinarySerializer(responseMessageStream);
                    if (this.requestDescription.TargetKind == RequestTargetKind.MediaResource)
                    {
                        // If this.mediaResourceStream is null, we will set the status code to 204.
                        // We would not get here on a WCF host, but on other hosts we would.
                        Debug.Assert(this.requestDescription.IsNamedStream || this.mediaResourceStream != null, "this.mediaResourceStream cannot be null for MR request.");
                        if (this.mediaResourceStream != null)
                        {
                            binarySerializer.WriteRequest(this.mediaResourceStream, this.service.StreamProvider.StreamBufferSize);
                        }
                    }
                    else
                    {
                        binarySerializer.WriteRequest(this.queryResults.Current);
                    }

                    break;

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

                    TextSerializer textSerializer = new TextSerializer(this.messageWriter);
                    textSerializer.WriteRequest(this.queryResults.Current);
                    break;

                case ODataPayloadKind.ServiceDocument:
                    Debug.Assert(this.requestDescription.TargetKind == RequestTargetKind.ServiceDirectory, "this.requestDescription.TargetKind == RequestTargetKind.ServiceDirectory");
                    ServiceDocumentSerializer serializer = new ServiceDocumentSerializer(this.messageWriter);
                    serializer.WriteServiceDocument(this.service.Provider);
                    break;

                case ODataPayloadKind.EntityReferenceLink:
                case ODataPayloadKind.EntityReferenceLinks:
                case ODataPayloadKind.Collection:
                case ODataPayloadKind.Property:
                    Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.Resource || this.requestDescription.LinkUri, "this.requestDescription.TargetKind != RequestTargetKind.Resource || this.requestDescription.LinkUri");
                    NonEntitySerializer nonEntitySerializer = new NonEntitySerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, this.messageWriter);
                    responseSerializer = nonEntitySerializer;
                    nonEntitySerializer.WriteRequest(this.queryResults);
                    break;

                case ODataPayloadKind.Entry:
                case ODataPayloadKind.Feed:
                    Debug.Assert(this.requestDescription.TargetKind == RequestTargetKind.Resource, "TargetKind " + this.requestDescription.TargetKind + " == Resource");
                    EntitySerializer entitySerializer = new EntitySerializer(
                        this.requestDescription,
                        this.AbsoluteServiceUri,
                        this.service,
                        this.service.OperationContext.ResponseMessage.GetHeader(XmlConstants.HttpResponseETag),
                        this.messageWriter,
                        this.contentFormat);

                    responseSerializer = entitySerializer;
                    entitySerializer.WriteRequest(this.queryResults);
                    break;

                default:
                    Debug.Assert(this.PayloadKind == ODataPayloadKind.MetadataDocument, "this.payloadKind == ODataPayloadKind.MetadataDocument");
                    Debug.Assert(this.requestDescription.TargetKind == RequestTargetKind.Metadata, "this.requestDescription.TargetKind == RequestTargetKind.Metadata");

                    this.messageWriter.WriteMetadataDocument();
                    break;
                }
            }
            catch (Exception exception)
            {
                if (!CommonUtil.IsCatchableExceptionType(exception))
                {
                    throw;
                }

                // Astoria-ODataLib-Integration: Astoria does not call flush before calling the IDataServiceHost.ProcessException method
                // If the request is for an entry/feed and the data source throws an error while these results are being enumerated and written,
                // the server doesn't flush the writer's stream before it calls HandleException and starts writing out the error.
                // To handle this case, we'll make the EntitySerializer expose a method that calls Flush on the underlying ODL writer instance.
                if (responseSerializer != null)
                {
                    responseSerializer.Flush();
                }

                ErrorHandler.HandleExceptionWhileWriting(this.service, exception, this.actualResponseMessageWhoseHeadersMayBeOverridden, this.messageWriter, this.encoding, stream, this.messageWriterBuilder);
            }
            finally
            {
                WebUtil.Dispose(this.messageWriter);
                WebUtil.Dispose(this.queryResults);
                WebUtil.Dispose(this.mediaResourceStream);

                // For batch operations, we need to dispose the stream obtained from calling
                // IODataResponseMessage.GetStream to signal that we are done with the message.
                if (responseMessageStream != null && this.actualResponseMessageWhoseHeadersMayBeOverridden is ODataBatchOperationResponseMessage)
                {
                    responseMessageStream.Dispose();
                }
            }
        }