public int compare(MediaType mediaType1, MediaType mediaType2) { double quality1 = mediaType1.getQualityValue(); double quality2 = mediaType2.getQualityValue(); int qualityComparison = Double.compare(quality2, quality1); if (qualityComparison != 0) { return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3 } else if (mediaType1.isWildcardType() && !mediaType2.isWildcardType()) { // */* < audio/* return 1; } else if (mediaType2.isWildcardType() && !mediaType1.isWildcardType()) { // audio/* > */* return -1; } else if (!mediaType1 .getType() .equals(mediaType2.getType())) { // audio/basic == text/html return 0; } else { // mediaType1.getType().equals(mediaType2.getType()) if (mediaType1.isWildcardSubtype() && !mediaType2.isWildcardSubtype()) { // audio/* < audio/basic return 1; } else if (mediaType2.isWildcardSubtype() && !mediaType1.isWildcardSubtype()) { // audio/basic > audio/* return -1; } else if (!mediaType1 .getSubtype() .equals(mediaType2.getSubtype())) { // audio/basic == audio/wave return 0; } else { int paramsSize1 = mediaType1.parameters.size(); int paramsSize2 = mediaType2.parameters.size(); return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic } } }
public static boolean isJSONType(MediaType type) { if (type == null) return false; if (type.getType() == null) return false; if (type.getSubtype() == null) return false; if (!"application".equals(type.getType().toLowerCase())) return false; if (!"json".equals(type.getSubtype().toLowerCase())) return false; return true; }
private int indexOfEqualMediaType(MediaType mediaType) { for (int i = 0; i < getExpressionsToCompare().size(); i++) { MediaType currentMediaType = getExpressionsToCompare().get(i).getMediaType(); if (mediaType.getType().equalsIgnoreCase(currentMediaType.getType()) && mediaType.getSubtype().equalsIgnoreCase(currentMediaType.getSubtype())) { return i; } } return -1; }
@SuppressWarnings("unchecked") public void doWithRequest(ClientHttpRequest request) throws IOException { if (responseType != null) { List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); for (HttpMessageConverter<?> messageConverter : getMessageConverters()) { if (messageConverter.canRead(responseType, null)) { List<MediaType> supportedMediaTypes = messageConverter.getSupportedMediaTypes(); for (MediaType supportedMediaType : supportedMediaTypes) { if (supportedMediaType.getCharSet() != null) { supportedMediaType = new MediaType(supportedMediaType.getType(), supportedMediaType.getSubtype()); } allSupportedMediaTypes.add(supportedMediaType); } } } if (!allSupportedMediaTypes.isEmpty()) { MediaType.sortBySpecificity(allSupportedMediaTypes); if (logger.isDebugEnabled()) { logger.debug("Setting request Accept header to " + allSupportedMediaTypes); } request.getHeaders().setAccept(allSupportedMediaTypes); } } }
@Test public void parseURLConnectionMediaType() throws Exception { String s = "*; q=.2"; MediaType mediaType = MediaType.parseMediaType(s); assertEquals("Invalid type", "*", mediaType.getType()); assertEquals("Invalid subtype", "*", mediaType.getSubtype()); assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D); }
@Test public void parseQuotedCharset() { String s = "application/xml;charset=\"utf-8\""; MediaType mediaType = MediaType.parseMediaType(s); assertEquals("Invalid type", "application", mediaType.getType()); assertEquals("Invalid subtype", "xml", mediaType.getSubtype()); assertEquals("Invalid charset", Charset.forName("UTF-8"), mediaType.getCharSet()); }
@Test public void parseCharset() throws Exception { String s = "text/html; charset=iso-8859-1"; MediaType mediaType = MediaType.parseMediaType(s); assertEquals("Invalid type", "text", mediaType.getType()); assertEquals("Invalid subtype", "html", mediaType.getSubtype()); assertEquals("Invalid charset", Charset.forName("ISO-8859-1"), mediaType.getCharSet()); }
private void validateResponse(final String responseBody, final MediaType mediaType) { if (responseBody != null && !responseBody.isEmpty()) { if (mediaType.getSubtype().equals(MediaType.APPLICATION_JSON.getSubtype())) { if (responseBody.contains("description") && responseBody.contains("code")) { String errorCode = extractCodeFromJson(responseBody); String errorDiscription = extractDescriptionFromJson(responseBody); LOGGER.info("errorCode : " + errorCode); LOGGER.info("errorDiscription : " + errorDiscription); throw new ResponseException(errorDiscription, errorCode); } } else { // TODO return; } } }
@Override protected HttpHeaders initHeaders() { HttpHeaders headers = new HttpHeaders(); for (Enumeration<?> names = getServletRequest().getHeaderNames(); names.hasMoreElements(); ) { String name = (String) names.nextElement(); for (Enumeration<?> values = getServletRequest().getHeaders(name); values.hasMoreElements(); ) { headers.add(name, (String) values.nextElement()); } } MediaType contentType = headers.getContentType(); if (contentType == null) { String requestContentType = getServletRequest().getContentType(); if (StringUtils.hasLength(requestContentType)) { contentType = MediaType.parseMediaType(requestContentType); headers.setContentType(contentType); } } if (contentType != null && contentType.getCharset() == null) { String encoding = getServletRequest().getCharacterEncoding(); if (StringUtils.hasLength(encoding)) { Charset charset = Charset.forName(encoding); Map<String, String> params = new LinkedCaseInsensitiveMap<>(); params.putAll(contentType.getParameters()); params.put("charset", charset.toString()); headers.setContentType( new MediaType(contentType.getType(), contentType.getSubtype(), params)); } } if (headers.getContentLength() == -1) { int contentLength = getServletRequest().getContentLength(); if (contentLength != -1) { headers.setContentLength(contentLength); } } return headers; }
/** * Copy-constructor that copies the type and subtype of the given {@code MediaType}, and allows * for different parameter. * * @param other the other media type * @param parameters the parameters, may be <code>null</code> * @throws IllegalArgumentException if any of the parameters contain illegal characters */ public MediaType(MediaType other, Map<String, String> parameters) { this(other.getType(), other.getSubtype(), parameters); }