private static RemoteDocument LoadJson(Uri remoteRef, JsonLdLoaderOptions loaderOptions,
                                        JsonLdProcessorOptions options)
 {
     return(options.DocumentLoader != null
         ? options.DocumentLoader(remoteRef, loaderOptions)
         : DefaultDocumentLoader.LoadJson(remoteRef, loaderOptions));
 }
示例#2
0
        public static RemoteDocument LoadJson(Uri remoteRef, JsonLdLoaderOptions loaderOptions)
        {
            var client = new RedirectingWebClient();

            client.Headers.Set(HttpRequestHeader.Accept, "application/ld+json;q=1.0, application/json;q=0.9, */*+json;q=0.8");

            var  responseString     = client.DownloadString(remoteRef);
            var  contentType        = client.ResponseHeaders.GetValues("Content-Type");
            bool matchesContentType =
                contentType != null &&
                contentType.Any(x => x.Contains("application/json") ||
                                (x.Contains("application/ld+json") &&
                                 string.IsNullOrEmpty(loaderOptions.RequestProfile)) ||
                                (x.Contains("application/ld+json") &&
                                 !string.IsNullOrEmpty(loaderOptions.RequestProfile) &&
                                 x.Contains(loaderOptions.RequestProfile)) ||
                                x.Contains("+json"));
            var    documentUrl = client.ResponseUri;
            string contextLink = null;

            if (!matchesContentType)
            {
                var contextLinks  = ParseLinkHeaders(client.ResponseHeaders.GetValues("Link"));
                var alternateLink = contextLinks.FirstOrDefault(link =>
                                                                link.RelationTypes.Contains("alternate") && link.MediaTypes.Contains("application/ld+json"));
                if (alternateLink != null)
                {
                    return(LoadJson(new Uri(remoteRef, alternateLink.LinkValue), loaderOptions));
                }
                else
                {
                    throw new JsonLdProcessorException(JsonLdErrorCode.LoadingDocumentFailed,
                                                       "Loading document failed. The server did not respond with a processable JSON document.");
                }
            }

            // If content type is application/ld+json the context link header is ignored
            if (!contentType.Any(x => x.Contains("application/ld+json")))
            {
                var contextLinks = ParseLinkHeaders(client.ResponseHeaders.GetValues("Link"))
                                   .Where(x => x.RelationTypes.Contains(JsonLdVocabulary.Context))
                                   .Select(x => x.LinkValue).ToList();
                if (contextLinks.Count > 1)
                {
                    throw new JsonLdProcessorException(JsonLdErrorCode.MultipleContextLinkHeaders, "Multiple context link headers");
                }
                contextLink = contextLinks.FirstOrDefault();
            }

            var ret = new RemoteDocument
            {
                ContextUrl  = contextLink == null ? null : new Uri(contextLink),
                DocumentUrl = client.ResponseUri,
                Document    = JToken.Parse(responseString),
            };

            return(ret);
        }