internal Uri ProcessUriFromPayload(string uriFromPayload, Uri xmlBaseUri, bool makeAbsolute) { Uri baseUri = xmlBaseUri; if (baseUri == null) { baseUri = base.MessageReaderSettings.BaseUri; bool flag1 = baseUri != null; } Uri payloadUri = new Uri(uriFromPayload, UriKind.RelativeOrAbsolute); Uri uri3 = base.ResolveUri(baseUri, payloadUri); if (uri3 != null) { return(uri3); } if (payloadUri.IsAbsoluteUri || !makeAbsolute) { return(payloadUri); } if (baseUri == null) { throw new ODataException(Strings.ODataAtomDeserializer_RelativeUriUsedWithoutBaseUriSpecified(uriFromPayload)); } return(UriUtils.UriToAbsoluteUri(baseUri, payloadUri)); }
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)); }
/// <summary> /// Given a URI from the payload, this method will try to make it absolute, or fail otherwise. /// </summary> /// <param name="uriFromPayload">The URI string from the payload to process.</param> /// <returns>An absolute URI to report.</returns> internal Uri ProcessUriFromPayload(string uriFromPayload) { Debug.Assert(uriFromPayload != null, "uriFromPayload != null"); Uri uri = new Uri(uriFromPayload, UriKind.RelativeOrAbsolute); // Try to resolve the URI using a custom URL resolver first. Uri metadataDocumentUri = this.MetadataDocumentUri; Uri resolvedUri = this.JsonLightInputContext.ResolveUri(metadataDocumentUri, uri); if (resolvedUri != null) { return(resolvedUri); } if (!uri.IsAbsoluteUri) { if (metadataDocumentUri == null) { throw new ODataException(Strings.ODataJsonLightDeserializer_RelativeUriUsedWithouODataMetadataAnnotation(uriFromPayload, ODataAnnotationNames.ODataContext)); } uri = UriUtils.UriToAbsoluteUri(metadataDocumentUri, uri); } Debug.Assert(uri.IsAbsoluteUri, "By now we should have an absolute URI."); return(uri); }
/// <summary> /// Given a URI from the payload, this method will try to make it absolute, or fail otherwise. /// </summary> /// <param name="uriFromPayload">The URI string from the payload to process.</param> /// <param name="requireAbsoluteUri">true if the payload URI needs to be translated into an absolute URI; otherwise false.</param> /// <returns>An absolute URI to report.</returns> internal Uri ProcessUriFromPayload(string uriFromPayload, bool requireAbsoluteUri) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(uriFromPayload != null, "uriFromPayload != null"); Uri uri = new Uri(uriFromPayload, UriKind.RelativeOrAbsolute); // Try to resolve the URI using a custom URL resolver first. Uri resolvedUri = this.VerboseJsonInputContext.ResolveUri(this.MessageReaderSettings.BaseUri, uri); if (resolvedUri != null) { return(resolvedUri); } if (!uri.IsAbsoluteUri) { if (this.MessageReaderSettings.BaseUri != null) { // Try to use the base URI from the settings. Debug.Assert(this.MessageReaderSettings.BaseUri.IsAbsoluteUri, "The BaseUri on settings should have been verified to be absolute by now."); uri = UriUtils.UriToAbsoluteUri(this.MessageReaderSettings.BaseUri, uri); } else if (requireAbsoluteUri) { // Otherwise fail throw new ODataException(Strings.ODataJsonDeserializer_RelativeUriUsedWithoutBaseUriSpecified(uriFromPayload)); } } Debug.Assert(uri.IsAbsoluteUri || !requireAbsoluteUri, "By now we should have absolute URI if we require one."); return(uri); }
/// <summary> /// Given a string representation of a URI from the payload, this method will return an absolute or relative URI. /// </summary> /// <param name="uriFromPayload">The URI string from the payload to process.</param> /// <param name="xmlBaseUri">The (optional) Xml base URI as specified in the payload.</param> /// <param name="makeAbsolute">If true, then this method will try to make the URI absolute, or fail otherwise.</param> /// <returns>An absolute or relative URI to report based on the value of the <paramref name="makeAbsolute"/> parameter.</returns> internal Uri ProcessUriFromPayload(string uriFromPayload, Uri xmlBaseUri, bool makeAbsolute) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(uriFromPayload != null, "uriFromPayload != null"); // Figure out what base URI to use (if any) Uri baseUri = xmlBaseUri; if (baseUri != null) { Debug.Assert(baseUri.IsAbsoluteUri, "Base URI from the Xml reader should always be absolute."); } else { baseUri = this.MessageReaderSettings.BaseUri; if (baseUri != null) { Debug.Assert(this.MessageReaderSettings.BaseUri.IsAbsoluteUri, "The BaseUri on settings should have been verified to be absolute by now."); } } Uri uri = new Uri(uriFromPayload, UriKind.RelativeOrAbsolute); // Try to resolve the URI using a custom URL resolver first. Uri resolvedUri = this.AtomInputContext.ResolveUri(baseUri, uri); if (resolvedUri != null) { return(resolvedUri); } if (!uri.IsAbsoluteUri && makeAbsolute) { // Try to apply the base Uri if it's available if (baseUri != null) { uri = UriUtils.UriToAbsoluteUri(baseUri, uri); } else { // Otherwise fail throw new ODataException(Strings.ODataAtomDeserializer_RelativeUriUsedWithoutBaseUriSpecified(uriFromPayload)); } } Debug.Assert(uri.IsAbsoluteUri || !makeAbsolute, "If makeAbsolute was true, by now we should have absolute URI."); return(uri); }
public override bool Read() { if (!this.disableXmlBase && (this.xmlBaseStack.Count > 0)) { XmlNodeType nodeType = this.NodeType; if (nodeType == XmlNodeType.Attribute) { this.MoveToElement(); nodeType = XmlNodeType.Element; } if ((this.xmlBaseStack.Peek().Depth == this.Depth) && ((nodeType == XmlNodeType.EndElement) || ((nodeType == XmlNodeType.Element) && this.IsEmptyElement))) { this.xmlBaseStack.Pop(); } } bool flag = this.ReadInternal(this.disableInStreamErrorDetection); if ((flag && !this.disableXmlBase) && (this.NodeType == XmlNodeType.Element)) { string attributeWithAtomizedName = this.GetAttributeWithAtomizedName(this.XmlBaseAttributeName, this.XmlNamespace); if (attributeWithAtomizedName == null) { return(flag); } Uri relativeUri = new Uri(attributeWithAtomizedName, UriKind.RelativeOrAbsolute); if (!relativeUri.IsAbsoluteUri) { if (this.xmlBaseStack.Count == 0) { if (this.documentBaseUri == null) { throw new ODataException(Microsoft.Data.OData.Strings.ODataAtomDeserializer_RelativeUriUsedWithoutBaseUriSpecified(attributeWithAtomizedName)); } relativeUri = UriUtils.UriToAbsoluteUri(this.documentBaseUri, relativeUri); } else { relativeUri = UriUtils.UriToAbsoluteUri(this.xmlBaseStack.Peek().BaseUri, relativeUri); } } this.xmlBaseStack.Push(new XmlBaseDefinition(relativeUri, this.Depth)); } return(flag); }
/// <summary> /// Build an ODataUriParser /// </summary> /// <param name="model">Model to use for metadata binding.</param> /// <param name="serviceRoot">Absolute URI of the service root.</param> /// <param name="uri">Absolute or relative URI to be parsed.</param> /// <param name="container">The optional dependency injection container to get related services for URI parsing.</param> public ODataUriParser(IEdmModel model, Uri serviceRoot, Uri uri, IServiceProvider container) { ExceptionUtils.CheckArgumentNotNull(uri, "uri"); if (serviceRoot == null) { throw new ODataException(ODataErrorStrings.UriParser_NeedServiceRootForThisOverload); } if (!serviceRoot.IsAbsoluteUri) { throw new ODataException(ODataErrorStrings.UriParser_UriMustBeAbsolute(serviceRoot)); } this.configuration = new ODataUriParserConfiguration(model, container); this.serviceRoot = UriUtils.EnsureTaillingSlash(serviceRoot); this.uri = uri.IsAbsoluteUri ? uri : UriUtils.UriToAbsoluteUri(this.ServiceRoot, uri); this.queryOptions = QueryOptionUtils.ParseQueryOptions(this.uri); }
/// <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)); }
/// <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)); }
internal Uri ProcessUriFromPayload(string uriFromPayload, bool requireAbsoluteUri) { Uri payloadUri = new Uri(uriFromPayload, UriKind.RelativeOrAbsolute); Uri uri2 = base.ResolveUri(base.MessageReaderSettings.BaseUri, payloadUri); if (uri2 != null) { return(uri2); } if (!payloadUri.IsAbsoluteUri) { if (base.MessageReaderSettings.BaseUri != null) { return(UriUtils.UriToAbsoluteUri(base.MessageReaderSettings.BaseUri, payloadUri)); } if (requireAbsoluteUri) { throw new ODataException(Strings.ODataJsonDeserializer_RelativeUriUsedWithoutBaseUriSpecified(uriFromPayload)); } } return(payloadUri); }
/// <summary> /// Converts the specified URI into an absolute URI. /// </summary> /// <param name="uri">The uri to process.</param> /// <param name="baseUri">The base Uri to use.</param> /// <returns>An absolute URI which is either the specified <paramref name="uri"/> if it was absolute, /// or it's a combination of the <paramref name="baseUri"/> and the relative <paramref name="uri"/>. /// The return value is the string representation of the URI.</returns> /// <remarks>This method will fail if the specified <paramref name="uri"/> is relative and there's no base URI available.</remarks> internal static string UriToAbsoluteUriString(Uri uri, Uri baseUri) { DebugUtils.CheckNoExternalCallers(); Debug.Assert(uri != null, "uri != null"); Uri resultUri; if (uri.IsAbsoluteUri) { resultUri = uri; } else { if (baseUri == null) { throw new ODataException(Strings.ODataWriter_RelativeUriUsedWithoutBaseUriSpecified(UriUtils.UriToString(uri))); } resultUri = UriUtils.UriToAbsoluteUri(baseUri, uri); } return(UriUtils.UriToString(resultUri)); }