Refine search
private boolean containsType( List<MediaType> mediaTypes, MediaType mediaType ) { for ( MediaType type : mediaTypes ) { if ( mediaType.getType().equals( type.getType() ) && mediaType.getSubtype().equals( type.getSubtype() ) ) { return true; } } return false; }
public static MediaType mediaTypeWithCharsetUtf8( MediaType mediaType ) { Map<String,String> parameters = mediaType.getParameters(); if ( parameters.isEmpty() ) { return new MediaType( mediaType.getType(), mediaType.getSubtype(), CHARSET ); } if ( parameters.containsKey( "charset" ) ) { return mediaType; } Map<String,String> paramsWithCharset = new HashMap<>( parameters ); paramsWithCharset.putAll( CHARSET ); return new MediaType( mediaType.getType(), mediaType.getSubtype(), paramsWithCharset ); }
@Override public String toString(MediaType header) { throwIllegalArgumentExceptionIfNull(header, MEDIA_TYPE_IS_NULL); StringBuilder b = new StringBuilder(); b.append(header.getType()).append('/').append(header.getSubtype()); for (Map.Entry<String, String> e : header.getParameters().entrySet()) { b.append(";").append(e.getKey()).append('='); StringBuilderUtils.appendQuotedIfNonToken(b, e.getValue()); } return b.toString(); }
@Override public int hash(final MediaType mt) { // treat compatible types as equal return mt.getType().toLowerCase().hashCode() + mt.getSubtype().toLowerCase().hashCode(); } };
@Override public String toString(MediaType header) { throwIllegalArgumentExceptionIfNull(header, MEDIA_TYPE_IS_NULL); StringBuilder b = new StringBuilder(); b.append(header.getType()).append('/').append(header.getSubtype()); for (Map.Entry<String, String> e : header.getParameters().entrySet()) { b.append(";").append(e.getKey()).append('='); StringBuilderUtils.appendQuotedIfNonToken(b, e.getValue()); } return b.toString(); }
@Override public int hash(final MediaType mt) { // treat compatible types as equal return mt.getType().toLowerCase().hashCode() + mt.getSubtype().toLowerCase().hashCode(); } };
private static MediaType unquoteMediaTypeParameters(final MediaType mediaType, final String... parameters) { if (parameters == null || parameters.length == 0) { return mediaType; } final Map<String, String> unquotedParams = new HashMap<>(mediaType.getParameters()); for (final String parameter : parameters) { String value = mediaType.getParameters().get(parameter); if (value != null && value.startsWith("\"")) { value = value.substring(1, value.length() - 1); unquotedParams.put(parameter, value); } } return new MediaType(mediaType.getType(), mediaType.getSubtype(), unquotedParams); } }
/** * Returns a flag whether the given {@link MediaType media type} belongs to the group of JavaScript media types. * * @param mediaType media type to check. * @return {@code true} if the given media type is a JavaScript type, {@code false} otherwise (or if the media type is * {@code null}} */ private boolean isJavascript(final MediaType mediaType) { if (mediaType == null) { return false; } final Set<String> subtypes = JAVASCRIPT_TYPES.get(mediaType.getType()); return subtypes != null && subtypes.contains(mediaType.getSubtype()); }
/** * Strips any quality parameters, i.e. q and qs from given media type. * * @param mediaType type to strip quality parameters from * @return media type instance corresponding to the given one with quality parameters stripped off * or the original instance if no such parameters are present */ public static MediaType stripQualityParams(MediaType mediaType) { final Map<String, String> oldParameters = mediaType.getParameters(); if (oldParameters.isEmpty() || (!oldParameters.containsKey(Quality.QUALITY_SOURCE_PARAMETER_NAME) && !oldParameters.containsKey(Quality.QUALITY_PARAMETER_NAME))) { return mediaType; } return new MediaType(mediaType.getType(), mediaType.getSubtype(), oldParameters.entrySet() .stream() .filter(entry -> QUALITY_PARAM_FILTERING_PREDICATE.test(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); }
/** * Returns a flag whether the given {@link MediaType media type} belongs to the group of JavaScript media types. * * @param mediaType media type to check. * @return {@code true} if the given media type is a JavaScript type, {@code false} otherwise (or if the media type is * {@code null}} */ private boolean isJavascript(final MediaType mediaType) { if (mediaType == null) { return false; } final Set<String> subtypes = JAVASCRIPT_TYPES.get(mediaType.getType()); return subtypes != null && subtypes.contains(mediaType.getSubtype()); }
/** * Strips any quality parameters, i.e. q and qs from given media type. * * @param mediaType type to strip quality parameters from * @return media type instance corresponding to the given one with quality parameters stripped off * or the original instance if no such parameters are present */ public static MediaType stripQualityParams(MediaType mediaType) { final Map<String, String> oldParameters = mediaType.getParameters(); if (oldParameters.isEmpty() || (!oldParameters.containsKey(Quality.QUALITY_SOURCE_PARAMETER_NAME) && !oldParameters.containsKey(Quality.QUALITY_PARAMETER_NAME))) { return mediaType; } return new MediaType(mediaType.getType(), mediaType.getSubtype(), oldParameters.entrySet() .stream() .filter(entry -> QUALITY_PARAM_FILTERING_PREDICATE.test(entry.getKey())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))); }
/** * Determine if the two media types are type-equal (their {@link MediaType#getType() type} * and {@link MediaType#getSubtype() subtype} are equal). For example: * <pre> * m/n == m/n * m/n;p1 == m/n;p2 * * m/n != m/y * m/n != x/n * m/n != x/y</pre> * * @param m1 first media type. * @param m2 second media type. * @return {@code true} if the two media types are of the same type and subtype, * {@code false} otherwise. */ public static boolean typeEqual(MediaType m1, MediaType m2) { if (m1 == null || m2 == null) { return false; } return m1.getSubtype().equalsIgnoreCase(m2.getSubtype()) && m1.getType().equalsIgnoreCase(m2.getType()); }
/** * Transforms a media type and add a boundary parameter with a unique value * if one is not already present. * * @param mediaType if {@code null} then a media type of "multipart/mixed" with a boundary parameter will be returned. * @return the media type with a boundary parameter. */ public static MediaType addBoundary(MediaType mediaType) { if (mediaType == null) { return MultiPartMediaTypes.createMixed(); } if (!mediaType.getParameters().containsKey(BOUNDARY_PARAMETER)) { final Map<String, String> parameters = new HashMap<String, String>( mediaType.getParameters()); parameters.put(BOUNDARY_PARAMETER, createBoundary()); return new MediaType(mediaType.getType(), mediaType.getSubtype(), parameters); } return mediaType; }
/** * Determine if the two media types are type-equal (their {@link MediaType#getType() type} * and {@link MediaType#getSubtype() subtype} are equal). For example: * <pre> * m/n == m/n * m/n;p1 == m/n;p2 * * m/n != m/y * m/n != x/n * m/n != x/y</pre> * * @param m1 first media type. * @param m2 second media type. * @return {@code true} if the two media types are of the same type and subtype, * {@code false} otherwise. */ public static boolean typeEqual(MediaType m1, MediaType m2) { if (m1 == null || m2 == null) { return false; } return m1.getSubtype().equalsIgnoreCase(m2.getSubtype()) && m1.getType().equalsIgnoreCase(m2.getType()); }
/** * Create new acceptable media type instance from the supplied * {@link javax.ws.rs.core.MediaType media type}. * * @param mediaType general-purpose media type. * @return new acceptable media type instance. * * @throws ParseException in case the quality parameter parsing failed. */ public static AcceptableMediaType valueOf(MediaType mediaType) throws ParseException { if (mediaType instanceof AcceptableMediaType) { return (AcceptableMediaType) mediaType; } final Map<String, String> parameters = mediaType.getParameters(); int quality = Quality.DEFAULT; if (parameters != null) { final String v = parameters.get(Quality.QUALITY_PARAMETER_NAME); if (v != null) { quality = HttpHeaderReader.readQualityFactor(v); } } // use private constructor to skip quality value validation step return new AcceptableMediaType(mediaType.getType(), mediaType.getSubtype(), parameters, quality); }
private static boolean overrides(MediaType mt1, MediaType mt2) { return mt1.isWildcardType() || (mt1.getType().equals(mt2.getType()) && (mt1.isWildcardSubtype() || mt1.getSubtype().equals(mt2.getSubtype() ))); }
/** * Set the {@link HttpHeaders#CONTENT_TYPE} header to the {@code httpHeaders} based on {@code mediaType} and * {@link #getEncoding() default encoding} defined in this processor. If {@code mediaType} defines encoding * then this encoding will be used otherwise the default processor encoding is used. The chosen encoding * is returned from the method. * * @param mediaType Media type of the entity. * @param httpHeaders Http headers. * @return Selected encoding. */ protected Charset setContentType(final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders) { final Charset encoding; final String charset = mediaType.getParameters().get(MediaType.CHARSET_PARAMETER); final MediaType finalMediaType; if (charset == null) { encoding = getEncoding(); final HashMap<String, String> params = new HashMap<>(mediaType.getParameters()); params.put(MediaType.CHARSET_PARAMETER, encoding.name()); finalMediaType = new MediaType(mediaType.getType(), mediaType.getSubtype(), params); } else { encoding = Charset.forName(charset); finalMediaType = mediaType; } final ArrayList<Object> typeList = new ArrayList<>(1); typeList.add(finalMediaType.toString()); httpHeaders.put(HttpHeaders.CONTENT_TYPE, typeList); return encoding; }
private static boolean overrides(MediaType mt1, MediaType mt2) { return mt1.isWildcardType() || (mt1.getType().equals(mt2.getType()) && (mt1.isWildcardSubtype() || mt1.getSubtype().equals(mt2.getSubtype() ))); }
/** * Create new acceptable media type instance from the supplied * {@link javax.ws.rs.core.MediaType media type}. * * @param mediaType general-purpose media type. * @return new acceptable media type instance. * * @throws ParseException in case the quality parameter parsing failed. */ public static AcceptableMediaType valueOf(MediaType mediaType) throws ParseException { if (mediaType instanceof AcceptableMediaType) { return (AcceptableMediaType) mediaType; } final Map<String, String> parameters = mediaType.getParameters(); int quality = Quality.DEFAULT; if (parameters != null) { final String v = parameters.get(Quality.QUALITY_PARAMETER_NAME); if (v != null) { quality = HttpHeaderReader.readQualityFactor(v); } } // use private constructor to skip quality value validation step return new AcceptableMediaType(mediaType.getType(), mediaType.getSubtype(), parameters, quality); }
private static MediaType create(MediaType mt) { return new MediaType(mt.getType(), mt.getSubtype(), Collections.singletonMap(Boundary.BOUNDARY_PARAMETER, Boundary.createBoundary())); }