@Override public MediaType getMediaType() { return representationType.getMediaType(mediaTypeParams); }
@Override public RepresentationType valueOf(final String str) { return RepresentationType.lookup(str); }
@SuppressWarnings({ "rawtypes", "unchecked" }) public static RestfulResponse<JsonRepresentation> of(final Response response) { final MediaType jaxRsMediaType = getHeader(response, Header.CONTENT_TYPE); final RepresentationType representationType = RepresentationType.lookup(jaxRsMediaType); final Class<? extends JsonRepresentation> returnType = representationType.getRepresentationClass(); return new RestfulResponse(response, returnType); }
private void ensureCompatibleAcceptHeader(final RepresentationType representationType) { if (representationType == null) { return; } final MediaType producedType = representationType.getMediaType(); final List<MediaType> acceptableMediaTypes = acceptableMediaTypes(); for (final MediaType mediaType : acceptableMediaTypes) { if (compatible(mediaType, representationType)) { return; } } if (!acceptableMediaTypes.contains(producedType)) { throw RestfulObjectsApplicationException.create(HttpStatusCode.NOT_ACCEPTABLE, "Resource produces %s media type", representationType.getMediaTypeWithProfile()); } }
@Test public void roundtrip() { // overloaded for (final RepresentationType repType : RepresentationType.values()) { final MediaType mediaType = repType.getMediaType(); final RepresentationType lookup = RepresentationType.lookup(mediaType); assertSame(repType, lookup); } for (final RepresentationType repType : RepresentationType.values()) { final String name = repType.getName(); final RepresentationType lookup = RepresentationType.lookup(name); assertSame(repType, lookup); } }
@Test public void whenDomainObjectWithXRoParameter() { MediaType toLookup = RepresentationType.DOMAIN_OBJECT.getMediaType("x-ro-domain-type", "http://mycompany.com:39393/domain-types/JdkValuedEntities"); // ignores the parameter. assertThat( RepresentationType.lookup(toLookup), is(RepresentationType.DOMAIN_OBJECT)); }
public static RepresentationType lookup(final MediaType mediaType) { if(mediaType != null) { for (final RepresentationType representationType : values()) { final MediaType candidate = representationType.getMediaType(); if(!candidate.getType().equals(mediaType.getType())) { continue; } if(!candidate.getSubtype().equals(mediaType.getSubtype())) { continue; } String candidateProfile = candidate.getParameters().get("profile"); String mediaTypeProfile = mediaType.getParameters().get("profile"); if(candidateProfile == null || candidateProfile.equals(mediaTypeProfile)) { return representationType; } } } return RepresentationType.GENERIC; }
private boolean isAccepted( final RepresentationType representationType, final List<MediaType> acceptableMediaTypes, final boolean strictAcceptChecking) { if(!strictAcceptChecking) { return true; } final String producedProfile = representationType.getMediaTypeProfile(); if (producedProfile == null) { throw new IllegalArgumentException("RepresentationType " + representationType + " does not specify a 'profile' parameter"); } return isAccepted(producedProfile, acceptableMediaTypes); }
@Test public void roundtrips() { final Parser<RepresentationType> parser = RepresentationType.parser(); for (final RepresentationType repType : RepresentationType.values()) { final String asString = parser.asString(repType); final RepresentationType roundtripped = parser.valueOf(asString); assertSame(roundtripped, repType); } }
@Override public String asString(final RepresentationType t) { return t.getName(); } };
@Test public void roundtrip() { // overloaded for (final RepresentationType repType : RepresentationType.values()) { final MediaType mediaType = repType.getMediaType(); final RepresentationType lookup = RepresentationType.lookup(mediaType); assertSame(repType, lookup); } for (final RepresentationType repType : RepresentationType.values()) { final String name = repType.getName(); final RepresentationType lookup = RepresentationType.lookup(name); assertSame(repType, lookup); } }
@Test public void whenDomainObjectWithXRoParameter() { MediaType toLookup = RepresentationType.DOMAIN_OBJECT.getMediaType("x-ro-domain-type", "http://mycompany.com:39393/domain-types/JdkValuedEntities"); // ignores the parameter. assertThat( RepresentationType.lookup(toLookup), is(RepresentationType.DOMAIN_OBJECT)); }
private void ensureCompatibleAcceptHeader(final RepresentationType representationType) { if (representationType == null) { return; } // RestEasy will check the basic media types... // ... so we just need to check the profile paramter final String producedProfile = representationType.getMediaTypeProfile(); if(producedProfile != null) { for (MediaType mediaType : httpHeaders.getAcceptableMediaTypes()) { String acceptedProfileValue = mediaType.getParameters().get("profile"); if(acceptedProfileValue == null) { continue; } if(!producedProfile.equals(acceptedProfileValue)) { throw RestfulObjectsApplicationException.create(HttpStatusCode.NOT_ACCEPTABLE); } } } }
@Test public void roundtrips() { final Parser<RepresentationType> parser = RepresentationType.parser(); for (final RepresentationType repType : RepresentationType.values()) { final String asString = parser.asString(repType); final RepresentationType roundtripped = parser.valueOf(asString); assertSame(roundtripped, repType); } }
@Override public MediaType getMediaType() { return representationType.getMediaType(mediaTypeParams); }
private void ensureCompatibleAcceptHeader( final RepresentationType representationType, final List<MediaType> acceptableMediaTypes) { if(!strictAcceptChecking) { return; } if (representationType == null) { return; } // RestEasy will check the basic media types... // ... so we just need to check the profile paramter final String producedProfile = representationType.getMediaTypeProfile(); if (producedProfile == null) { return; } boolean accepted = isAccepted(producedProfile, acceptableMediaTypes); if(!accepted) { throw RestfulObjectsApplicationException.create(RestfulResponse.HttpStatusCode.NOT_ACCEPTABLE); } }