Example #1
0
        /// <summary>
        /// Gets the correct model for the given media range
        /// </summary>
        /// <param name="mediaRange">The <see cref="MediaRange"/> to get the model for.</param>
        /// <returns>The model for the provided <paramref name="mediaRange"/> if it has been mapped, otherwise the <see cref="DefaultModel"/> will be returned.</returns>
        public dynamic GetModelForMediaRange(MediaRange mediaRange)
        {
            var matching = this.MediaRangeModelMappings.Any(m => mediaRange.Matches(m.Key));

            return(matching ?
                   this.MediaRangeModelMappings.First(m => mediaRange.Matches(m.Key)).Value.Invoke() :
                   this.DefaultModel);
        }
        private static bool IsExactJsonContentType(MediaRange requestedContentType)
        {
            if (requestedContentType.Type.IsWildcard && requestedContentType.Subtype.IsWildcard)
            {
                return(true);
            }

            return(requestedContentType.Matches("application/json") || requestedContentType.Matches("text/json"));
        }
Example #3
0
        public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            if (model is SparqlQueryProcessingModel)
            {
                var processingModel = model as SparqlQueryProcessingModel;
                if (processingModel.SparqlRequest.Format != null)
                {
                    var sparqlFormat =
                        processingModel.SparqlRequest.Format.Select(SparqlResultsFormat.GetResultsFormat)
                                       .FirstOrDefault();
                    var graphFormat =
                        processingModel.SparqlRequest.Format.Select(RdfFormat.GetResultsFormat)
                                       .FirstOrDefault();
                    processingModel.OverrideSparqlFormat = sparqlFormat;
                    processingModel.OverrideGraphFormat = graphFormat;
                    if (sparqlFormat != null || graphFormat != null)
                    {
                        return new ProcessorMatch
                            {
                                ModelResult = MatchResult.ExactMatch,
                                RequestedContentTypeResult = MatchResult.ExactMatch
                            };
                    }
                }

                if ((processingModel.ResultModel == SerializableModel.SparqlResultSet) &&
                    (SparqlResultsFormat.AllMediaTypes.Any(m => requestedMediaRange.Matches(MediaRange.FromString(m)))))
                {
                    return new ProcessorMatch
                        {
                            ModelResult = MatchResult.ExactMatch,
                            RequestedContentTypeResult = MatchResult.ExactMatch
                        };
                }
                if ((processingModel.ResultModel == SerializableModel.RdfGraph) &&
                    (RdfFormat.AllMediaTypes.Any(m => requestedMediaRange.Matches(MediaRange.FromString(m)))))
                {
                    return new ProcessorMatch
                        {
                            ModelResult = MatchResult.ExactMatch,
                            RequestedContentTypeResult = MatchResult.ExactMatch
                        };
                }
                return new ProcessorMatch
                    {
                        ModelResult = MatchResult.ExactMatch,
                        RequestedContentTypeResult = MatchResult.NoMatch
                    };
            }
            return new ProcessorMatch
                {
                    ModelResult = MatchResult.NoMatch,
                    RequestedContentTypeResult = MatchResult.DontCare
                };
        }
Example #4
0
 public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context)
 {
     var queryModel = model as SparqlQueryProcessingModel;
     var format = queryModel.OverrideSparqlFormat ??
                  SparqlResultsFormat.AllFormats.FirstOrDefault(
                      f => f.MediaTypes.Any(m => requestedMediaRange.Matches(m)));
     var graphFormat =
         queryModel.OverrideGraphFormat ??
         RdfFormat.AllFormats.FirstOrDefault(f => f.MediaTypes.Any(m => requestedMediaRange.Matches(m)));
     
     return new SparqlQueryResponse(queryModel, context.Request.Headers.IfModifiedSince, format, graphFormat);
 }
 public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
 {
     var graphListModel = model as GraphListModel;
     if (graphListModel != null)
     {
         if (requestedMediaRange.Matches(JsonMediaRange))
         {
             return new ProcessorMatch
             {
                 ModelResult = MatchResult.ExactMatch,
                 RequestedContentTypeResult = MatchResult.ExactMatch
             };
         }
         return new ProcessorMatch
         {
             ModelResult = MatchResult.ExactMatch,
             RequestedContentTypeResult = MatchResult.NoMatch
         };
     }
     return new ProcessorMatch
     {
         ModelResult = MatchResult.NoMatch,
         RequestedContentTypeResult = MatchResult.NoMatch
     };
 }
        public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            if (requestedMediaRange.Matches("application/csv") || requestedMediaRange.Matches("text/csv"))
            {
                return new ProcessorMatch()
                    {
                        ModelResult = MatchResult.DontCare,
                        RequestedContentTypeResult = MatchResult.ExactMatch
                    };
            }

            return new ProcessorMatch
                {
                    ModelResult = MatchResult.DontCare,
                    RequestedContentTypeResult = MatchResult.NoMatch
                };
        }
        private static bool IsExactSirenContentType(MediaRange requestedContentType)
        {
            if (requestedContentType.Type.IsWildcard && requestedContentType.Subtype.IsWildcard)
            {
                return true;
            }

            return requestedContentType.Matches("application/vnd.siren+json");
        }
