refactored HTTP support into top-level package "org.springframework.http"; revised RestTemplate facility in package "org.springframework.web.client"
This commit is contained in:
@@ -1,144 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link MultiValueMap} that wraps a plain {@code Map}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @since 3.0
|
||||
*/
|
||||
public class DefaultMultiValueMap<K, V> implements MultiValueMap<K, V> {
|
||||
|
||||
private final Map<K, List<V>> wrappee;
|
||||
|
||||
/**
|
||||
* Constructs a new intance of the {@code DefaultMultiValueMap} wrapping a plain {@link LinkedHashMap}.
|
||||
*/
|
||||
public DefaultMultiValueMap() {
|
||||
this(new LinkedHashMap<K, List<V>>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new intance of the {@code DefaultMultiValueMap} wrapping the given map.
|
||||
*
|
||||
* @param wrappee the map to be wrapped
|
||||
*/
|
||||
public DefaultMultiValueMap(Map<K, List<V>> wrappee) {
|
||||
Assert.notNull(wrappee, "'wrappee' must not be null");
|
||||
this.wrappee = wrappee;
|
||||
}
|
||||
|
||||
/*
|
||||
* MultiValueMap implementation
|
||||
*/
|
||||
|
||||
public void add(K key, V value) {
|
||||
List<V> values = wrappee.get(key);
|
||||
if (values == null) {
|
||||
values = new LinkedList<V>();
|
||||
wrappee.put(key, values);
|
||||
}
|
||||
values.add(value);
|
||||
}
|
||||
|
||||
public V getFirst(K key) {
|
||||
List<V> values = wrappee.get(key);
|
||||
return values != null ? values.get(0) : null;
|
||||
}
|
||||
|
||||
public void set(K key, V value) {
|
||||
List<V> values = new LinkedList<V>();
|
||||
values.add(value);
|
||||
wrappee.put(key, values);
|
||||
}
|
||||
|
||||
/*
|
||||
* Map implementation
|
||||
*/
|
||||
|
||||
public int size() {
|
||||
return wrappee.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return wrappee.isEmpty();
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
return wrappee.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
return wrappee.containsValue(value);
|
||||
}
|
||||
|
||||
public List<V> get(Object key) {
|
||||
return wrappee.get(key);
|
||||
}
|
||||
|
||||
public List<V> put(K key, List<V> value) {
|
||||
return wrappee.put(key, value);
|
||||
}
|
||||
|
||||
public List<V> remove(Object key) {
|
||||
return wrappee.remove(key);
|
||||
}
|
||||
|
||||
public void putAll(Map<? extends K, ? extends List<V>> m) {
|
||||
wrappee.putAll(m);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
wrappee.clear();
|
||||
}
|
||||
|
||||
public Set<K> keySet() {
|
||||
return wrappee.keySet();
|
||||
}
|
||||
|
||||
public Collection<List<V>> values() {
|
||||
return wrappee.values();
|
||||
}
|
||||
|
||||
public Set<Entry<K, List<V>>> entrySet() {
|
||||
return wrappee.entrySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return wrappee.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return this.wrappee.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return wrappee.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Simple implementation of {@link MultiValueMap} that wraps a plain {@code Map}
|
||||
* (by default a {@link LinkedHashMap}, storing multiple values in a {@link LinkedList}.
|
||||
*
|
||||
* <p>This Map implementation is generally not thread-safe. It is primarily designed
|
||||
* for data structures exposed from request objects, for use in a single thread only.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V> {
|
||||
|
||||
private final Map<K, List<V>> targetMap;
|
||||
|
||||
|
||||
/**
|
||||
* Create a new SimpleMultiValueMap that wraps the given target Map.
|
||||
* @param wrappee the target Map to wrap
|
||||
*/
|
||||
public LinkedMultiValueMap() {
|
||||
this.targetMap = new LinkedHashMap<K, List<V>>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new SimpleMultiValueMap that wraps the given target Map.
|
||||
* <p>Note: The given Map will be used as active underlying Map.
|
||||
* Any changes in the underlying map will be reflected in the
|
||||
* MultiValueMap object, and vice versa.
|
||||
* @param targetMap the target Map to wrap
|
||||
*/
|
||||
public LinkedMultiValueMap(Map<K, List<V>> targetMap) {
|
||||
Assert.notNull(targetMap, "'targetMap' must not be null");
|
||||
this.targetMap = targetMap;
|
||||
}
|
||||
|
||||
// MultiValueMap implementation
|
||||
|
||||
public void add(K key, V value) {
|
||||
List<V> values = this.targetMap.get(key);
|
||||
if (values == null) {
|
||||
values = new LinkedList<V>();
|
||||
this.targetMap.put(key, values);
|
||||
}
|
||||
values.add(value);
|
||||
}
|
||||
|
||||
public V getFirst(K key) {
|
||||
List<V> values = this.targetMap.get(key);
|
||||
return (values != null ? values.get(0) : null);
|
||||
}
|
||||
|
||||
public void set(K key, V value) {
|
||||
List<V> values = new LinkedList<V>();
|
||||
values.add(value);
|
||||
this.targetMap.put(key, values);
|
||||
}
|
||||
|
||||
|
||||
// Map implementation
|
||||
|
||||
public int size() {
|
||||
return this.targetMap.size();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return this.targetMap.isEmpty();
|
||||
}
|
||||
|
||||
public boolean containsKey(Object key) {
|
||||
return this.targetMap.containsKey(key);
|
||||
}
|
||||
|
||||
public boolean containsValue(Object value) {
|
||||
return this.targetMap.containsValue(value);
|
||||
}
|
||||
|
||||
public List<V> get(Object key) {
|
||||
return this.targetMap.get(key);
|
||||
}
|
||||
|
||||
public List<V> put(K key, List<V> value) {
|
||||
return this.targetMap.put(key, value);
|
||||
}
|
||||
|
||||
public List<V> remove(Object key) {
|
||||
return this.targetMap.remove(key);
|
||||
}
|
||||
|
||||
public void putAll(Map<? extends K, ? extends List<V>> m) {
|
||||
this.targetMap.putAll(m);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.targetMap.clear();
|
||||
}
|
||||
|
||||
public Set<K> keySet() {
|
||||
return this.targetMap.keySet();
|
||||
}
|
||||
|
||||
public Collection<List<V>> values() {
|
||||
return this.targetMap.values();
|
||||
}
|
||||
|
||||
public Set<Entry<K, List<V>>> entrySet() {
|
||||
return this.targetMap.entrySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return this.targetMap.equals(obj);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.targetMap.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.targetMap.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,364 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.CollectionFactory;
|
||||
|
||||
/**
|
||||
* Represents an Internet Media Type, as defined in the HTTP specification.
|
||||
*
|
||||
* <p>Consists of a {@linkplain #getType() type} and a {@linkplain #getSubtype() subtype}. Also has functionality to
|
||||
* parse media types from a string using {@link #parseMediaType(String)}, or multiple comma-separated media types using
|
||||
* {@link #parseMediaTypes(String)}.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7">HTTP 1.1</a>
|
||||
* @since 3.0
|
||||
*/
|
||||
public final class MediaType implements Comparable<MediaType> {
|
||||
|
||||
public static final MediaType ALL = new MediaType();
|
||||
|
||||
private static final String PARAM_QUALITY_FACTORY = "q";
|
||||
|
||||
private static final String PARAM_CHARSET = "charset";
|
||||
|
||||
private static final String WILDCARD_TYPE = "*";
|
||||
|
||||
private final String type;
|
||||
|
||||
private final String subtype;
|
||||
|
||||
private final Map<String, String> parameters;
|
||||
|
||||
/**
|
||||
* Private constructor that creates a new {@link MediaType} representing <code>*/*</code>.
|
||||
*
|
||||
* @see #ALL
|
||||
*/
|
||||
private MediaType() {
|
||||
this(WILDCARD_TYPE, WILDCARD_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MediaType} for the given primary type. The {@linkplain #getSubtype() subtype} is set to
|
||||
* <code>*</code>, parameters empty.
|
||||
*
|
||||
* @param type the primary type
|
||||
*/
|
||||
public MediaType(String type) {
|
||||
this(type, WILDCARD_TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link MediaType} for the given primary type and subtype. The parameters are empty.
|
||||
*
|
||||
* @param type the primary type
|
||||
* @param subtype the subtype
|
||||
*/
|
||||
public MediaType(String type, String subtype) {
|
||||
this(type, subtype, Collections.<String, String>emptyMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MediaType} for the given type, subtype, and character set.
|
||||
*
|
||||
* @param type the primary type
|
||||
* @param subtype the subtype
|
||||
* @param charSet the character set
|
||||
*/
|
||||
public MediaType(String type, String subtype, Charset charSet) {
|
||||
this(type, subtype, Collections.singletonMap(PARAM_CHARSET, charSet.toString()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MediaType} for the given type, subtype, and parameters.
|
||||
*
|
||||
* @param type the primary type
|
||||
* @param subtype the subtype
|
||||
* @param parameters the parameters, mat be <code>null</code>
|
||||
*/
|
||||
public MediaType(String type, String subtype, Map<String, String> parameters) {
|
||||
Assert.hasText(type, "'type' must not be empty");
|
||||
Assert.hasText(subtype, "'subtype' must not be empty");
|
||||
this.type = type.toLowerCase(Locale.ENGLISH);
|
||||
this.subtype = subtype.toLowerCase(Locale.ENGLISH);
|
||||
if (!CollectionUtils.isEmpty(parameters)) {
|
||||
this.parameters = CollectionFactory.createLinkedCaseInsensitiveMapIfPossible(parameters.size());
|
||||
this.parameters.putAll(parameters);
|
||||
}
|
||||
else {
|
||||
this.parameters = Collections.emptyMap();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a single {@link MediaType}.
|
||||
*
|
||||
* @param mediaType the string to parse
|
||||
* @return the media type
|
||||
* @throws IllegalArgumentException if the string cannot be parsed
|
||||
*/
|
||||
public static MediaType parseMediaType(String mediaType) {
|
||||
Assert.hasLength(mediaType, "'mediaType' must not be empty");
|
||||
String[] parts = StringUtils.tokenizeToStringArray(mediaType, ";");
|
||||
|
||||
Map<String, String> parameters;
|
||||
if (parts.length <= 1) {
|
||||
parameters = null;
|
||||
}
|
||||
else {
|
||||
parameters = new LinkedHashMap<String, String>(parts.length - 1);
|
||||
}
|
||||
for (int i = 1; i < parts.length; i++) {
|
||||
String part = parts[i];
|
||||
int idx = part.indexOf('=');
|
||||
if (idx != -1) {
|
||||
String name = part.substring(0, idx);
|
||||
String value = part.substring(idx + 1, part.length());
|
||||
parameters.put(name, value);
|
||||
}
|
||||
}
|
||||
String fullType = parts[0].trim();
|
||||
|
||||
// java.net.HttpURLConnection returns a *; q=.2 Accept header
|
||||
if (WILDCARD_TYPE.equals(fullType)) {
|
||||
fullType = "*/*";
|
||||
}
|
||||
int idx = fullType.indexOf('/');
|
||||
String type = fullType.substring(0, idx);
|
||||
String subtype = fullType.substring(idx + 1, fullType.length());
|
||||
return new MediaType(type, subtype, parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given, comma-seperated string into a list of {@link MediaType} objects. This method can be used to parse
|
||||
* an Accept or Content-Type header.
|
||||
*
|
||||
* @param mediaTypes the string to parse
|
||||
* @return the list of media types
|
||||
* @throws IllegalArgumentException if the string cannot be parsed
|
||||
*/
|
||||
public static List<MediaType> parseMediaTypes(String mediaTypes) {
|
||||
Assert.hasLength(mediaTypes, "'mediaTypes' must not be empty");
|
||||
String[] tokens = mediaTypes.split(",\\s*");
|
||||
List<MediaType> result = new ArrayList<MediaType>(tokens.length);
|
||||
for (String token : tokens) {
|
||||
result.add(parseMediaType(token));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the primary type.
|
||||
*
|
||||
* @return the type
|
||||
*/
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the {@linkplain #getType() type} is the wildcard character <code>*</code> or not.
|
||||
*
|
||||
* @return whether the type is <code>*</code>
|
||||
*/
|
||||
public boolean isWildcardType() {
|
||||
return WILDCARD_TYPE.equals(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the subtype.
|
||||
*
|
||||
* @return the subtype
|
||||
*/
|
||||
public String getSubtype() {
|
||||
return subtype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether the {@linkplain #getSubtype() subtype} is the wildcard character <code>*</code> or not.
|
||||
*
|
||||
* @return whether the subtype is <code>*</code>
|
||||
*/
|
||||
public boolean isWildcardSubtype() {
|
||||
return WILDCARD_TYPE.equals(subtype);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the character set, as indicated by a <code>charset</code> parameter, if any.
|
||||
*
|
||||
* @return the character set; or <code>null</code> if not available
|
||||
*/
|
||||
public Charset getCharSet() {
|
||||
String charSet = parameters.get(PARAM_CHARSET);
|
||||
return charSet != null ? Charset.forName(charSet) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the quality value, as indicated by a <code>q</code> parameter, if any. Defaults to <code>1.0</code>.
|
||||
*
|
||||
* @return the quality factory
|
||||
*/
|
||||
public double getQualityValue() {
|
||||
String qualityFactory = parameters.get(PARAM_QUALITY_FACTORY);
|
||||
return qualityFactory != null ? Double.parseDouble(qualityFactory) : 1D;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a generic parameter value, given a parameter name.
|
||||
*
|
||||
* @param name the parameter name
|
||||
* @return the parameter value; or <code>null</code> if not present
|
||||
*/
|
||||
public String getParameter(String name) {
|
||||
return parameters.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether this {@link MediaType} includes the given media type. For instance, <code>text/*</code> includes
|
||||
* <code>text/plain</code>, <code>text/html</code>, etc.
|
||||
*
|
||||
* @param other the reference media type with which to compare
|
||||
* @return <code>true</code> if this media type includes the given media type; <code>false</code> otherwise
|
||||
*/
|
||||
public boolean includes(MediaType other) {
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (this.type.equals(other.type)) {
|
||||
if (this.subtype.equals(other.subtype) || isWildcardSubtype()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return isWildcardType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this {@link MediaType} to another. Sorting with this comparator follows the general rule: <blockquote>
|
||||
* audio/basic < audio/* < */* </blockquote>. That is, an explicit media type is sorted before an unspecific
|
||||
* media type. Quality parameters are also considered, so that <blockquote> audio/* < audio/*;q=0.7;
|
||||
* audio/*;q=0.3</blockquote>.
|
||||
*
|
||||
* @param other the media type to compare to
|
||||
* @return a negative integer, zero, or a positive integer as this media type is less than, equal to, or greater than
|
||||
* the specified media type
|
||||
*/
|
||||
public int compareTo(MediaType other) {
|
||||
double qVal1 = this.getQualityValue();
|
||||
double qVal2 = other.getQualityValue();
|
||||
int qComp = Double.compare(qVal2, qVal1);
|
||||
if (qComp != 0) {
|
||||
return qComp;
|
||||
}
|
||||
else if (this.isWildcardType() && !other.isWildcardType()) {
|
||||
return 1;
|
||||
}
|
||||
else if (other.isWildcardType() && !this.isWildcardType()) {
|
||||
return -1;
|
||||
}
|
||||
else if (!this.getType().equals(other.getType())) {
|
||||
return this.getType().compareTo(other.getType());
|
||||
}
|
||||
else { // mediaType1.getType().equals(mediaType2.getType())
|
||||
if (this.isWildcardSubtype() && !other.isWildcardSubtype()) {
|
||||
return 1;
|
||||
}
|
||||
else if (other.isWildcardSubtype() && !this.isWildcardSubtype()) {
|
||||
return -1;
|
||||
}
|
||||
else if (!this.getSubtype().equals(other.getSubtype())) {
|
||||
return this.getSubtype().compareTo(other.getSubtype());
|
||||
}
|
||||
else { // mediaType2.getSubtype().equals(mediaType2.getSubtype())
|
||||
double quality1 = this.getQualityValue();
|
||||
double quality2 = other.getQualityValue();
|
||||
return Double.compare(quality2, quality1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o != null && o instanceof MediaType) {
|
||||
MediaType other = (MediaType) o;
|
||||
return this.type.equals(other.type) && this.subtype.equals(other.subtype) &&
|
||||
this.parameters.equals(other.parameters);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = type.hashCode();
|
||||
result = 31 * result + subtype.hashCode();
|
||||
result = 31 * result + parameters.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
appendTo(builder);
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the given list of {@link MediaType} objects. This method can be used to for an
|
||||
* Accept or Content-Type header.
|
||||
*
|
||||
* @param mediaTypes the string to parse
|
||||
* @return the list of media types
|
||||
* @throws IllegalArgumentException if the string cannot be parsed
|
||||
*/
|
||||
public static String toString(List<MediaType> mediaTypes) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (Iterator<MediaType> iterator = mediaTypes.iterator(); iterator.hasNext();) {
|
||||
MediaType mediaType = iterator.next();
|
||||
mediaType.appendTo(builder);
|
||||
if (iterator.hasNext()) {
|
||||
builder.append(", ");
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private void appendTo(StringBuilder builder) {
|
||||
builder.append(type);
|
||||
builder.append('/');
|
||||
builder.append(subtype);
|
||||
for (Map.Entry<String, String> entry : parameters.entrySet()) {
|
||||
builder.append(';');
|
||||
builder.append(entry.getKey());
|
||||
builder.append('=');
|
||||
builder.append(entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,25 +28,22 @@ import java.util.Map;
|
||||
public interface MultiValueMap<K, V> extends Map<K, List<V>> {
|
||||
|
||||
/**
|
||||
* Adds the given single value to the current list of values for the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value to be added
|
||||
*/
|
||||
void add(K key, V value);
|
||||
|
||||
/**
|
||||
* Returns the first value for the given key.
|
||||
*
|
||||
* Return the first value for the given key.
|
||||
* @param key the key
|
||||
* @return the first value for the specified key, or <code>null</code>
|
||||
*/
|
||||
V getFirst(K key);
|
||||
|
||||
/**
|
||||
* Sets the given single value under the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* Add the given single value to the current list of values for the given key.
|
||||
* @param key the key
|
||||
* @param value the value to be added
|
||||
*/
|
||||
void add(K key, V value);
|
||||
|
||||
/**
|
||||
* Set the given single value under the given key.
|
||||
* @param key the key
|
||||
* @param value the value to set
|
||||
*/
|
||||
void set(K key, V value);
|
||||
|
||||
@@ -29,13 +29,13 @@ import org.junit.Test;
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class DefaultMultiValueMapTests {
|
||||
public class LinkedMultiValueMapTests {
|
||||
|
||||
private DefaultMultiValueMap<String, String> map;
|
||||
private LinkedMultiValueMap<String, String> map;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
map = new DefaultMultiValueMap<String, String>();
|
||||
map = new LinkedMultiValueMap<String, String>();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -71,7 +71,7 @@ public class DefaultMultiValueMapTests {
|
||||
public void equals() {
|
||||
map.set("key1", "value1");
|
||||
assertEquals(map, map);
|
||||
MultiValueMap<String, String> o1 = new DefaultMultiValueMap<String, String>();
|
||||
MultiValueMap<String, String> o1 = new LinkedMultiValueMap<String, String>();
|
||||
o1.set("key1", "value1");
|
||||
assertEquals(map, o1);
|
||||
assertEquals(o1, map);
|
||||
@@ -80,4 +80,5 @@ public class DefaultMultiValueMapTests {
|
||||
assertEquals(map, o2);
|
||||
assertEquals(o2, map);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class MediaTypeTest {
|
||||
|
||||
@Test
|
||||
public void includes() throws Exception {
|
||||
MediaType type1 = new MediaType("text", "plain");
|
||||
MediaType type2 = new MediaType("text", "plain");
|
||||
assertTrue("Equal types is not inclusive", type1.includes(type2));
|
||||
type1 = new MediaType("text");
|
||||
assertTrue("All subtypes is not inclusive", type1.includes(type2));
|
||||
type1 = MediaType.ALL;
|
||||
assertTrue("All types is not inclusive", type1.includes(type2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString() throws Exception {
|
||||
MediaType mediaType = new MediaType("text", "plain", Collections.singletonMap("q", "0.7"));
|
||||
String result = mediaType.toString();
|
||||
assertEquals("Invalid toString() returned", "text/plain;q=0.7", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMediaType() throws Exception {
|
||||
String s = "audio/*; q=0.2";
|
||||
MediaType mediaType = MediaType.parseMediaType(s);
|
||||
assertEquals("Invalid type", "audio", mediaType.getType());
|
||||
assertEquals("Invalid subtype", "*", mediaType.getSubtype());
|
||||
assertEquals("Invalid quality factor", 0.2D, mediaType.getQualityValue(), 0D);
|
||||
}
|
||||
|
||||
@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 parseMediaTypes() throws Exception {
|
||||
String s = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c";
|
||||
List<MediaType> mediaTypes = MediaType.parseMediaTypes(s);
|
||||
assertNotNull("No media types returned", mediaTypes);
|
||||
assertEquals("Invalid amount of media types", 4, mediaTypes.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compareTo() throws Exception {
|
||||
MediaType audioBasic = new MediaType("audio", "basic");
|
||||
MediaType audio = new MediaType("audio");
|
||||
MediaType audio03 = new MediaType("audio", "*", Collections.singletonMap("q", "0.3"));
|
||||
MediaType audio07 = new MediaType("audio", "*", Collections.singletonMap("q", "0.7"));
|
||||
MediaType all = MediaType.ALL;
|
||||
|
||||
// equal
|
||||
assertEquals("Invalid comparison result", 0, audioBasic.compareTo(audioBasic));
|
||||
assertEquals("Invalid comparison result", 0, audio.compareTo(audio));
|
||||
assertEquals("Invalid comparison result", 0, audio07.compareTo(audio07));
|
||||
|
||||
// specific to unspecific
|
||||
assertTrue("Invalid comparison result", audioBasic.compareTo(audio) < 0);
|
||||
assertTrue("Invalid comparison result", audioBasic.compareTo(all) < 0);
|
||||
assertTrue("Invalid comparison result", audio.compareTo(all) < 0);
|
||||
|
||||
// unspecific to specific
|
||||
assertTrue("Invalid comparison result", audio.compareTo(audioBasic) > 0);
|
||||
assertTrue("Invalid comparison result", all.compareTo(audioBasic) > 0);
|
||||
assertTrue("Invalid comparison result", all.compareTo(audio) > 0);
|
||||
|
||||
// qualifiers
|
||||
assertTrue("Invalid comparison result", audio.compareTo(audio07) < 0);
|
||||
assertTrue("Invalid comparison result", audio07.compareTo(audio03) < 0);
|
||||
assertTrue("Invalid comparison result", audio03.compareTo(all) > 0);
|
||||
|
||||
// sort
|
||||
List<MediaType> expected = new ArrayList<MediaType>();
|
||||
expected.add(audioBasic);
|
||||
expected.add(audio);
|
||||
expected.add(all);
|
||||
expected.add(audio07);
|
||||
expected.add(audio03);
|
||||
|
||||
List<MediaType> result = new ArrayList<MediaType>(expected);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Collections.shuffle(result);
|
||||
Collections.sort(result);
|
||||
|
||||
for (int j = 0; j < result.size(); j++) {
|
||||
assertEquals("Invalid media type at " + j, expected.get(j), result.get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user