예제 #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this output context.</param>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        internal ODataRawOutputContext(
            ODataFormat format,
            ODataMessageInfo messageInfo,
            ODataMessageWriterSettings messageWriterSettings)
            : base(format, messageInfo, messageWriterSettings)
        {
            Debug.Assert(messageInfo.MessageStream != null, "messageInfo.MessageStream != null");

            try
            {
                this.messageOutputStream = messageInfo.MessageStream;
                this.encoding = messageInfo.Encoding;

                if (this.Synchronous)
                {
                    this.outputStream = this.messageOutputStream;
                }
                else
                {
                    this.asynchronousOutputStream = new AsyncBufferedStream(this.messageOutputStream);
                    this.outputStream = this.asynchronousOutputStream;
                }
            }
            catch
            {
                this.messageOutputStream.Dispose();
                throw;
            }
        }
 /// <summary>Sets the format to be used when writing the payload. This will automatically set a compatible content type header.</summary>
 /// <param name="payloadFormat">The format to use for writing the payload.</param>
 /// <remarks>Calling this method replaces any previously set content-type settings.</remarks>
 public void SetContentType(ODataFormat payloadFormat)
 {
     this.acceptCharSets   = null;
     this.acceptMediaTypes = null;
     this.format           = payloadFormat;
     this.useFormat        = true;
 }
        private void CopyFrom(ODataMessageWriterSettings other)
        {
            ExceptionUtils.CheckArgumentNotNull(other, "other");

            this.acceptCharSets              = other.acceptCharSets;
            this.acceptMediaTypes            = other.acceptMediaTypes;
            this.BaseUri                     = other.BaseUri;
            this.EnableMessageStreamDisposal = other.EnableMessageStreamDisposal;
            this.EnableCharactersCheck       = other.EnableCharactersCheck;
            this.format                  = other.format;
            this.JsonPCallback           = other.JsonPCallback;
            this.messageQuotas           = new ODataMessageQuotas(other.MessageQuotas);
            this.ODataUri                = other.ODataUri.Clone();
            this.shouldIncludeAnnotation = other.shouldIncludeAnnotation;
            this.useFormat               = other.useFormat;
            this.Version                 = other.Version;
            this.LibraryCompatibility    = other.LibraryCompatibility;
            this.AlwaysAddTypeAnnotationsForDerivedTypes = other.AlwaysAddTypeAnnotationsForDerivedTypes;
            this.MetadataSelector    = other.MetadataSelector;
            this.IsIeee754Compatible = other.IsIeee754Compatible;

            this.validations = other.validations;
            this.ThrowIfTypeConflictsWithMetadata        = other.ThrowIfTypeConflictsWithMetadata;
            this.ThrowOnDuplicatePropertyNames           = other.ThrowOnDuplicatePropertyNames;
            this.ThrowOnUndeclaredPropertyForNonOpenType = other.ThrowOnUndeclaredPropertyForNonOpenType;
            this.ArrayPool = other.ArrayPool;
        }
        /// <summary>Constructor.</summary>
        /// <param name="format">The format for this input context.</param>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageReaderSettings">Configuration settings of the OData reader.</param>
        public ODataRawInputContext(
            ODataFormat format,
            ODataMessageInfo messageInfo,
            ODataMessageReaderSettings messageReaderSettings)
            : base(format, messageInfo, messageReaderSettings)
        {
            Debug.Assert(messageInfo.MessageStream != null, "messageInfo.MessageStream != null");
            Debug.Assert(messageInfo.PayloadKind != ODataPayloadKind.Unsupported, "readerPayloadKind != ODataPayloadKind.Unsupported");

            try
            {
                this.stream            = messageInfo.MessageStream;
                this.Encoding          = messageInfo.Encoding;
                this.readerPayloadKind = messageInfo.PayloadKind;
            }
            catch (Exception e)
            {
                // Dispose the message stream if we failed to create the input context.
                if (ExceptionUtils.IsCatchableExceptionType(e))
                {
                    messageInfo.MessageStream.Dispose();
                }

                throw;
            }
        }
