示例#1
0
        internal string UriToUriString(Uri uri, bool makeAbsolute)
        {
            Uri uri2;

            if (base.UrlResolver != null)
            {
                uri2 = base.UrlResolver.ResolveUrl(base.MessageWriterSettings.BaseUri, uri);
                if (uri2 != null)
                {
                    return(UriUtilsCommon.UriToString(uri2));
                }
            }
            uri2 = uri;
            if (!uri2.IsAbsoluteUri)
            {
                if (makeAbsolute)
                {
                    if (base.MessageWriterSettings.BaseUri == null)
                    {
                        throw new ODataException(Strings.ODataWriter_RelativeUriUsedWithoutBaseUriSpecified(UriUtilsCommon.UriToString(uri)));
                    }
                    uri2 = UriUtils.UriToAbsoluteUri(base.MessageWriterSettings.BaseUri, uri);
                }
                else
                {
                    uri2 = UriUtils.EnsureEscapedRelativeUri(uri2);
                }
            }
            return(UriUtilsCommon.UriToString(uri2));
        }
示例#2
0
        /// <summary>
        /// Converts the given <paramref name="uri"/> Uri to a string.
        /// If the provided baseUri is not null and is a base Uri of the <paramref name="uri"/> Uri
        /// the method returns the string form of the relative Uri.
        /// </summary>
        /// <param name="uri">The Uri to convert.</param>
        /// <param name="failOnRelativeUriWithoutBaseUri">If set to true then this method will fail if the uri specified by <paramref name="uri"/> is relative
        /// and no base uri is specified.</param>
        /// <returns>The string form of the <paramref name="uri"/> Uri. If the Uri is absolute it returns the
        /// string form of the <paramref name="uri"/>. If the <paramref name="uri"/> Uri is not absolute
        /// it returns the original string of the Uri.</returns>
        internal string UriToUrlAttributeValue(Uri uri, bool failOnRelativeUriWithoutBaseUri)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(uri != null, "uri != null");

            if (this.UrlResolver != null)
            {
                // The resolver returns 'null' if no custom resolution is desired.
                Uri resultUri = this.UrlResolver.ResolveUrl(this.MessageWriterSettings.BaseUri, uri);
                if (resultUri != null)
                {
                    return(UriUtilsCommon.UriToString(resultUri));
                }
            }

            if (!uri.IsAbsoluteUri)
            {
                // NOTE: the only URIs that are allowed to be relative (e.g., failOnRelativeUriWithoutBaseUri is false)
                //       are metadata URIs in operations; for such metadata URIs there is no base URI.
                if (this.MessageWriterSettings.BaseUri == null && failOnRelativeUriWithoutBaseUri)
                {
                    throw new ODataException(
                              ODataErrorStrings.ODataWriter_RelativeUriUsedWithoutBaseUriSpecified(UriUtilsCommon.UriToString(uri)));
                }

                uri = UriUtils.EnsureEscapedRelativeUri(uri);
            }

            return(UriUtilsCommon.UriToString(uri));
        }
示例#3
0
        /// <summary>
        /// Returns the string representation of the URI
        /// </summary>
        /// <param name="uri">The uri to process.</param>
        /// <returns>Returns the string representation of the URI.</returns>
        internal string UriToString(Uri uri)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(uri != null, "uri != null");

            // Get the metadataDocumentUri directly from MessageWriterSettings and not using MetadataUriBuilder because in the case of getting the service document with nometadata
            // MetadataUriBuilder returns null, but the metadataDocumentUri is needed to calculate Absolute Uris in the service document. In any other case jsonLightOutputContext.CreateMetadataUriBuilder() should be used.
            ODataMetadataDocumentUri odataMetadataDocumentUri = this.jsonLightOutputContext.MessageWriterSettings.MetadataDocumentUri;
            Uri metadataDocumentUri = odataMetadataDocumentUri == null ? null : odataMetadataDocumentUri.BaseUri;

            Uri resultUri;

            if (this.jsonLightOutputContext.UrlResolver != null)
            {
                // The resolver returns 'null' if no custom resolution is desired.
                resultUri = this.jsonLightOutputContext.UrlResolver.ResolveUrl(metadataDocumentUri, uri);
                if (resultUri != null)
                {
                    return(UriUtilsCommon.UriToString(resultUri));
                }
            }

            resultUri = uri;
            if (!resultUri.IsAbsoluteUri)
            {
                if (!this.allowRelativeUri)
                {
                    if (metadataDocumentUri == null)
                    {
                        throw new ODataException(Strings.ODataJsonLightSerializer_RelativeUriUsedWithoutMetadataDocumentUriOrMetadata(UriUtilsCommon.UriToString(resultUri)));
                    }

                    resultUri = UriUtils.UriToAbsoluteUri(metadataDocumentUri, uri);
                }
                else
                {
                    resultUri = UriUtils.EnsureEscapedRelativeUri(resultUri);
                }
            }

            return(UriUtilsCommon.UriToString(resultUri));
        }
