/// <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 ProcessorMatch result that determines the priority of the processor</returns> public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) { if (model is FeatureCollection) { // the model is a feature collection, only then can this GeoJson processor be used. if (IsExactJsonContentType(requestedMediaRange)) { return new ProcessorMatch { ModelResult = MatchResult.ExactMatch, RequestedContentTypeResult = MatchResult.ExactMatch }; } if (IsWildcardJsonContentType(requestedMediaRange)) { return new ProcessorMatch { ModelResult = MatchResult.ExactMatch, RequestedContentTypeResult = MatchResult.NonExactMatch }; } } return new ProcessorMatch { ModelResult = MatchResult.DontCare, RequestedContentTypeResult = MatchResult.NoMatch }; }
/// <summary> /// Deserialize the request body to a model /// </summary> /// <param name="mediaRange">Content type to deserialize</param> /// <param name="bodyStream">Request body stream</param> /// <param name="context">Current context</param> /// <returns>Model instance</returns> public object Deserialize(MediaRange mediaRange, Stream bodyStream, BindingContext context) { var serializer = new JavaScriptSerializer( null, false, this.configuration.MaxJsonLength, this.configuration.MaxRecursions, this.configuration.RetainCasing, this.configuration.UseISO8601DateFormat, this.configuration.Converters, this.configuration.PrimitiveConverters); serializer.RegisterConverters(this.configuration.Converters, this.configuration.PrimitiveConverters); bodyStream.Position = 0; string bodyText; using (var bodyReader = new StreamReader(bodyStream)) { bodyText = bodyReader.ReadToEnd(); } var genericDeserializeMethod = this.deserializeMethod.MakeGenericMethod(context.DestinationType); var deserializedObject = genericDeserializeMethod.Invoke(serializer, new object[] { bodyText }); return deserializedObject; }
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { return new JsonResponse(BuildHypermedia(model, context), serializer) { ContentType = "application/hal+json" }; }
/// <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 ProcessorMatch result that determines the priority of the processor</returns> public override ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) { if (model as PomonaResponse == null) return new ProcessorMatch { ModelResult = MatchResult.NoMatch, RequestedContentTypeResult = MatchResult.DontCare }; //if (IsTextHtmlContentType(requestedMediaRange)) // return new ProcessorMatch // { // ModelResult = MatchResult.ExactMatch, // RequestedContentTypeResult = MatchResult.ExactMatch // }; if (IsExactCsvContentType(requestedMediaRange)) { return new ProcessorMatch { ModelResult = MatchResult.ExactMatch, RequestedContentTypeResult = MatchResult.ExactMatch }; } return new ProcessorMatch { ModelResult = MatchResult.ExactMatch, RequestedContentTypeResult = MatchResult.NoMatch }; }
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { var format = SparqlResultsFormat.AllFormats.FirstOrDefault(f => f.MediaTypes.Any(m => requestedMediaRange.Matches(m))); var queryModel = model as SparqlQueryProcessingModel; return new SparqlQueryResponse(queryModel, context.Request.Headers.IfModifiedSince, format); }
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()); }
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 (IsSomeXmlType(requestedMediaRange) || IsTextHhtml(requestedMediaRange)) return new ProcessorMatch {RequestedContentTypeResult = MatchResult.ExactMatch, ModelResult = MatchResult.DontCare}; else return new ProcessorMatch {ModelResult = MatchResult.NoMatch, RequestedContentTypeResult = MatchResult.NoMatch}; }
/// <summary> /// Deserialize the request body to a model /// </summary> /// <param name="mediaRange">Content type to deserialize</param> /// <param name="bodyStream">Request body stream</param> /// <param name="context">Current context</param> /// <returns>Model instance</returns> public object Deserialize(MediaRange mediaRange, Stream bodyStream, BindingContext context) { if (bodyStream.CanSeek) { bodyStream.Position = 0; } var deserializedObject = this.serializer.Deserialize(new StreamReader(bodyStream), context.DestinationType); var properties = context.DestinationType.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Select(p => new BindingMemberInfo(p)); var fields = context.DestinationType.GetFields(BindingFlags.Public | BindingFlags.Instance) .Select(f => new BindingMemberInfo(f)); if (properties.Concat(fields).Except(context.ValidModelBindingMembers).Any()) { return CreateObjectWithBlacklistExcluded(context, deserializedObject); } return deserializedObject; }
private static string GetErrorMessage(IEnumerable<ISerializer> matches, MediaRange mediaRange) { var details = string.Join("\n", matches.Select(x => string.Concat(" - ", x.GetType().FullName))); return string.Format("Multiple ISerializer implementations matched the '{0}' media range.\nThe following serializers matched \n\n{1}", mediaRange, details); }
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) { if (IsExactSirenContentType(requestedMediaRange)) { return new ProcessorMatch { ModelResult = MatchResult.DontCare, RequestedContentTypeResult = MatchResult.ExactMatch }; } if (IsWildcardSirenContentType(requestedMediaRange)) { return new ProcessorMatch { ModelResult = MatchResult.DontCare, RequestedContentTypeResult = MatchResult.NonExactMatch }; } 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) { return new ProcessorMatch { ModelResult = (model is Response) ? MatchResult.ExactMatch : MatchResult.NoMatch, RequestedContentTypeResult = MatchResult.DontCare }; }
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) { return new ProcessorMatch { ModelResult = MatchResult.DontCare, RequestedContentTypeResult = MatchResult.DontCare }; }
public void Should_strip_whitespace_when_calling_tostring() { // Given var range = new MediaRange("application/vnd.nancy ; a=1; b=2"); // Then range.ToString().ShouldEqual("application/vnd.nancy;a=1;b=2"); }
public void Should_include_parameters_when_calling_tostring() { // Given var range = new MediaRange("application/vnd.nancy;a=1;b=2"); // Then range.ToString().ShouldEqual("application/vnd.nancy;a=1;b=2"); }
public void Should_handle_no_parameters_when_calling_tostring() { // Given var range = new MediaRange("application/vnd.nancy"); // Then range.ToString().ShouldEqual("application/vnd.nancy"); }
/// <summary> /// Process the response. /// </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="Response"/> instance.</returns> public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { var viewResponse = this.viewFactory.RenderView(context.NegotiationContext.ViewName, model, GetViewLocationContext(context)); return this.traceConfiguration.DisplayErrorTraces ? new MaterialisingResponse(viewResponse) : viewResponse; }
/// <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")); }
/// <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(); }
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { return new Response { ContentType = "application/json", Contents = stream => this.serializer.Serialize("application/json", model, stream), StatusCode = HttpStatusCode.OK }.WithHeader("Link", "</context.jsonld>; rel=\"http://www.w3.org/ns/json-ld#context\"; type=\"application/ld+json\""); }
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) { if (IsWildCardOrApplicationType(requestedMediaRange.Type) && IsWildCardOrXmlSubType(requestedMediaRange.Subtype)) return new ProcessorMatch {RequestedContentTypeResult = MatchResult.ExactMatch, ModelResult = MatchResult.DontCare}; else if (IsTextHhtml(requestedMediaRange)) return new ProcessorMatch { RequestedContentTypeResult = MatchResult.NonExactMatch, ModelResult = MatchResult.DontCare }; else return new ProcessorMatch {ModelResult = MatchResult.NoMatch, RequestedContentTypeResult = MatchResult.NoMatch}; }
private static bool IsExactJsonContentType(MediaRange requestedContentType) { if (requestedContentType.Type.IsWildcard && requestedContentType.Subtype.IsWildcard) { return true; } return requestedContentType.Matches("application/json") || requestedContentType.Matches("text/json"); }
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) { if (requestedMediaRange.Matches("application/json")) { return new ProcessorMatch { ModelResult = MatchResult.NoMatch, RequestedContentTypeResult = MatchResult.NoMatch }; } return new ProcessorMatch { ModelResult = MatchResult.ExactMatch, RequestedContentTypeResult = MatchResult.ExactMatch }; }
private static bool IsExactPdfContentType(MediaRange requestedContentType) { if (requestedContentType.Type.IsWildcard && requestedContentType.Subtype.IsWildcard) { return true; } return requestedContentType.Matches("application/pdf"); }
private static bool IsExactSirenContentType(MediaRange requestedContentType) { if (requestedContentType.Type.IsWildcard && requestedContentType.Subtype.IsWildcard) { return true; } return requestedContentType.Matches("application/vnd.siren+json"); }
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { if (model is Stream) { return new StreamResponse(() => model, requestedMediaRange); } return new ByteArrayResponse((byte[])model, requestedMediaRange); }
public void Should_match_with_parameters_if_parameters_match_in_any_order() { // Given var range1 = new MediaRange("application/vnd.nancy;a=1;b=2"); var range2 = new MediaRange("application/vnd.nancy;b=2;a=1"); // Then range1.MatchesWithParameters(range2).ShouldBeTrue(); }
public void Should_not_match_with_parameters_if_parameters_do_not_match() { // Given var range1 = new MediaRange("application/vnd.nancy;a=1;b=2"); var range2 = new MediaRange("application/vnd.nancy;a=1;b=2;c=3"); // Then range1.MatchesWithParameters(range2).ShouldBeFalse(); }
/// <summary> /// Deserialize the request body to a model /// </summary> /// <param name="mediaRange">Content type to deserialize</param> /// <param name="bodyStream">Request body stream</param> /// <param name="context">Current <see cref="BindingContext"/>.</param> /// <returns>Model instance</returns> public object Deserialize(MediaRange mediaRange, Stream bodyStream, BindingContext context) { if (bodyStream.CanSeek) { bodyStream.Position = 0; } var ser = new XmlSerializer(context.DestinationType); return ser.Deserialize(bodyStream); }
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 }; }
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) { if (!context.Request.Url.Path.Contains("jmmserverimage2", StringComparison.OrdinalIgnoreCase)) return new ProcessorMatch { ModelResult = MatchResult.NoMatch, RequestedContentTypeResult = MatchResult.NoMatch }; var acceptableType = (model != null && (model.GetType() == typeof(byte[]) || model is Stream)); var modelResult = acceptableType ? MatchResult.ExactMatch : MatchResult.NoMatch; var contentTypeResult = Mappings.Any(map => map.Item2.Matches(requestedMediaRange)) ? MatchResult.ExactMatch : MatchResult.NoMatch; return new ProcessorMatch { ModelResult = modelResult, RequestedContentTypeResult = contentTypeResult }; }
/// <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()); }
private static bool IsWildcardJsonContentType(MediaRange requestedContentType) { if (!requestedContentType.Type.IsWildcard && !string.Equals("application", requestedContentType.Type, StringComparison.OrdinalIgnoreCase)) { return(false); } if (requestedContentType.Subtype.IsWildcard) { return(true); } var subtypeString = requestedContentType.Subtype.ToString(); return(subtypeString.EndsWith("+json", StringComparison.OrdinalIgnoreCase)); }
private static bool IsWildcardXmlContentType(MediaRange requestedContentType) { if (!requestedContentType.Type.IsWildcard && !string.Equals("application", requestedContentType.Type, StringComparison.InvariantCultureIgnoreCase)) { return(false); } if (requestedContentType.Subtype.IsWildcard) { return(true); } var subtypeString = requestedContentType.Subtype.ToString(); return(subtypeString.StartsWith("vnd", StringComparison.InvariantCultureIgnoreCase) && subtypeString.EndsWith("+xml", StringComparison.InvariantCultureIgnoreCase)); }
/// <summary> /// Whether or not a media range matches another, taking into account wildcards and parameters /// </summary> /// <param name="other">Other media range</param> /// <returns>True if matching, false if not</returns> public bool MatchesWithParameters(MediaRange other) { return(this.Matches(other) && this.Parameters.Matches(other.Parameters)); }
/// <summary> /// Process the response. /// </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="Response"/> instance.</returns> public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { return(this.viewFactory.RenderView(context.NegotiationContext.ViewName, model, GetViewLocationContext(context))); }
/// <summary> /// Process the response /// </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 response</returns> public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { return(new JsonResponse(model, this.serializer, this.environment)); }
/// <summary> /// Process the response. /// </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="Response"/> instance.</returns> public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { var viewResponse = this.viewFactory.RenderView(context.NegotiationContext.ViewName, model, GetViewLocationContext(context)); return(StaticConfiguration.DisableErrorTraces ? viewResponse : new MaterialisingResponse(viewResponse)); }
/// <summary> /// Process the response. /// </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="Response"/> instance.</returns> public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { return(CreateResponse(model, serializer)); }
/// <summary> /// Process the response. /// </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="Response"/> instance.</returns> public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) { return((Response)model); }
/// <summary> /// Whether or not a media range matches another, taking into account wildcards /// </summary> /// <param name="other">Other media range</param> /// <returns>True if matching, false if not</returns> public bool Matches(MediaRange other) { return(this.Type.Matches(other.Type) && this.Subtype.Matches(other.Subtype)); }