Example #8
0
        private static bool IsExactPdfContentType(MediaRange requestedContentType)
        {
            if (requestedContentType.Type.IsWildcard && requestedContentType.Subtype.IsWildcard)
            {
                return true;
            }

            return requestedContentType.Matches("application/pdf");
        }
        /// <summary>
        /// Determines whether the the processor can handle a given content type and model.
        /// </summary>
        /// <param name="requestedMediaRange">Content type requested by the client.</param>
        /// <param name="model">The model for the given media range.</param>
        /// <param name="context">The nancy context.</param>
        /// <returns>A <see cref="ProcessorMatch"/> result that determines the priority of the processor.</returns>
        public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            var matchingContentType =
                requestedMediaRange.Matches("text/html");

            return matchingContentType
                ? new ProcessorMatch { ModelResult = MatchResult.DontCare, RequestedContentTypeResult = MatchResult.ExactMatch }
                : new ProcessorMatch();
        }
Example #10
0
        /// <summary>
        /// Determines whether the processor can handle a given content type and model.
        /// </summary>
        /// <param name="requestedMediaRange">Content type requested by the client.</param>
        /// <param name="model">The model for the given media range.</param>
        /// <param name="context">The nancy context.</param>
        /// <returns>A <see cref="ProcessorMatch"/> result that determines the priority of the processor.</returns>
        public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            var matchingContentType =
                requestedMediaRange.Matches("text/html");

            return(matchingContentType
                ? new ProcessorMatch {
                ModelResult = MatchResult.DontCare, RequestedContentTypeResult = MatchResult.ExactMatch
            }
                : new ProcessorMatch());
        }
Example #11
0
        public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            if (requestedMediaRange.Matches(MediaRange.FromString("text/plain")) && model is Quip)
            {
                return new ProcessorMatch
                    {
                        ModelResult = MatchResult.DontCare,
                        RequestedContentTypeResult = MatchResult.ExactMatch
                    };
            }

            return new ProcessorMatch { ModelResult = MatchResult.DontCare, RequestedContentTypeResult = MatchResult.NoMatch };
        }
        /// <summary>
        /// Determines whether the the processor can handle a given content type and model.
        /// </summary>
        /// <param name="requestedMediaRange">Content type requested by the client.</param>
        /// <param name="model">The model for the given media range.</param>
        /// <param name="context">The nancy context.</param>
        /// <returns>A <see cref="ProcessorMatch"/> result that determines the priority of the processor.</returns>
        public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            if (IsWildcardProtobufContentType(requestedMediaRange) || requestedMediaRange.Matches(Constants.ProtoBufContentType))
            {
                return new ProcessorMatch
                {
                    ModelResult = MatchResult.DontCare,
                    RequestedContentTypeResult = MatchResult.ExactMatch
                };
            }

            return new ProcessorMatch
            {
                ModelResult = MatchResult.DontCare,
                RequestedContentTypeResult = MatchResult.NoMatch
            };
        }
Example #13
0
        public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            if (requestedMediaRange.Matches("text/csv") && model is IEnumerable)
            {
                return new ProcessorMatch
                {
                    ModelResult = MatchResult.DontCare,
                    RequestedContentTypeResult = MatchResult.ExactMatch
                };
            }

            return new ProcessorMatch
            {
                ModelResult = MatchResult.DontCare,
                RequestedContentTypeResult = MatchResult.NoMatch
            };
        }
 public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context)
 {
     if (model is SparqlQueryProcessingModel)
     {
         if (SparqlResultsFormat.AllMediaTypes.Any(m => requestedMediaRange.Matches(m)))
         {
             return new ProcessorMatch
                 {
                     ModelResult = MatchResult.ExactMatch,
                     RequestedContentTypeResult = MatchResult.ExactMatch
                 };
         }
         return new ProcessorMatch
             {
                 ModelResult = MatchResult.ExactMatch,
                 RequestedContentTypeResult = MatchResult.NoMatch
             };
     }
     return new ProcessorMatch
         {
             ModelResult = MatchResult.NoMatch,
             RequestedContentTypeResult = MatchResult.DontCare
         };
 }
Example #15
0
        public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context)
        {
            var processingModel = model as SparqlQueryProcessingModel;
            if (processingModel != null)
            {
                var format = (processingModel.OverrideResultsFormat ??
                              SparqlResultsFormat.AllFormats.FirstOrDefault(
                                  f => f.MediaTypes.Any(m => requestedMediaRange.Matches(m)))) ??
                             SparqlResultsFormat.Xml;
                var graphFormat =
                    (processingModel.OverrideGraphFormat ??
                     RdfFormat.AllFormats.FirstOrDefault(f => f.MediaTypes.Any(m => requestedMediaRange.Matches(m)))) ??
                    RdfFormat.RdfXml;

                return new SparqlQueryResponse(processingModel, context.Request.Headers.IfModifiedSince, format, graphFormat);
            }
            var graphList = model as GraphListModel;
            if (graphList != null)
            {
                var format =
                    SparqlResultsFormat.AllFormats.FirstOrDefault(
                        f => f.MediaTypes.Any(m => requestedMediaRange.Matches(m))) ?? SparqlResultsFormat.Xml;
                return new TextResponse(
                    graphList.AsString(format), format.MediaTypes[0]);
            }
            throw new ArgumentException("Unexpected model type: " + model.GetType());
        }
