public MediaType getPreferredMediaType(List<MediaType> mediaTypes) { for (MediaType mediaType : mediaTypes) { if (mediaType.getMimeType() == MimeType.APPLICATION_XML) { return mediaType; } else if (mediaType.getMimeType() == MimeType.APPLICATION_JSON) { return mediaType; } } return mediaTypes.get(0); }
private void describeLimitsForRequest( HttpServletRequest request, FilterDirector director, MediaType preferredMediaType) { if (preferredMediaType.getMimeType() == MimeType.UNKNOWN) { director.setFilterAction(FilterAction.RETURN); director.setResponseStatus(HttpStatusCode.NOT_ACCEPTABLE); } else { // If include absolute limits let request pass thru but prepare the combined // (absolute and active) limits when processing the response if (includeAbsoluteLimits) { director.setFilterAction(FilterAction.PROCESS_RESPONSE); director .requestHeaderManager() .putHeader(CommonHttpHeader.ACCEPT.toString(), MimeType.APPLICATION_XML.toString()); } else { try { final MimeType mimeType = rateLimitingServiceHelper.queryActiveLimits( request, preferredMediaType, director.getResponseOutputStream()); director .responseHeaderManager() .putHeader(CommonHttpHeader.CONTENT_TYPE.toString(), mimeType.toString()); director.setFilterAction(FilterAction.RETURN); director.setResponseStatus(HttpStatusCode.OK); } catch (Exception e) { consumeException(e, director); } } } }
public com.rackspace.papi.components.versioning.config.MediaType getMatchingMediaType( ServiceVersionMapping serviceVersionMapping, MediaType preferedMediaType) { final MediaTypeList configuredMediaTypes = serviceVersionMapping.getMediaTypes(); if (configuredMediaTypes == null) { return null; } for (com.rackspace.papi.components.versioning.config.MediaType configuredMediaType : configuredMediaTypes.getMediaType()) { HeaderValue mediaType = new HeaderValueParser(configuredMediaType.getType()).parse(); if (preferedMediaType.equalsTo(mediaType)) { return configuredMediaType; } } return null; }