@Test public void testGetNormalisedRequestMediaType() { when(request.getMethod()).thenReturn("POST"); when(request.getContentType()).thenReturn("application/xml"); MediaType mediaType = ctn.getNormalisedRequestMediaType(request); assertNotNull(mediaType); assertEquals(MediaType.APPLICATION_XML_TYPE, mediaType); }
@Override public MediaType getNormalisedRequestMediaType(HttpServletRequest request) { String contentType = request.getContentType(); if (request.getMethod().equals("POST")) { if (contentType == null) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type was not specified for deserialisable response"); } MediaType requestMT; try { requestMT = MediaType.valueOf(contentType); } catch (Exception e) { throw new CougarValidationException(ServerFaultCode.MediaTypeParseFailure, "Input content type cannot be parsed: " + contentType,e); } if (requestMT.isWildcardType() || requestMT.isWildcardSubtype()) { throw new CougarValidationException(ServerFaultCode.InvalidInputMediaType, "Input content type may not be wildcard: " + requestMT); } if (!MediaTypeUtils.isValid(allContentTypes, requestMT)) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type is not valid: " + requestMT); } String candidateContentType = requestMT.getType() + "/" + requestMT.getSubtype(); MediaType normalizedMediaType = validContentTypes.get(candidateContentType); if (normalizedMediaType == null) { throw new CougarValidationException(ServerFaultCode.FrameworkError, "Input content type " + contentType + " failed to find a normalized type using key " + candidateContentType); } return normalizedMediaType; } return null; }
ContentTypeNormaliserImpl implements ContentTypeNormaliser { @Override public MediaType getNormalisedRequestMediaType(HttpServletRequest request) { String contentType = request.getContentType(); if (request.getMethod().equals("POST")) { if (contentType == null) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type was not specified for deserialisable response"); } MediaType requestMT; try { requestMT = MediaType.valueOf(contentType); } catch (Exception e) { throw new CougarValidationException(ServerFaultCode.MediaTypeParseFailure, "Input content type cannot be parsed: " + contentType,e); } if (requestMT.isWildcardType() || requestMT.isWildcardSubtype()) { throw new CougarValidationException(ServerFaultCode.InvalidInputMediaType, "Input content type may not be wildcard: " + requestMT); } if (!MediaTypeUtils.isValid(allContentTypes, requestMT)) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type is not valid: " + requestMT); } String candidateContentType = requestMT.getType() + "/" + requestMT.getSubtype(); MediaType normalizedMediaType = validContentTypes.get(candidateContentType); if (normalizedMediaType == null) { throw new CougarValidationException(ServerFaultCode.FrameworkError, "Input content type " + contentType + " failed to find a normalized type using key " + candidateContentType); } return normalizedMediaType; } return null; } }
ContentTypeNormaliserImpl implements ContentTypeNormaliser { @Override public MediaType getNormalisedRequestMediaType(HttpServletRequest request) { String contentType = request.getContentType(); if (request.getMethod().equals("POST")) { if (contentType == null) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type was not specified for deserialisable response"); } MediaType requestMT; try { requestMT = MediaType.valueOf(contentType); } catch (Exception e) { throw new CougarValidationException(ServerFaultCode.MediaTypeParseFailure, "Input content type cannot be parsed: " + contentType,e); } if (requestMT.isWildcardType() || requestMT.isWildcardSubtype()) { throw new CougarValidationException(ServerFaultCode.InvalidInputMediaType, "Input content type may not be wildcard: " + requestMT); } if (!MediaTypeUtils.isValid(allContentTypes, requestMT)) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type is not valid: " + requestMT); } String candidateContentType = requestMT.getType() + "/" + requestMT.getSubtype(); MediaType normalizedMediaType = validContentTypes.get(candidateContentType); if (normalizedMediaType == null) { throw new CougarValidationException(ServerFaultCode.FrameworkError, "Input content type " + contentType + " failed to find a normalized type using key " + candidateContentType); } return normalizedMediaType; } return null; }  }
ContentTypeNormaliserImpl implements ContentTypeNormaliser { @Override public MediaType getNormalisedRequestMediaType(HttpServletRequest request) { String contentType = request.getContentType(); if (request.getMethod().equals("POST")) { if (contentType == null) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type was not specified for deserialisable response"); } MediaType requestMT; try { requestMT = MediaType.valueOf(contentType); } catch (Exception e) { throw new CougarValidationException(ServerFaultCode.MediaTypeParseFailure, "Input content type cannot be parsed: " + contentType,e); } if (requestMT.isWildcardType() || requestMT.isWildcardSubtype()) { throw new CougarValidationException(ServerFaultCode.InvalidInputMediaType, "Input content type may not be wildcard: " + requestMT); } if (!MediaTypeUtils.isValid(allContentTypes, requestMT)) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type is not valid: " + requestMT); } String candidateContentType = requestMT.getType() + "/" + requestMT.getSubtype(); MediaType normalizedMediaType = validContentTypes.get(candidateContentType); if (normalizedMediaType == null) { throw new CougarValidationException(ServerFaultCode.FrameworkError, "Input content type " + contentType + " failed to find a normalized type using key " + candidateContentType); } return normalizedMediaType; } return null; }  @Override void addValidContentTypes(final Set<String> vct, MediaType normalisedContentType); @Override void addValidEncodings(final Set<String> validEncodings); @Override MediaType getNormalisedResponseMediaType(HttpServletRequest request); @Override MediaType getNormalisedRequestMediaType(HttpServletRequest request); void setDefaultResponseFormat(String defaultResponseFormat); @Override String getNormalisedEncoding(HttpServletRequest request); }
ContentTypeNormaliserImpl implements ContentTypeNormaliser { @Override public MediaType getNormalisedRequestMediaType(HttpServletRequest request) { String contentType = request.getContentType(); if (request.getMethod().equals("POST")) { if (contentType == null) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type was not specified for deserialisable response"); } MediaType requestMT; try { requestMT = MediaType.valueOf(contentType); } catch (Exception e) { throw new CougarValidationException(ServerFaultCode.MediaTypeParseFailure, "Input content type cannot be parsed: " + contentType,e); } if (requestMT.isWildcardType() || requestMT.isWildcardSubtype()) { throw new CougarValidationException(ServerFaultCode.InvalidInputMediaType, "Input content type may not be wildcard: " + requestMT); } if (!MediaTypeUtils.isValid(allContentTypes, requestMT)) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type is not valid: " + requestMT); } String candidateContentType = requestMT.getType() + "/" + requestMT.getSubtype(); MediaType normalizedMediaType = validContentTypes.get(candidateContentType); if (normalizedMediaType == null) { throw new CougarValidationException(ServerFaultCode.FrameworkError, "Input content type " + contentType + " failed to find a normalized type using key " + candidateContentType); } return normalizedMediaType; } return null; }  @Override void addValidContentTypes(final Set<String> vct, MediaType normalisedContentType); @Override void addValidEncodings(final Set<String> validEncodings); @Override MediaType getNormalisedResponseMediaType(HttpServletRequest request); @Override MediaType getNormalisedRequestMediaType(HttpServletRequest request); void setDefaultResponseFormat(String defaultResponseFormat); @Override String getNormalisedEncoding(HttpServletRequest request); static final String DEFAULT_ENCODING; }