@Override public int compare(final AcceptType a1, final AcceptType a2) { int compare = a2.getQuality().compareTo(a1.getQuality()); if (compare != 0) { return compare; } compare = (a1.getType().equals(TypeUtil.MEDIA_TYPE_WILDCARD) ? 1 : 0) - (a2.getType().equals(TypeUtil.MEDIA_TYPE_WILDCARD) ? 1 : 0); if (compare != 0) { return compare; } compare = (a1.getSubtype().equals(TypeUtil.MEDIA_TYPE_WILDCARD) ? 1 : 0) - (a2.getSubtype().equals(TypeUtil.MEDIA_TYPE_WILDCARD) ? 1 : 0); if (compare != 0) { return compare; } return a2.getParameters().size() - a1.getParameters().size(); } });
/** * Creates a list of {@link AcceptType} objects based on given input string. * @param acceptTypes accept types, comma-separated, as specified for the HTTP header <code>Accept</code> * @return a list of <code>AcceptType</code> objects * @throws IllegalArgumentException if input string is not parseable */ public static List<AcceptType> create(final String acceptTypes) { if (acceptTypes == null) { throw new IllegalArgumentException("Type parameter MUST NOT be null."); } List<AcceptType> result = new ArrayList<AcceptType>(); List<IllegalArgumentException> exceptionList = new ArrayList<IllegalArgumentException>(); String[] values = acceptTypes.split(","); for (String value : values) { try { result.add(new AcceptType(value.trim())); } catch (IllegalArgumentException e) { exceptionList.add(e); } } if (!exceptionList.isEmpty() || result.isEmpty()) { throw exceptionList.get(0); } sort(result); return result; }
public static boolean isSupported(final ContentType contentType, final CustomContentTypeSupport customContentTypeSupport, final RepresentationType representationType) throws ContentNegotiatorException { for (ContentType supportedContentType : getSupportedContentTypes(customContentTypeSupport, representationType)) { if (AcceptType.fromContentType(supportedContentType).get(0).matches(contentType)) { return true; } } return false; } }
List<AcceptType> formatTypes = null; try { formatTypes = AcceptType.fromContentType(contentType == null ? ContentType.create(formatOption.getFormat()) : contentType); } catch (final IllegalArgumentException e) { AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString); Map<String, String> formatParameters = formatTypes.get(0).getParameters(); if (!formatParameters.isEmpty() && null != formatParameters.get(ContentType.PARAMETER_CHARSET)) { isCharsetInFormat = true; charsets = getAcceptCharset(acceptCharset); try { result = getAcceptedType(AcceptType.create(acceptHeaderValue), supportedContentTypes, charsets); } catch (final IllegalArgumentException e) { List<AcceptType> types = AcceptType.create(acceptHeaderValue); throw new ContentNegotiatorException( "The combination of type and subtype " + types.get(0) + charsets = getAcceptCharset(acceptCharset); final ContentType requestedContentType = getDefaultSupportedContentTypes(representationType).get(0); result = getAcceptedType(AcceptType.fromContentType(requestedContentType), supportedContentTypes, charsets);
for (final ContentType supportedContentType : supportedContentTypes) { ContentType contentType = supportedContentType; final String charSetValue = acceptedType.getParameter(ContentType.PARAMETER_CHARSET); if (charset != null) { if ("*".equals(charset.toString())) { if (CHARSET_PATTERN.matcher(charSetValue).matches()) { throw new ContentNegotiatorException("Unsupported accept-header-charset = " + charSetValue, ContentNegotiatorException.MessageKeys.UNSUPPORTED_ACCEPT_HEADER_CHARSET, acceptedType.toString()); } else { throw new AcceptHeaderContentNegotiatorException( "Illegal charset in Accept header: " + charSetValue, AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_ACCEPT_HEADER_CHARSET, acceptedType.toString()); final String ieee754compatibleValue = acceptedType.getParameter(ContentType.PARAMETER_IEEE754_COMPATIBLE); if ("true".equalsIgnoreCase(ieee754compatibleValue)) { contentType = ContentType.create(contentType, ContentType.PARAMETER_IEEE754_COMPATIBLE, "true"); } else if (ieee754compatibleValue != null) { throw new IllegalArgumentException("Invalid IEEE754Compatible value in accept header:" + acceptedType.toString()); if (acceptedType.matches(contentType)) { return contentType;
/** * Creates a list of {@link AcceptType} objects based on given content type. * @param contentType the content type * @return an immutable one-element list of <code>AcceptType</code> objects that matches only the given content type */ public static List<AcceptType> fromContentType(final ContentType contentType) { return Collections.singletonList(new AcceptType( contentType.getType(), contentType.getSubtype(), contentType.getParameters(), 1F)); }
private AcceptType(final String type) { List<String> typeSubtype = new ArrayList<String>(); parameters = TypeUtil.createParameterMap(); parse(type, typeSubtype, parameters); this.type = typeSubtype.get(0); subtype = typeSubtype.get(1); if (TypeUtil.MEDIA_TYPE_WILDCARD.equals(this.type) && !TypeUtil.MEDIA_TYPE_WILDCARD.equals(subtype)) { throw new IllegalArgumentException("Illegal combination of WILDCARD type with NONE WILDCARD " + "subtype in accept header:" + type); } final String q = parameters.get(TypeUtil.PARAMETER_Q); if (q == null) { quality = 1F; } else if (Q_PATTERN.matcher(q).matches()) { quality = Float.valueOf(q); } else { throw new IllegalArgumentException("Illegal quality parameter '" + q + "' in accept header:" + type); } }
List<AcceptType> formatTypes = null; try { formatTypes = AcceptType.fromContentType(contentType == null ? ContentType.create(formatOption.getFormat()) : contentType); } catch (final IllegalArgumentException e) { AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString); Map<String, String> formatParameters = formatTypes.get(0).getParameters(); if (!formatParameters.isEmpty() && null != formatParameters.get(ContentType.PARAMETER_CHARSET)) { isCharsetInFormat = true; charsets = getAcceptCharset(acceptCharset); try { result = getAcceptedType(AcceptType.create(acceptHeaderValue), supportedContentTypes, charsets); } catch (final IllegalArgumentException e) { List<AcceptType> types = AcceptType.create(acceptHeaderValue); throw new ContentNegotiatorException( "The combination of type and subtype " + types.get(0) + charsets = getAcceptCharset(acceptCharset); final ContentType requestedContentType = getDefaultSupportedContentTypes(representationType).get(0); result = getAcceptedType(AcceptType.fromContentType(requestedContentType), supportedContentTypes, charsets);
for (final ContentType supportedContentType : supportedContentTypes) { ContentType contentType = supportedContentType; final String charSetValue = acceptedType.getParameter(ContentType.PARAMETER_CHARSET); if (charset != null) { if ("*".equals(charset.toString())) { if (CHARSET_PATTERN.matcher(charSetValue).matches()) { throw new ContentNegotiatorException("Unsupported accept-header-charset = " + charSetValue, ContentNegotiatorException.MessageKeys.UNSUPPORTED_ACCEPT_HEADER_CHARSET, acceptedType.toString()); } else { throw new AcceptHeaderContentNegotiatorException( "Illegal charset in Accept header: " + charSetValue, AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_ACCEPT_HEADER_CHARSET, acceptedType.toString()); final String ieee754compatibleValue = acceptedType.getParameter(ContentType.PARAMETER_IEEE754_COMPATIBLE); if ("true".equalsIgnoreCase(ieee754compatibleValue)) { contentType = ContentType.create(contentType, ContentType.PARAMETER_IEEE754_COMPATIBLE, "true"); } else if (ieee754compatibleValue != null) { throw new IllegalArgumentException("Invalid IEEE754Compatible value in accept header:" + acceptedType.toString()); if (acceptedType.matches(contentType)) { return contentType;
/** * Creates a list of {@link AcceptType} objects based on given content type. * @param contentType the content type * @return an immutable one-element list of <code>AcceptType</code> objects that matches only the given content type */ public static List<AcceptType> fromContentType(final ContentType contentType) { return Collections.singletonList(new AcceptType( contentType.getType(), contentType.getSubtype(), contentType.getParameters(), 1F)); }
private AcceptType(final String type) { List<String> typeSubtype = new ArrayList<String>(); parameters = TypeUtil.createParameterMap(); parse(type, typeSubtype, parameters); this.type = typeSubtype.get(0); subtype = typeSubtype.get(1); if (TypeUtil.MEDIA_TYPE_WILDCARD.equals(this.type) && !TypeUtil.MEDIA_TYPE_WILDCARD.equals(subtype)) { throw new IllegalArgumentException("Illegal combination of WILDCARD type with NONE WILDCARD " + "subtype in accept header:" + type); } final String q = parameters.get(TypeUtil.PARAMETER_Q); if (q == null) { quality = 1F; } else if (Q_PATTERN.matcher(q).matches()) { quality = Float.valueOf(q); } else { throw new IllegalArgumentException("Illegal quality parameter '" + q + "' in accept header:" + type); } }
@Override public int compare(final AcceptType a1, final AcceptType a2) { int compare = a2.getQuality().compareTo(a1.getQuality()); if (compare != 0) { return compare; } compare = (a1.getType().equals(TypeUtil.MEDIA_TYPE_WILDCARD) ? 1 : 0) - (a2.getType().equals(TypeUtil.MEDIA_TYPE_WILDCARD) ? 1 : 0); if (compare != 0) { return compare; } compare = (a1.getSubtype().equals(TypeUtil.MEDIA_TYPE_WILDCARD) ? 1 : 0) - (a2.getSubtype().equals(TypeUtil.MEDIA_TYPE_WILDCARD) ? 1 : 0); if (compare != 0) { return compare; } return a2.getParameters().size() - a1.getParameters().size(); } });
public static boolean isSupported(final ContentType contentType, final CustomContentTypeSupport customContentTypeSupport, final RepresentationType representationType) throws ContentNegotiatorException { for (ContentType supportedContentType : getSupportedContentTypes(customContentTypeSupport, representationType)) { if (AcceptType.fromContentType(supportedContentType).get(0).matches(contentType)) { return true; } } return false; } }
/** * Creates a list of {@link AcceptType} objects based on given input string. * @param acceptTypes accept types, comma-separated, as specified for the HTTP header <code>Accept</code> * @return a list of <code>AcceptType</code> objects * @throws IllegalArgumentException if input string is not parseable */ public static List<AcceptType> create(final String acceptTypes) { if (acceptTypes == null) { throw new IllegalArgumentException("Type parameter MUST NOT be null."); } List<AcceptType> result = new ArrayList<AcceptType>(); List<IllegalArgumentException> exceptionList = new ArrayList<IllegalArgumentException>(); String[] values = acceptTypes.split(","); for (String value : values) { try { result.add(new AcceptType(value.trim())); } catch (IllegalArgumentException e) { exceptionList.add(e); } } if (!exceptionList.isEmpty() || result.isEmpty()) { throw exceptionList.get(0); } sort(result); return result; }
public static void checkSupport(final ContentType contentType, final CustomContentTypeSupport customContentTypeSupport, final RepresentationType representationType) throws ContentNegotiatorException { for (ContentType supportedContentType : getSupportedContentTypes(customContentTypeSupport, representationType)) { if (AcceptType.fromContentType(supportedContentType).get(0).matches(contentType)) { return; } } throw new ContentNegotiatorException("unsupported content type: " + contentType, ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, contentType.toContentTypeString()); }
public static void checkSupport(final ContentType contentType, final CustomContentTypeSupport customContentTypeSupport, final RepresentationType representationType) throws ContentNegotiatorException { for (ContentType supportedContentType : getSupportedContentTypes(customContentTypeSupport, representationType)) { if (AcceptType.fromContentType(supportedContentType).get(0).matches(contentType)) { return; } } throw new ContentNegotiatorException("unsupported content type: " + contentType, ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPE, contentType.toContentTypeString()); }