Example #16
0
        private static void TestSparqlPostSucceeds(string storeName, string query, IEnumerable<string> defaultGraphUris, IEnumerable<string> namedGraphUris, MediaRange accept, SparqlResultsFormat expectedQueryFormat, Action<Mock<IBrightstarService>> brightstarSetup)
        {
            // Setup
            var brightstar = new Mock<IBrightstarService>();
            ISerializationFormat format = expectedQueryFormat;
            brightstar.Setup(s => s.ExecuteQuery(storeName, query, defaultGraphUris, null, expectedQueryFormat,  It.IsAny<RdfFormat>(), out format))
                      .Returns(new MemoryStream(Encoding.UTF8.GetBytes("Mock Results")))
                      .Verifiable();
            if (brightstarSetup != null) brightstarSetup(brightstar);
            var app = new Browser(new FakeNancyBootstrapper(brightstar.Object));

            // Execute
            var response = app.Post("/" + storeName + "/sparql", with =>
            {
                with.Body(query);
                with.Header("Content-Type", "application/sparql-query");
                if (defaultGraphUris != null)
                {
                    foreach (var defaultGraphUri in defaultGraphUris)
                    {
                        with.Query("default-graph-uri", defaultGraphUri);
                    }
                }
                if (namedGraphUris != null)
                {
                    foreach (var namedGraphUri in namedGraphUris)
                    {
                        with.Query("named-graph-uri", namedGraphUri);
                    }
                }
                with.Accept(accept);
            });

            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
            Assert.That(accept.Matches(MediaRange.FromString(response.ContentType)));
            Assert.That(response.Body.AsString(), Is.EqualTo("Mock Results"));
            brightstar.Verify();
        }
Example #17
0
        private static BrowserResponse TestGetSucceeds(
            Mock<IBrightstarService> brightstar,
            string storeName, string query, 
            IEnumerable<string> defaultGraphUris = null,
            IEnumerable<string> namedGraphUris = null, 
            IEnumerable<string> formats = null,
            MediaRange accept = null)
        {
            // Setup
            var app = new Browser(new FakeNancyBootstrapper(brightstar.Object));
            if (accept == null) accept = MediaRange.FromString("application/sparql-results+xml");
            // Execute
            var response = app.Get("/" + storeName + "/sparql", with =>
                {
                    with.Query("query", query);
                    if (defaultGraphUris != null)
                    {
                        foreach (var defaultGraph in defaultGraphUris)
                        {
                            with.Query("default-graph-uri", defaultGraph);
                        }
                    }
                    if (namedGraphUris != null)
                    {
                        foreach (var namedGraph in namedGraphUris)
                        {
                            with.Query("named-graph-uri", namedGraph);
                        }
                    }
                    if (formats != null)
                    {
                        foreach (var format in formats)
                        {
                            with.Query("format", format);
                        }
                    }
                    with.Accept(accept);
                });

            Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
            if (formats == null)
            {
                Assert.That(accept.Matches(MediaRange.FromString(response.ContentType)));
            }
            brightstar.Verify();
            return response;
        }
 protected bool IsTextHtmlContentType(MediaRange requestedMediaRange)
 {
     return requestedMediaRange.Matches("text/html");
 }
Example #19
0
        /// <summary>
        /// Gets the correct model for the given media range
        /// </summary>
        /// <param name="mediaRange">The <see cref="MediaRange"/> to get the model for.</param>
        /// <returns>The model for the provided <paramref name="mediaRange"/> if it has been mapped, otherwise the <see cref="DefaultModel"/> will be returned.</returns>
        public dynamic GetModelForMediaRange(MediaRange mediaRange)
        {
            var matching = this.MediaRangeModelMappings.Any(m => mediaRange.Matches(m.Key));

            return matching ?
                this.MediaRangeModelMappings.First(m => mediaRange.Matches(m.Key)).Value.Invoke() :
                this.DefaultModel;
        }
        private static bool IsExactJsonContentType(MediaRange requestedContentType)
        {
            if (requestedContentType.Type.IsWildcard && requestedContentType.Subtype.IsWildcard)
                return true;

            return requestedContentType.Matches("application/json") || requestedContentType.Matches("text/json");
        }
        private static bool IsExactCsvContentType(MediaRange requestedContentType)
        {
            if (requestedContentType.Type.IsWildcard && requestedContentType.Subtype.IsWildcard)
            {
                return true;
            }

            return requestedContentType.Matches("text/plain");
        }