예제 #5
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this output context.</param>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        internal ODataRawOutputContext(
            ODataFormat format,
            ODataMessageInfo messageInfo,
            ODataMessageWriterSettings messageWriterSettings)
            : base(format, messageInfo, messageWriterSettings)
        {
            Debug.Assert(messageInfo.MessageStream != null, "messageInfo.MessageStream != null");

            try
            {
                this.messageOutputStream = messageInfo.MessageStream;
                this.encoding            = messageInfo.Encoding;

                if (this.Synchronous)
                {
                    this.outputStream = this.messageOutputStream;
                }
                else
                {
#if NETSTANDARD1_1
                    this.asynchronousOutputStream = new AsyncBufferedStream(this.messageOutputStream);
#else
                    this.asynchronousOutputStream = new BufferedStream(this.messageOutputStream, ODataConstants.DefaultOutputBufferSize);
#endif
                    this.outputStream = this.asynchronousOutputStream;
                }
            }
            catch
            {
                this.messageOutputStream.Dispose();
                throw;
            }
        }
예제 #6
0
        /// <summary>
        /// Instantiate an <see cref="ODataBatchOperationRequestMessage"/> instance.
        /// </summary>
        /// <param name="streamCreatorFunc">The function for stream creation.</param>
        /// <param name="method">The HTTP method used for this request message.</param>
        /// <param name="requestUri">The request Url for this request message.</param>
        /// <param name="headers">The headers for this request message.</param>
        /// <param name="contentId">The contentId of this request message.</param>
        /// <param name="groupId">The group id that this request belongs to. Can be null.</param>
        /// <param name="dependsOnRequestIds">
        /// The prerequisite request Ids of this request message. For batch in Json format,
        /// some of these request Ids are resolved from prerequisite atomic groups that are
        /// specified in the dependsOn attribute of the request.
        /// </param>
        /// <param name="batchFormat">Format of the batch.</param>
        /// <returns>The <see cref="ODataBatchOperationRequestMessage"/> instance.</returns>
        protected ODataBatchOperationRequestMessage BuildOperationRequestMessage(
            Func <Stream> streamCreatorFunc,
            string method,
            Uri requestUri,
            ODataBatchOperationHeaders headers,
            string contentId,
            string groupId,
            IEnumerable <string> dependsOnRequestIds,
            ODataFormat batchFormat)
        {
            if (dependsOnRequestIds != null)
            {
                foreach (string id in dependsOnRequestIds)
                {
                    if (!this.payloadUriConverter.ContainsContentId(id))
                    {
                        throw new ODataException(Strings.ODataBatchReader_DependsOnIdNotFound(id, contentId));
                    }
                }

                ODataBatchUtils.ValidateReferenceUri(requestUri, dependsOnRequestIds,
                                                     this.inputContext.MessageReaderSettings.BaseUri, batchFormat);
            }

            Uri uri = ODataBatchUtils.CreateOperationRequestUri(
                requestUri, this.inputContext.MessageReaderSettings.BaseUri, this.payloadUriConverter);

            return(new ODataBatchOperationRequestMessage(streamCreatorFunc, method, uri, headers, this,
                                                         contentId, this.payloadUriConverter, /*writing*/ false, this.container, dependsOnRequestIds, groupId));
        }
 /// <summary>Sets the acceptable media types and character sets from which the content type will be computed when writing the payload.</summary>
 /// <param name="acceptableMediaTypes">The acceptable media types used to determine the content type of the message. This is a comma separated list of content types as specified in RFC 2616, Section 14.1.</param>
 /// <param name="acceptableCharSets"> The acceptable charsets to use to determine the encoding of the message. This is a comma separated list of charsets as specified in RFC 2616, Section 14.2 </param>
 /// <remarks>Calling this method replaces any previously set content-type settings.</remarks>
 public void SetContentType(string acceptableMediaTypes, string acceptableCharSets)
 {
     // Should accept json as application/json
     this.acceptMediaTypes = string.Equals(acceptableMediaTypes, MimeConstants.MimeJsonSubType, StringComparison.OrdinalIgnoreCase) ? MimeConstants.MimeApplicationJson : acceptableMediaTypes;
     this.acceptCharSets   = acceptableCharSets;
     this.format           = null;
     this.useFormat        = false;
 }
