Esempio n. 1
0
 /// <summary>Writes the contents of this SyndicationContent object to the specified XmlWriter.</summary>
 /// <param name='writer'>The XmlWriter to write to.</param>
 private void WritePropertyContentsTo(XmlWriter writer)
 {
     for (int i = 0; i < this.valueNames.Count; i++)
     {
         string propertyName     = this.valueNames[i];
         string propertyTypeName = this.valueTypes[i];
         object propertyValue    = this.valueContents[i];
         if (propertyValue == null)
         {
             PlainXmlSerializer.WriteNullValue(writer, propertyName, propertyTypeName);
         }
         else if (propertyValue is DictionaryContent)
         {
             PlainXmlSerializer.WriteStartElementWithType(writer, propertyName, propertyTypeName);
             ((DictionaryContent)propertyValue).WritePropertyContentsTo(writer);
             writer.WriteEndElement();
         }
         else
         {
             string propertyText = (string)propertyValue;
             PlainXmlSerializer.WriteTextValue(writer, propertyName, propertyTypeName, propertyText);
         }
     }
 }
Esempio 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);
            }
        }