private void PopulateData(EpmTargetPathSegment segment, object element, EpmContentSerializer.EpmNullValuedPropertyTree nullValuedProperties, DataServiceProviderWrapper provider)
#endif
        {
            if (segment.EpmInfo != null)
            {
                Object propertyValue;

                try
                {
#if ASTORIA_CLIENT
                    propertyValue = segment.EpmInfo.ReadPropertyValue(element);
#else
                    propertyValue = segment.EpmInfo.ReadPropertyValue(element, provider);
#endif
                }
                catch
#if ASTORIA_CLIENT
                (System.Reflection.TargetInvocationException)
#else
                (System.Reflection.TargetInvocationException e)
#endif
                {
#if !ASTORIA_CLIENT
                    ErrorHandler.HandleTargetInvocationException(e);
#endif
                    throw;
                }

#if ASTORIA_CLIENT
                this.Data = propertyValue == null ? String.Empty : ClientConvert.ToString(propertyValue, false);
#else
                if (propertyValue == null || propertyValue == DBNull.Value)
                {
                    this.Data = String.Empty;
                    nullValuedProperties.Add(segment.EpmInfo);
                }
                else
                {
                    this.Data = PlainXmlSerializer.PrimitiveToString(propertyValue);
                }
#endif
            }
        }
    }
예제 #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);
            }
        }
        /// <summary>
        /// Override of the base Visitor method, which actually performs mapping search and serialization
        /// </summary>
        /// <param name="targetSegment">Current segment being checked for mapping</param>
        /// <param name="kind">Which sub segments to serialize</param>
        /// <param name="provider">Data Service provider used for rights verification.</param>
        protected override void Serialize(EpmTargetPathSegment targetSegment, EpmSerializationKind kind, DataServiceProviderWrapper provider)
        {
            if (targetSegment.HasContent)
            {
                EntityPropertyMappingInfo epmInfo = targetSegment.EpmInfo;
                Object propertyValue;

                try
                {
                    propertyValue = epmInfo.ReadPropertyValue(this.Element, provider);
                }
                catch (System.Reflection.TargetInvocationException e)
                {
                    ErrorHandler.HandleTargetInvocationException(e);
                    throw;
                }

                if (propertyValue == null)
                {
                    this.nullValuedProperties.Add(epmInfo);
                }

                String textPropertyValue = propertyValue != null?PlainXmlSerializer.PrimitiveToString(propertyValue) : String.Empty;

                TextSyndicationContentKind contentKind = (TextSyndicationContentKind)epmInfo.Attribute.TargetTextContentKind;

                switch (epmInfo.Attribute.TargetSyndicationItem)
                {
                case SyndicationItemProperty.AuthorEmail:
                    this.CreateAuthor(false);
                    this.author.Email = textPropertyValue;
                    break;

                case SyndicationItemProperty.AuthorName:
                    this.CreateAuthor(false);
                    this.author.Name       = textPropertyValue;
                    this.authorNamePresent = true;
                    break;

                case SyndicationItemProperty.AuthorUri:
                    this.CreateAuthor(false);
                    this.author.Uri = textPropertyValue;
                    break;

                case SyndicationItemProperty.ContributorEmail:
                case SyndicationItemProperty.ContributorName:
                case SyndicationItemProperty.ContributorUri:
                    this.SetContributorProperty(epmInfo.Attribute.TargetSyndicationItem, textPropertyValue);
                    break;

                case SyndicationItemProperty.Updated:
                    this.Target.LastUpdatedTime = EpmSyndicationContentSerializer.GetDate(propertyValue, Strings.EpmSerializer_UpdatedHasWrongType);
                    break;

                case SyndicationItemProperty.Published:
                    this.Target.PublishDate = EpmSyndicationContentSerializer.GetDate(propertyValue, Strings.EpmSerializer_PublishedHasWrongType);
                    break;

                case SyndicationItemProperty.Rights:
                    this.Target.Copyright = new TextSyndicationContent(textPropertyValue, contentKind);
                    break;

                case SyndicationItemProperty.Summary:
                    this.Target.Summary = new TextSyndicationContent(textPropertyValue, contentKind);
                    break;

                case SyndicationItemProperty.Title:
                    this.Target.Title = new TextSyndicationContent(textPropertyValue, contentKind);
                    break;

                default:
                    Debug.Fail("Unhandled SyndicationItemProperty enum value - should never get here.");
                    break;
                }
            }
            else
            {
                base.Serialize(targetSegment, kind, provider);
            }
        }