예제 #8
0
        /// <summary>
        /// Gets the default media type for a given payload kind in a given format.
        /// </summary>
        /// <param name="supportedMediaTypes">A list of supported media types and formats.</param>
        /// <param name="specifiedFormat">The user-specified format in which to write the payload (can be null).</param>
        /// <param name="actualFormat">The default format for the specified payload kind</param>
        /// <returns>The default media type for the given payload kind and format.</returns>
        private static ODataMediaType GetDefaultMediaType(
            IList <ODataMediaTypeFormat> supportedMediaTypes,
            ODataFormat specifiedFormat,
            out ODataFormat actualFormat)
        {
            for (int i = 0; i < supportedMediaTypes.Count; ++i)
            {
                // NOTE: the supportedMediaTypes are sorted (DESC) by format and media type; so the
                //       default format and media type is the first resource in the list
                ODataMediaTypeFormat supportedMediaType = supportedMediaTypes[i];
                if (specifiedFormat == null || supportedMediaType.Format == specifiedFormat)
                {
                    actualFormat = supportedMediaType.Format;
                    return(supportedMediaType.MediaType);
                }
            }

            throw new ODataException(Strings.ODataUtils_DidNotFindDefaultMediaType(specifiedFormat));
        }
예제 #9
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this input context.</param>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageReaderSettings">Configuration settings of the OData reader.</param>
        protected ODataInputContext(
            ODataFormat format,
            ODataMessageInfo messageInfo,
            ODataMessageReaderSettings messageReaderSettings)
        {
            ExceptionUtils.CheckArgumentNotNull(format, "format");
            ExceptionUtils.CheckArgumentNotNull(messageInfo, "messageInfo");
            ExceptionUtils.CheckArgumentNotNull(messageReaderSettings, "messageReaderSettings");

            this.format = format;
            this.messageReaderSettings = messageReaderSettings;
            this.readingResponse       = messageInfo.IsResponse;
            this.synchronous           = !messageInfo.IsAsync;
            this.model = messageInfo.Model ?? EdmCoreModel.Instance;
            this.payloadUriConverter    = messageInfo.PayloadUriConverter;
            this.container              = messageInfo.Container;
            this.edmTypeResolver        = new EdmTypeReaderResolver(this.Model, this.MessageReaderSettings.ClientCustomTypeResolver);
            this.payloadValueConverter  = ODataPayloadValueConverter.GetPayloadValueConverter(this.container);
            this.odataSimplifiedOptions = ODataSimplifiedOptions.GetODataSimplifiedOptions(this.container, messageReaderSettings.Version);
        }
예제 #10
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="format">The format for this output context.</param>
        /// <param name="messageInfo">The context information for the message.</param>
        /// <param name="messageWriterSettings">Configuration settings of the OData writer.</param>
        protected ODataOutputContext(
            ODataFormat format,
            ODataMessageInfo messageInfo,
            ODataMessageWriterSettings messageWriterSettings)
        {
            ExceptionUtils.CheckArgumentNotNull(format, "format");
            ExceptionUtils.CheckArgumentNotNull(messageWriterSettings, "messageWriterSettings");

            this.format = format;
            this.messageWriterSettings = messageWriterSettings;
            this.writingResponse       = messageInfo.IsResponse;
            this.synchronous           = !messageInfo.IsAsync;
            this.model = messageInfo.Model ?? EdmCoreModel.Instance;
            this.payloadUriConverter    = messageInfo.PayloadUriConverter;
            this.container              = messageInfo.Container;
            this.edmTypeResolver        = EdmTypeWriterResolver.Instance;
            this.payloadValueConverter  = ODataPayloadValueConverter.GetPayloadValueConverter(this.container);
            this.writerValidator        = messageWriterSettings.Validator;
            this.odataSimplifiedOptions = ODataSimplifiedOptions.GetODataSimplifiedOptions(this.container);
        }
