private AcceptType(final String type, final String subtype, final Map<String, String> parameters, final Float quality) { this.type = type; this.subtype = subtype; this.parameters = TypeUtil.createParameterMap(); this.parameters.putAll(parameters); this.quality = quality; }
/** * Valid input are <code>;</code> separated <code>key=value</code> pairs * without spaces between key and value. * <p> * See RFC 7231: * The type, subtype, and parameter name tokens are case-insensitive. * Parameter values might or might not be case-sensitive, depending on * the semantics of the parameter name. The presence or absence of a * parameter might be significant to the processing of a media-type, * depending on its definition within the media type registry. * </p> * * @param parameters as <code>;</code> separated <code>key=value</code> pairs * @param parameterMap map to which all parsed parameters are added */ static void parseParameters(final String parameters, final Map<String, String> parameterMap) { if (parameters != null) { for (String parameter : parameters.split(TypeUtil.PARAMETER_SEPARATOR)) { final String[] keyValue = parseParameter(parameter); parameterMap.put(keyValue[0], keyValue[1]); } } }
private String parse(String acceptCharset, Map<String, String> parameters) { final String[] charsetAndParameters = acceptCharset.split(TypeUtil.PARAMETER_SEPARATOR, 2); acceptCharset = charsetAndParameters[0]; final String params = (charsetAndParameters.length > 1 ? charsetAndParameters[1] : null); TypeUtil.parseParameters(params, parameters); return acceptCharset; }
/** * Creates a content type from an existing content type and an additional parameter as key-value pair. * @param contentType an existing content type * @param parameterName the name of the additional parameter * @param parameterValue the value of the additional parameter * @return a new {@link ContentType} object */ public static ContentType create(final ContentType contentType, final String parameterName, final String parameterValue) throws IllegalArgumentException { TypeUtil.validateParameterNameAndValue(parameterName, parameterValue); ContentType type = new ContentType(contentType.type, contentType.subtype, contentType.parameters); type.parameters.put(parameterName.toLowerCase(Locale.ROOT), parameterValue); return type; }
/** * Creates a content type from an existing content type and an additional parameter as key-value pair. * @param contentType an existing content type * @param parameterName the name of the additional parameter * @param parameterValue the value of the additional parameter * @return a new {@link ContentType} object */ public static ContentType create(final ContentType contentType, final String parameterName, final String parameterValue) throws IllegalArgumentException { TypeUtil.validateParameterNameAndValue(parameterName, parameterValue); ContentType type = new ContentType(contentType.type, contentType.subtype, contentType.parameters); type.parameters.put(parameterName.toLowerCase(Locale.ROOT), parameterValue); return type; }
private AcceptType(final String type, final String subtype, final Map<String, String> parameters, final Float quality) { this.type = type; this.subtype = subtype; this.parameters = TypeUtil.createParameterMap(); this.parameters.putAll(parameters); this.quality = quality; }
private String parse(String acceptCharset, Map<String, String> parameters) { final String[] charsetAndParameters = acceptCharset.split(TypeUtil.PARAMETER_SEPARATOR, 2); acceptCharset = charsetAndParameters[0]; final String params = (charsetAndParameters.length > 1 ? charsetAndParameters[1] : null); TypeUtil.parseParameters(params, parameters); return acceptCharset; }
/** * Valid input is one <code>key=value</code> pair without spaces between key and value. * <p> * See RFC 7231: * The type, subtype, and parameter name tokens are case-insensitive. * Parameter values might or might not be case-sensitive, depending on * the semantics of the parameter name. The presence or absence of a * parameter might be significant to the processing of a media-type, * depending on its definition within the media type registry. * </p> * * @param parameter as <code>key=value</code> pair * @return <code>key</code> as first array value (as lower case) and <code>value</code> as second array value */ static String[] parseParameter(final String parameter) { if (parameter.isEmpty()) { throw new IllegalArgumentException("An empty parameter is not allowed."); } String[] keyValue = parameter.trim().split(PARAMETER_KEY_VALUE_SEPARATOR); if (keyValue.length != 2) { throw new IllegalArgumentException("Parameter '" + parameter + "' must have exactly one '" + PARAMETER_KEY_VALUE_SEPARATOR + "' that separates the name and the value."); } validateParameterNameAndValue(keyValue[0], keyValue[1]); keyValue[0] = keyValue[0].toLowerCase(Locale.ENGLISH); return keyValue; }
/** * Valid input are <code>;</code> separated <code>key=value</code> pairs * without spaces between key and value. * <p> * See RFC 7231: * The type, subtype, and parameter name tokens are case-insensitive. * Parameter values might or might not be case-sensitive, depending on * the semantics of the parameter name. The presence or absence of a * parameter might be significant to the processing of a media-type, * depending on its definition within the media type registry. * </p> * * @param parameters as <code>;</code> separated <code>key=value</code> pairs * @param parameterMap map to which all parsed parameters are added */ static void parseParameters(final String parameters, final Map<String, String> parameterMap) { if (parameters != null) { for (String parameter : parameters.split(TypeUtil.PARAMETER_SEPARATOR)) { final String[] keyValue = parseParameter(parameter); parameterMap.put(keyValue[0], keyValue[1]); } } }
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); } }
private static void parse(final String format, final List<String> typeSubtype, final Map<String, String> parameters) { final String[] typesAndParameters = format.split(TypeUtil.PARAMETER_SEPARATOR, 2); final String types = typesAndParameters[0]; final String params = (typesAndParameters.length > 1 ? typesAndParameters[1] : null); String[] tokens = types.split(TypeUtil.TYPE_SUBTYPE_SEPARATOR); if (tokens.length == 2) { if (tokens[0] == null || tokens[0].isEmpty()) { throw new IllegalArgumentException("No type found in format: '" + format + "'."); } else if (tokens[1] == null || tokens[1].isEmpty()) { throw new IllegalArgumentException("No subtype found in format: '" + format + "'."); } else { typeSubtype.add(tokens[0]); typeSubtype.add(tokens[1]); } } else { throw new IllegalArgumentException("Not exactly one '" + TypeUtil.TYPE_SUBTYPE_SEPARATOR + " at the beginning or at the end in format: " + format); } TypeUtil.parseParameters(params, parameters); }
/** * Valid input is one <code>key=value</code> pair without spaces between key and value. * <p> * See RFC 7231: * The type, subtype, and parameter name tokens are case-insensitive. * Parameter values might or might not be case-sensitive, depending on * the semantics of the parameter name. The presence or absence of a * parameter might be significant to the processing of a media-type, * depending on its definition within the media type registry. * </p> * * @param parameter as <code>key=value</code> pair * @return <code>key</code> as first array value (as lower case) and <code>value</code> as second array value */ static String[] parseParameter(final String parameter) { if (parameter.isEmpty()) { throw new IllegalArgumentException("An empty parameter is not allowed."); } String[] keyValue = parameter.trim().split(PARAMETER_KEY_VALUE_SEPARATOR); if (keyValue.length != 2) { throw new IllegalArgumentException("Parameter '" + parameter + "' must have exactly one '" + PARAMETER_KEY_VALUE_SEPARATOR + "' that separates the name and the value."); } validateParameterNameAndValue(keyValue[0], keyValue[1]); keyValue[0] = keyValue[0].toLowerCase(Locale.ENGLISH); return keyValue; }
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); } }
private static void parse(final String format, final List<String> typeSubtype, final Map<String, String> parameters) { final String[] typesAndParameters = format.split(TypeUtil.PARAMETER_SEPARATOR, 2); final String types = typesAndParameters[0]; final String params = (typesAndParameters.length > 1 ? typesAndParameters[1] : null); String[] tokens = types.split(TypeUtil.TYPE_SUBTYPE_SEPARATOR); if (tokens.length == 2) { if (tokens[0] == null || tokens[0].isEmpty()) { throw new IllegalArgumentException("No type found in format: '" + format + "'."); } else if (tokens[1] == null || tokens[1].isEmpty()) { throw new IllegalArgumentException("No subtype found in format: '" + format + "'."); } else { typeSubtype.add(tokens[0]); typeSubtype.add(tokens[1]); } } else { throw new IllegalArgumentException("Not exactly one '" + TypeUtil.TYPE_SUBTYPE_SEPARATOR + " at the beginning or at the end in format: " + format); } TypeUtil.parseParameters(params, parameters); }
private AcceptCharset(final String charset) { parameters = TypeUtil.createParameterMap(); this.charset = parse(charset, parameters); if (!(UTF8_CHARSET.equalsIgnoreCase(this.charset)) && !(UTF8_CHARSET1.equalsIgnoreCase(this.charset)) && !(TypeUtil.MEDIA_TYPE_WILDCARD.equals(this.charset))) { if (CHARSET_PATTERN.matcher(this.charset).matches()) { throw new UnsupportedCharsetException("Unsupported charset in accept charset header:" + charset); } else { throw new IllegalArgumentException("Illegal charset in accept charset header:" + charset); } } for (Entry<String, String> param : parameters.entrySet()) { if (!param.getKey().equals(TypeUtil.PARAMETER_Q)) { throw new IllegalArgumentException("Illegal parameters in accept charset header:" + charset); } } 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 charset header:" + charset); } }
private static void parse(final String format, List<String> typeSubtype, Map<String, String> parameters) throws IllegalArgumentException { final String[] typesAndParameters = format.split(TypeUtil.PARAMETER_SEPARATOR, 2); final String types = typesAndParameters[0]; final String params = (typesAndParameters.length > 1 ? typesAndParameters[1] : null); if (types.contains(TypeUtil.TYPE_SUBTYPE_SEPARATOR)) { final String[] tokens = types.split(TypeUtil.TYPE_SUBTYPE_SEPARATOR); if (tokens.length == 2) { if (tokens[0] == null || tokens[0].isEmpty()) { throw new IllegalArgumentException("No type found in format '" + format + "'."); } else if (tokens[1] == null || tokens[1].isEmpty()) { throw new IllegalArgumentException("No subtype found in format '" + format + "'."); } else { typeSubtype.add(tokens[0]); typeSubtype.add(tokens[1]); } } else { throw new IllegalArgumentException( "Too many '" + TypeUtil.TYPE_SUBTYPE_SEPARATOR + "' in format '" + format + "'."); } } else { throw new IllegalArgumentException("No separator '" + TypeUtil.TYPE_SUBTYPE_SEPARATOR + "' was found in format '" + format + "'."); } TypeUtil.parseParameters(params, parameters); }
/** * Creates a content type from type, subtype, and parameters. * @param type type * @param subtype subtype * @param parameters parameters as map from names to values */ private ContentType(final String type, final String subtype, final Map<String, String> parameters) { this.type = validateType(type); this.subtype = validateType(subtype); if (parameters == null) { this.parameters = Collections.emptyMap(); } else { this.parameters = TypeUtil.createParameterMap(); this.parameters.putAll(parameters); } }
private static void parse(final String format, List<String> typeSubtype, Map<String, String> parameters) throws IllegalArgumentException { final String[] typesAndParameters = format.split(TypeUtil.PARAMETER_SEPARATOR, 2); final String types = typesAndParameters[0]; final String params = (typesAndParameters.length > 1 ? typesAndParameters[1] : null); if (types.contains(TypeUtil.TYPE_SUBTYPE_SEPARATOR)) { final String[] tokens = types.split(TypeUtil.TYPE_SUBTYPE_SEPARATOR); if (tokens.length == 2) { if (tokens[0] == null || tokens[0].isEmpty()) { throw new IllegalArgumentException("No type found in format '" + format + "'."); } else if (tokens[1] == null || tokens[1].isEmpty()) { throw new IllegalArgumentException("No subtype found in format '" + format + "'."); } else { typeSubtype.add(tokens[0]); typeSubtype.add(tokens[1]); } } else { throw new IllegalArgumentException( "Too many '" + TypeUtil.TYPE_SUBTYPE_SEPARATOR + "' in format '" + format + "'."); } } else { throw new IllegalArgumentException("No separator '" + TypeUtil.TYPE_SUBTYPE_SEPARATOR + "' was found in format '" + format + "'."); } TypeUtil.parseParameters(params, parameters); }
/** * Creates a content type from type, subtype, and parameters. * @param type type * @param subtype subtype * @param parameters parameters as map from names to values */ private ContentType(final String type, final String subtype, final Map<String, String> parameters) { this.type = validateType(type); this.subtype = validateType(subtype); if (parameters == null) { this.parameters = Collections.emptyMap(); } else { this.parameters = TypeUtil.createParameterMap(); this.parameters.putAll(parameters); } }
private AcceptCharset(final String charset) { parameters = TypeUtil.createParameterMap(); this.charset = parse(charset, parameters); if (!(UTF8_CHARSET.equalsIgnoreCase(this.charset)) && !(UTF8_CHARSET1.equalsIgnoreCase(this.charset)) && !(TypeUtil.MEDIA_TYPE_WILDCARD.equals(this.charset))) { if (CHARSET_PATTERN.matcher(this.charset).matches()) { throw new UnsupportedCharsetException("Unsupported charset in accept charset header:" + charset); } else { throw new IllegalArgumentException("Illegal charset in accept charset header:" + charset); } } for (Entry<String, String> param : parameters.entrySet()) { if (!param.getKey().equals(TypeUtil.PARAMETER_Q)) { throw new IllegalArgumentException("Illegal parameters in accept charset header:" + charset); } } 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 charset header:" + charset); } }