示例#4
0
 internal string UriToUrlAttributeValue(Uri uri, bool failOnRelativeUriWithoutBaseUri)
 {
     if (base.UrlResolver != null)
     {
         Uri uri2 = base.UrlResolver.ResolveUrl(base.MessageWriterSettings.BaseUri, uri);
         if (uri2 != null)
         {
             return(UriUtilsCommon.UriToString(uri2));
         }
     }
     if (!uri.IsAbsoluteUri)
     {
         if ((base.MessageWriterSettings.BaseUri == null) && failOnRelativeUriWithoutBaseUri)
         {
             throw new ODataException(Strings.ODataWriter_RelativeUriUsedWithoutBaseUriSpecified(UriUtilsCommon.UriToString(uri)));
         }
         uri = UriUtils.EnsureEscapedRelativeUri(uri);
     }
     return(UriUtilsCommon.UriToString(uri));
 }
        /// <summary>
        /// Returns the string representation of the URI; Converts the URI into an absolute URI if the <paramref name="makeAbsolute"/> parameter is set to true.
        /// </summary>
        /// <param name="uri">The uri to process.</param>
        /// <param name="makeAbsolute">true, if the URI needs to be translated into an absolute URI; false otherwise.</param>
        /// <returns>If the <paramref name="makeAbsolute"/> parameter is set to true, then a string representation of an absolute URI which is either the
        /// specified <paramref name="uri"/> if it was absolute, or it's a combination of the BaseUri and the relative <paramref name="uri"/>;
        /// otherwise a string representation of the specified <paramref name="uri"/>.
        /// </returns>
        /// <remarks>This method will fail if <paramref name="makeAbsolute"/> is set to true and the specified <paramref name="uri"/> is relative and there's no base URI available.</remarks>
        internal string UriToUriString(Uri uri, bool makeAbsolute)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(uri != null, "uri != null");

            Uri resultUri;

            if (this.UrlResolver != null)
            {
                // The resolver returns 'null' if no custom resolution is desired.
                resultUri = this.UrlResolver.ResolveUrl(this.MessageWriterSettings.BaseUri, uri);
                if (resultUri != null)
                {
                    return(UriUtilsCommon.UriToString(resultUri));
                }
            }

            resultUri = uri;

            if (!resultUri.IsAbsoluteUri)
            {
                if (makeAbsolute)
                {
                    if (this.MessageWriterSettings.BaseUri == null)
                    {
                        throw new ODataException(o.Strings.ODataWriter_RelativeUriUsedWithoutBaseUriSpecified(UriUtilsCommon.UriToString(uri)));
                    }

                    resultUri = UriUtils.UriToAbsoluteUri(this.MessageWriterSettings.BaseUri, uri);
                }
                else
                {
                    // NOTE: the only URIs that are allowed to be relative are metadata URIs
                    //       in operations; for such metadata URIs there is no base URI.
                    resultUri = UriUtils.EnsureEscapedRelativeUri(resultUri);
                }
            }

            return(UriUtilsCommon.UriToString(resultUri));
        }