예제 #11
0
        private void CopyFrom(ODataMessageWriterSettings other)
        {
            ExceptionUtils.CheckArgumentNotNull(other, "other");

            this.acceptCharSets              = other.acceptCharSets;
            this.acceptMediaTypes            = other.acceptMediaTypes;
            this.BaseUri                     = other.BaseUri;
            this.EnableMessageStreamDisposal = other.EnableMessageStreamDisposal;
            this.EnableCharactersCheck       = other.EnableCharactersCheck;
            this.format                  = other.format;
            this.JsonPCallback           = other.JsonPCallback;
            this.messageQuotas           = new ODataMessageQuotas(other.MessageQuotas);
            this.ODataUri                = other.ODataUri.Clone();
            this.shouldIncludeAnnotation = other.shouldIncludeAnnotation;
            this.useFormat               = other.useFormat;
            this.Version                 = other.Version;
            this.IgnoreNullValues        = other.IgnoreNullValues;

            this.validations = other.validations;
            this.ThrowIfTypeConflictsWithMetadata        = other.ThrowIfTypeConflictsWithMetadata;
            this.ThrowOnDuplicatePropertyNames           = other.ThrowOnDuplicatePropertyNames;
            this.ThrowOnUndeclaredPropertyForNonOpenType = other.ThrowOnUndeclaredPropertyForNonOpenType;
        }
예제 #12
0
        /// <summary>
        /// Validates a element in service document.
        /// </summary>
        /// <param name="serviceDocumentElement">The element in service document to validate.</param>
        /// <param name="format">Format that is being validated.</param>
        internal static void ValidateServiceDocumentElement(ODataServiceDocumentElement serviceDocumentElement, ODataFormat format)
        {
            if (serviceDocumentElement == null)
            {
                throw new ODataException(Strings.ValidationUtils_WorkspaceResourceMustNotContainNullItem);
            }

            // The resource collection URL must not be null;
            if (serviceDocumentElement.Url == null)
            {
                throw new ODataException(Strings.ValidationUtils_ResourceMustSpecifyUrl);
            }

            if (format == ODataFormat.Json && string.IsNullOrEmpty(serviceDocumentElement.Name))
            {
                throw new ODataException(Strings.ValidationUtils_ResourceMustSpecifyName(serviceDocumentElement.Url.ToString()));
            }
        }
예제 #13
0
 /// <summary>
 /// Validates that the uri's reference of $requestId, if used, is one of the depends-on requests.
 /// The Uri can be either absolute or relative.
 /// Exception is thrown if the request Id reference is not found in the list of depends-on requests.
 /// </summary>
 /// <param name="uri">The Uri to validate the request Id reference.</param>
 /// <param name="dependsOnRequestIds">Enumeration of request Ids used to lookup the request Id reference.</param>
 /// <param name="baseUri">The base Uri of the service.</param>
 /// <param name="batchFormat">Format of the batch.</param>
 internal static void ValidateReferenceUri(Uri uri, IEnumerable <string> dependsOnRequestIds, Uri baseUri, ODataFormat batchFormat)
 {
     // To be backward compatible with multipart batch creation, when batch is created with V4 API, the dependsOnId is
     // default as null and therefore it should not enforce any Uri reference check.
     if (batchFormat == ODataFormat.Batch && dependsOnRequestIds == null)
     {
         return;
     }
     else
     {
         ValidateReferenceUri(uri, dependsOnRequestIds, baseUri);
     }
 }