void LogCodecSelected(IType responseEntityType, CodecRegistration negotiatedCodec, int codecsCount) { Log.WriteInfo( "Selected codec {0} out of {1} codecs for entity of type {2} and negotiated media type {3}.". With(negotiatedCodec.CodecType.Name, codecsCount, responseEntityType.Name, negotiatedCodec.MediaType)); }
private void LogCodecSelected(IType responseEntityType, CodecRegistration negotiatedCodec, int codecsCount) { this.Log.WriteInfo( "Selected codec {0} out of {1} codecs for entity of type {2} and negotiated media type {3}.".With( negotiatedCodec.CodecType.Name, codecsCount, responseEntityType.Name, negotiatedCodec.MediaType)); }
protected void WhenFindingCodec(string contentType, params Type[] resourcetypes) { ThenTheResult = null; ThenTheResultScoring = null; ThenTheResultScoring = Codecs.FindMediaTypeReader(new MediaType(contentType), resourcetypes.Select(x => (IMember)TypeSystem.FromClr(x)), null); if (ThenTheResultScoring == null) { return; } ThenTheResult = ThenTheResultScoring.CodecRegistration; }
Uri GetContentLocationFromCodec(Uri resourceUri, CodecRegistration negotiatedCodec) { if (negotiatedCodec.Extensions.Any() == false) { return(resourceUri); } var builder = new UriBuilder(resourceUri); builder.Path += "." + negotiatedCodec.Extensions.First(); return(builder.Uri); }
protected void given_response_entity(object responseEntity, Type codecType, string contentType) { Context.Response.Entity.ContentType = new MediaType(contentType); Context.Environment.ResponseCodec = CodecRegistration.FromResourceType(responseEntity == null ? typeof(object) : responseEntity.GetType(), codecType, TypeSystem, new MediaType(contentType), null, null, false); given_response_entity(responseEntity); }
protected void given_registration_codec <TCodec, TResource>(string mediaTypes) { foreach (var contentType in MediaType.Parse(mediaTypes)) { Codecs.Add(CodecRegistration.FromResourceType(typeof(TResource), typeof(TCodec), TypeSystem, contentType, null, null, false)); } }
protected void GivenACodec <TCodec, TResource>(string mediaTypes, string config) { foreach (var mediaType in MediaType.Parse(mediaTypes)) { Type resourceType = typeof(TResource); Codecs.Add(CodecRegistration.FromResourceType(resourceType, typeof(TCodec), TypeSystems.Default, mediaType, null, config, false)); } }
void GivenAResponseCodec <TCodec>(object config) { if (Context.PipelineData.ResponseCodec != null) { Context.PipelineData.ResponseCodec = null; } Context.PipelineData.ResponseCodec = CodecRegistration.FromResourceType(typeof(object), typeof(TCodec), TypeSystems.Default, new MediaType("application/unknown"), null, config, false); }
protected virtual void RegisterCodecs(IDependencyResolver resolver) { var repo = resolver.Resolve <ICodecRepository>(); var typeSystem = resolver.Resolve <ITypeSystem>(); foreach (Type codecType in CodecTypes) { if (!resolver.HasDependency(codecType)) { resolver.AddDependency(codecType, DependencyLifetime.Transient); } IEnumerable <CodecRegistration> registrations = CodecRegistration.FromCodecType(codecType, typeSystem); registrations.ForEach(repo.Add); } }
public bool Parse(Uri uri, out Uri processedUri) { processedUri = null; var appBaseUri = this.context.ApplicationBaseUri.EnsureHasTrailingSlash(); var fakeBaseUri = new Uri("http://localhost/", UriKind.Absolute); var uriRelativeToAppBase = appBaseUri .MakeRelativeUri(uri) .MakeAbsolute(fakeBaseUri); // find the resource type for the uri string lastUriSegment = uriRelativeToAppBase.GetSegments()[uriRelativeToAppBase.GetSegments().Length - 1]; int lastDot = lastUriSegment.LastIndexOf("."); if (lastDot == -1) { return false; } var uriWithoutExtension = this.ChangePath( uriRelativeToAppBase, srcUri => srcUri.AbsolutePath.Substring(0, srcUri.AbsolutePath.LastIndexOf("."))); this.resourceMatch = this.uris.Match(uriWithoutExtension); if (this.resourceMatch == null) { return false; } string potentialExtension = lastUriSegment.Substring(lastDot + 1); // _codecs. this.selectedCodec = this.codecs.FindByExtension(this.resourceMatch.ResourceKey as IType, potentialExtension); if (this.selectedCodec == null) { return false; } processedUri = fakeBaseUri.MakeRelativeUri(uriWithoutExtension).MakeAbsolute(appBaseUri); // TODO: Ensure that if the Accept: is not compatible with the overriden value a 406 is returned. return true; }
public bool Parse(Uri uri, out Uri processedUri) { processedUri = null; var appBaseUri = _context.ApplicationBaseUri.EnsureHasTrailingSlash(); var fakeBaseUri = new Uri("http://localhost/", UriKind.Absolute); var uriRelativeToAppBase = appBaseUri .MakeRelativeUri(uri) .MakeAbsolute(fakeBaseUri); // find the resource type for the uri string lastUriSegment = uriRelativeToAppBase.Segments[uriRelativeToAppBase.Segments.Length - 1]; int lastDot = lastUriSegment.LastIndexOf("."); if (lastDot == -1) { return(false); } var uriWithoutExtension = ChangePath(uriRelativeToAppBase, srcUri => srcUri.AbsolutePath.Substring(0, srcUri.AbsolutePath.LastIndexOf("."))); _resourceMatch = _uris.Match(uriWithoutExtension); if (_resourceMatch == null) { return(false); } string potentialExtension = lastUriSegment.Substring(lastDot + 1); // _codecs. _selectedCodec = _codecs.FindByExtension(_resourceMatch.ResourceKey as IType, potentialExtension); if (_selectedCodec == null) { return(false); } processedUri = fakeBaseUri.MakeRelativeUri(uriWithoutExtension) .MakeAbsolute(appBaseUri); // TODO: Ensure that if the Accept: is not compatible with the overriden value a 406 is returned. return(true); }
public PipelineContinuation FindResponseCodec(ICommunicationContext context) { if (context.Response.Entity.Instance == null || context.Environment.ResponseCodec != null) { LogNoResponseEntity(); return(PipelineContinuation.Continue); } string acceptHeader = context.Request.Headers[HEADER_ACCEPT]; IEnumerable <MediaType> acceptedContentTypes = MediaType.Parse(string.IsNullOrEmpty(acceptHeader) ? "*/*" : acceptHeader); IType responseEntityType = _typeSystem.FromInstance(context.Response.Entity.Instance); IEnumerable <CodecRegistration> sortedCodecs = _codecs.FindMediaTypeWriter(responseEntityType, acceptedContentTypes); int codecsCount = sortedCodecs.Count(); CodecRegistration negotiatedCodec = sortedCodecs.FirstOrDefault(); if (negotiatedCodec != null) { LogCodecSelected(responseEntityType, negotiatedCodec, codecsCount); context.Response.Entity.ContentType = negotiatedCodec.MediaType.WithoutQuality(); context.Environment.ResponseCodec = negotiatedCodec; context.Response.Headers.Add("Vary", "Content-Type"); try { var resourceUri = context.OperationResult.ResponseResource.CreateUri(); SetAlternateRepresentationLinks(context, sortedCodecs, negotiatedCodec, resourceUri); SetCanonicalLink(context, resourceUri); context.Response.Entity.ContentLocation = GetContentLocationFromCodec(resourceUri, negotiatedCodec); } catch { } } else { context.OperationResult = ResponseEntityHasNoCodec(acceptHeader, responseEntityType); return(PipelineContinuation.RenderNow); } return(PipelineContinuation.Continue); }
static ResourceModel RegisterResourceModel(IFluentTarget has, object resourceKey) { var resourceKeyAsType = resourceKey as Type; bool isStrictRegistration = false; if (resourceKeyAsType != null && CodecRegistration.IsStrictRegistration(resourceKeyAsType)) { resourceKey = CodecRegistration.GetStrictType(resourceKeyAsType); isStrictRegistration = true; } var registration = new ResourceModel { ResourceKey = resourceKey, IsStrictRegistration = isStrictRegistration }; has.Repository.ResourceRegistrations.Add(registration); return(registration); }
void SetAlternateRepresentationLinks(ICommunicationContext context, IEnumerable<CodecRegistration> sortedCodecs, CodecRegistration negotiatedCodec, Uri resourceUri) { context.OperationResult.Links.AddRange( from codec in sortedCodecs where codec != negotiatedCodec select (Link)new AlternateLink( GetContentLocationFromCodec(resourceUri, codec), codec.MediaType.WithoutQuality() )); }
Uri GetContentLocationFromCodec(Uri resourceUri, CodecRegistration negotiatedCodec) { if (negotiatedCodec.Extensions.Any() == false) return resourceUri; var builder = new UriBuilder(resourceUri); builder.Path += "." + negotiatedCodec.Extensions.First(); return builder.Uri; }
void LogCodecSelected(IType responseEntityType, CodecRegistration negotiatedCodec, int codecsCount) { Log.WriteInfo( $"Selected codec {negotiatedCodec.CodecType.Name} out of {codecsCount} codecs for entity of type {responseEntityType.Name} and negotiated media type {negotiatedCodec.MediaType}."); }
void SetAlternateRepresentationLinks(ICommunicationContext context, IEnumerable <CodecRegistration> sortedCodecs, CodecRegistration negotiatedCodec, Uri resourceUri) { context.OperationResult.Links.AddRange( from codec in sortedCodecs where codec != negotiatedCodec select(Link) new AlternateLink( GetContentLocationFromCodec(resourceUri, codec), codec.MediaType.WithoutQuality() )); }
public void Setup() { Codecs = new CodecRepository(); ThenTheResult = null; }
protected void given_registration_codec <TCodec>() { CodecRegistration.FromCodecType(typeof(TCodec), TypeSystem).ForEach(x => Codecs.Add(x)); }