ConversionService fully supports conversion from String to MediaType now (through 'valueOf'; SPR-7282); revised exception handling in ObjectToObjectConverter, avoiding InvocationTargetExceptions

This commit is contained in:
Juergen Hoeller 2010-06-14 23:23:49 +00:00
parent 902938e95f
commit 96b1dc9db4
4 changed files with 78 additions and 76 deletions

View File

@ -40,7 +40,7 @@ public final class ConversionFailedException extends ConversionException {
* @param cause the cause of the conversion failure * @param cause the cause of the conversion failure
*/ */
public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) { public ConversionFailedException(TypeDescriptor sourceType, TypeDescriptor targetType, Object value, Throwable cause) {
super("Unable to convert value " + value + " from type '" + sourceType.getName() + super("Unable to convert value \"" + value + "\" from type '" + sourceType.getName() +
"' to type '" + targetType.getName() + "'", cause); "' to type '" + targetType.getName() + "'", cause);
this.sourceType = sourceType; this.sourceType = sourceType;
this.targetType = targetType; this.targetType = targetType;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -54,37 +54,27 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Class<?> sourceClass = sourceType.getObjectType(); Class<?> sourceClass = sourceType.getObjectType();
Class<?> targetClass = targetType.getObjectType(); Class<?> targetClass = targetType.getObjectType();
Object target;
Method method = getValueOfMethodOn(targetClass, sourceClass); Method method = getValueOfMethodOn(targetClass, sourceClass);
if (method != null) { try {
ReflectionUtils.makeAccessible(method); if (method != null) {
target = ReflectionUtils.invokeMethod(method, null, source); ReflectionUtils.makeAccessible(method);
} return method.invoke(null, source);
else {
Constructor<?> constructor = getConstructor(targetClass, sourceClass);
if (constructor != null) {
try {
target = constructor.newInstance(source);
}
catch (IllegalArgumentException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
catch (InstantiationException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
catch (IllegalAccessException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
catch (InvocationTargetException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
} }
else { else {
throw new IllegalStateException("No static valueOf(" + sourceClass.getName() + Constructor<?> constructor = getConstructor(targetClass, sourceClass);
") method or Constructor(" + sourceClass.getName() + ") exists on " + targetClass.getName()); if (constructor != null) {
return constructor.newInstance(source);
}
} }
} }
return target; catch (InvocationTargetException ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex.getTargetException());
}
catch (Throwable ex) {
throw new ConversionFailedException(sourceType, targetType, source, ex);
}
throw new IllegalStateException("No static valueOf(" + sourceClass.getName() +
") method or Constructor(" + sourceClass.getName() + ") exists on " + targetClass.getName());
} }

View File

@ -37,14 +37,14 @@ import org.springframework.util.StringUtils;
/** /**
* Represents an Internet Media Type, as defined in the HTTP specification. * 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 * <p>Consists of a {@linkplain #getType() type} and a {@linkplain #getSubtype() subtype}.
* parse media types from a string using {@link #parseMediaType(String)}, or multiple comma-separated media types using * Also has functionality to parse media types from a string using {@link #parseMediaType(String)},
* {@link #parseMediaTypes(String)}. * or multiple comma-separated media types using {@link #parseMediaTypes(String)}.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Juergen Hoeller * @author Juergen Hoeller
* @see <a href="http://tools.ietf.org/html/rfc2616#section-3.7">HTTP 1.1, section 3.7</a>
* @since 3.0 * @since 3.0
* @see <a href="http://tools.ietf.org/html/rfc2616#section-3.7">HTTP 1.1, section 3.7</a>
*/ */
public class MediaType implements Comparable<MediaType> { public class MediaType implements Comparable<MediaType> {
@ -118,6 +118,7 @@ public class MediaType implements Comparable<MediaType> {
* */ * */
public final static MediaType TEXT_XML; public final static MediaType TEXT_XML;
private static final BitSet TOKEN; private static final BitSet TOKEN;
private static final String WILDCARD_TYPE = "*"; private static final String WILDCARD_TYPE = "*";
@ -184,11 +185,10 @@ public class MediaType implements Comparable<MediaType> {
TEXT_XML = new MediaType("text","xml"); TEXT_XML = new MediaType("text","xml");
} }
/** /**
* Create a new {@link MediaType} for the given primary type. * Create a new {@link MediaType} for the given primary type.
*
* <p>The {@linkplain #getSubtype() subtype} is set to <code>&#42;</code>, parameters empty. * <p>The {@linkplain #getSubtype() subtype} is set to <code>&#42;</code>, parameters empty.
*
* @param type the primary type * @param type the primary type
* @throws IllegalArgumentException if any of the parameters contain illegal characters * @throws IllegalArgumentException if any of the parameters contain illegal characters
*/ */
@ -197,8 +197,8 @@ public class MediaType implements Comparable<MediaType> {
} }
/** /**
* Create a new {@link MediaType} for the given primary type and subtype. <p>The parameters are empty. * Create a new {@link MediaType} for the given primary type and subtype.
* * <p>The parameters are empty.
* @param type the primary type * @param type the primary type
* @param subtype the subtype * @param subtype the subtype
* @throws IllegalArgumentException if any of the parameters contain illegal characters * @throws IllegalArgumentException if any of the parameters contain illegal characters
@ -209,7 +209,6 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Create a new {@link MediaType} for the given type, subtype, and character set. * Create a new {@link MediaType} for the given type, subtype, and character set.
*
* @param type the primary type * @param type the primary type
* @param subtype the subtype * @param subtype the subtype
* @param charSet the character set * @param charSet the character set
@ -232,9 +231,8 @@ public class MediaType implements Comparable<MediaType> {
} }
/** /**
* Copy-constructor that copies the type and subtype of the given {@link MediaType}, and allows for different * Copy-constructor that copies the type and subtype of the given {@link MediaType},
* parameter. * and allows for different parameter.
*
* @param other the other media type * @param other the other media type
* @param parameters the parameters, may be <code>null</code> * @param parameters the parameters, may be <code>null</code>
* @throws IllegalArgumentException if any of the parameters contain illegal characters * @throws IllegalArgumentException if any of the parameters contain illegal characters
@ -245,7 +243,6 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Create a new {@link MediaType} for the given type, subtype, and parameters. * Create a new {@link MediaType} for the given type, subtype, and parameters.
*
* @param type the primary type * @param type the primary type
* @param subtype the subtype * @param subtype the subtype
* @param parameters the parameters, may be <code>null</code> * @param parameters the parameters, may be <code>null</code>
@ -275,7 +272,6 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Checks the given token string for illegal characters, as defined in RFC 2616, section 2.2. * Checks the given token string for illegal characters, as defined in RFC 2616, section 2.2.
*
* @throws IllegalArgumentException in case of illegal characters * @throws IllegalArgumentException in case of illegal characters
* @see <a href="http://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a> * @see <a href="http://tools.ietf.org/html/rfc2616#section-2.2">HTTP 1.1, section 2.2</a>
*/ */
@ -295,7 +291,8 @@ public class MediaType implements Comparable<MediaType> {
if (PARAM_QUALITY_FACTOR.equals(attribute)) { if (PARAM_QUALITY_FACTOR.equals(attribute)) {
value = unquote(value); value = unquote(value);
double d = Double.parseDouble(value); double d = Double.parseDouble(value);
Assert.isTrue(d >= 0D && d <= 1D, "Invalid quality value \"" + value + "\": should be between 0.0 and 1.0"); Assert.isTrue(d >= 0D && d <= 1D,
"Invalid quality value \"" + value + "\": should be between 0.0 and 1.0");
} }
else if (PARAM_CHARSET.equals(attribute)) { else if (PARAM_CHARSET.equals(attribute)) {
value = unquote(value); value = unquote(value);
@ -317,24 +314,29 @@ public class MediaType implements Comparable<MediaType> {
return isQuotedString(s) ? s.substring(1, s.length() - 1) : s; return isQuotedString(s) ? s.substring(1, s.length() - 1) : s;
} }
/** Return the primary type. */ /**
* Return the primary type.
*/
public String getType() { public String getType() {
return this.type; return this.type;
} }
/** Indicate whether the {@linkplain #getType() type} is the wildcard character <code>&#42;</code> or not. */ /**
* Indicate whether the {@linkplain #getType() type} is the wildcard character <code>&#42;</code> or not.
*/
public boolean isWildcardType() { public boolean isWildcardType() {
return WILDCARD_TYPE.equals(type); return WILDCARD_TYPE.equals(type);
} }
/** Return the subtype. */ /**
* Return the subtype.
*/
public String getSubtype() { public String getSubtype() {
return this.subtype; return this.subtype;
} }
/** /**
* Indicate whether the {@linkplain #getSubtype() subtype} is the wildcard character <code>&#42;</code> or not. * Indicate whether the {@linkplain #getSubtype() subtype} is the wildcard character <code>&#42;</code> or not.
*
* @return whether the subtype is <code>&#42;</code> * @return whether the subtype is <code>&#42;</code>
*/ */
public boolean isWildcardSubtype() { public boolean isWildcardSubtype() {
@ -343,7 +345,6 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Return the character set, as indicated by a <code>charset</code> parameter, if any. * Return the character set, as indicated by a <code>charset</code> parameter, if any.
*
* @return the character set; or <code>null</code> if not available * @return the character set; or <code>null</code> if not available
*/ */
public Charset getCharSet() { public Charset getCharSet() {
@ -352,8 +353,8 @@ public class MediaType implements Comparable<MediaType> {
} }
/** /**
* Return the quality value, as indicated by a <code>q</code> parameter, if any. Defaults to <code>1.0</code>. * Return the quality value, as indicated by a <code>q</code> parameter, if any.
* * Defaults to <code>1.0</code>.
* @return the quality factory * @return the quality factory
*/ */
public double getQualityValue() { public double getQualityValue() {
@ -363,7 +364,6 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Return a generic parameter value, given a parameter name. * Return a generic parameter value, given a parameter name.
*
* @param name the parameter name * @param name the parameter name
* @return the parameter value; or <code>null</code> if not present * @return the parameter value; or <code>null</code> if not present
*/ */
@ -373,10 +373,8 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Indicate whether this {@link MediaType} includes the given media type. * Indicate whether this {@link MediaType} includes the given media type.
*
* <p>For instance, {@code text/*} includes {@code text/plain}, {@code text/html}, and {@code application/*+xml} * <p>For instance, {@code text/*} includes {@code text/plain}, {@code text/html}, and {@code application/*+xml}
* includes {@code application/soap+xml}, etc. This method is non-symmetic. * includes {@code application/soap+xml}, etc. This method is non-symmetic.
*
* @param other the reference media type with which to compare * @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 * @return <code>true</code> if this media type includes the given media type; <code>false</code> otherwise
*/ */
@ -410,10 +408,8 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Indicate whether this {@link MediaType} is compatible with the given media type. * Indicate whether this {@link MediaType} is compatible with the given media type.
* * <p>For instance, {@code text/*} is compatible with {@code text/plain}, {@code text/html}, and vice versa.
* <p>For instance, {@code text/*} is compatible with {@code text/plain}, {@code text/html}, and vice versa. In * In effect, this method is similar to {@link #includes(MediaType)}, except that it's symmetric.
* effect, this method is similar to {@link #includes(MediaType)}, except that it's symmetric.
*
* @param other the reference media type with which to compare * @param other the reference media type with which to compare
* @return <code>true</code> if this media type is compatible with the given media type; <code>false</code> otherwise * @return <code>true</code> if this media type is compatible with the given media type; <code>false</code> otherwise
*/ */
@ -449,7 +445,6 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Compares this {@link MediaType} to another alphabetically. * Compares this {@link MediaType} to another alphabetically.
*
* @param other media type to compare to * @param other media type to compare to
* @see #sortBySpecificity(List) * @see #sortBySpecificity(List)
*/ */
@ -527,7 +522,7 @@ public class MediaType implements Comparable<MediaType> {
appendTo(this.parameters, builder); appendTo(this.parameters, builder);
} }
private static void appendTo(Map<String, String> map, StringBuilder builder) { private void appendTo(Map<String, String> map, StringBuilder builder) {
for (Map.Entry<String, String> entry : map.entrySet()) { for (Map.Entry<String, String> entry : map.entrySet()) {
builder.append(';'); builder.append(';');
builder.append(entry.getKey()); builder.append(entry.getKey());
@ -536,9 +531,19 @@ public class MediaType implements Comparable<MediaType> {
} }
} }
/**
* Parse the given String value into a {@link MediaType} object,
* with this method name following the 'valueOf' naming convention
* (as supported by {@link org.springframework.core.convert.ConversionService}.
* @see #parseMediaType(String)
*/
public static MediaType valueOf(String value) {
return parseMediaType(value);
}
/** /**
* Parse the given String into a single {@link MediaType}. * Parse the given String into a single {@link MediaType}.
*
* @param mediaType the string to parse * @param mediaType the string to parse
* @return the media type * @return the media type
* @throws IllegalArgumentException if the string cannot be parsed * @throws IllegalArgumentException if the string cannot be parsed
@ -581,9 +586,8 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Parse the given, comma-seperated string into a list of {@link MediaType} objects. <p>This method can be used to * Parse the given, comma-seperated string into a list of {@link MediaType} objects.
* parse an Accept or Content-Type header. * <p>This method can be used to parse an Accept or Content-Type header.
*
* @param mediaTypes the string to parse * @param mediaTypes the string to parse
* @return the list of media types * @return the list of media types
* @throws IllegalArgumentException if the string cannot be parsed * @throws IllegalArgumentException if the string cannot be parsed
@ -602,9 +606,7 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Return a string representation of the given list of {@link MediaType} objects. * Return a string representation of the given list of {@link MediaType} objects.
*
* <p>This method can be used to for an {@code Accept} or {@code Content-Type} header. * <p>This method can be used to for an {@code Accept} or {@code Content-Type} header.
*
* @param mediaTypes the string to parse * @param mediaTypes the string to parse
* @return the list of media types * @return the list of media types
* @throws IllegalArgumentException if the String cannot be parsed * @throws IllegalArgumentException if the String cannot be parsed
@ -623,7 +625,6 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Sorts the given list of {@link MediaType} objects by specificity. * Sorts the given list of {@link MediaType} objects by specificity.
*
* <p>Given two media types: * <p>Given two media types:
* <ol> * <ol>
* <li>if either media type has a {@linkplain #isWildcardType() wildcard type}, then the media type without the * <li>if either media type has a {@linkplain #isWildcardType() wildcard type}, then the media type without the
@ -639,14 +640,12 @@ public class MediaType implements Comparable<MediaType> {
* <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the * <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the
* media type with the most parameters is ordered before the other.</li> * media type with the most parameters is ordered before the other.</li>
* </ol> * </ol>
*
* <p>For example: * <p>For example:
* <blockquote>audio/basic &lt; audio/* &lt; *&#047;*</blockquote> * <blockquote>audio/basic &lt; audio/* &lt; *&#047;*</blockquote>
* <blockquote>audio/* &lt; audio/*;q=0.7; audio/*;q=0.3</blockquote> * <blockquote>audio/* &lt; audio/*;q=0.7; audio/*;q=0.3</blockquote>
* <blockquote>audio/basic;level=1 &lt; audio/basic</blockquote> * <blockquote>audio/basic;level=1 &lt; audio/basic</blockquote>
* <blockquote>audio/basic == text/html</blockquote> * <blockquote>audio/basic == text/html</blockquote>
* <blockquote>audio/basic == audio/wave</blockquote> * <blockquote>audio/basic == audio/wave</blockquote>
*
* @param mediaTypes the list of media types to be sorted * @param mediaTypes the list of media types to be sorted
* @see <a href="http://tools.ietf.org/html/rfc2616#section-14.1">HTTP 1.1, section 14.1</a> * @see <a href="http://tools.ietf.org/html/rfc2616#section-14.1">HTTP 1.1, section 14.1</a>
*/ */
@ -659,7 +658,6 @@ public class MediaType implements Comparable<MediaType> {
/** /**
* Sorts the given list of {@link MediaType} objects by quality value. * Sorts the given list of {@link MediaType} objects by quality value.
*
* <p>Given two media types: * <p>Given two media types:
* <ol> * <ol>
* <li>if the two media types have different {@linkplain #getQualityValue() quality value}, then the media type * <li>if the two media types have different {@linkplain #getQualityValue() quality value}, then the media type
@ -675,7 +673,6 @@ public class MediaType implements Comparable<MediaType> {
* <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the * <li>if the two media types have a different amount of {@linkplain #getParameter(String) parameters}, then the
* media type with the most parameters is ordered before the other.</li> * media type with the most parameters is ordered before the other.</li>
* </ol> * </ol>
*
* @param mediaTypes the list of media types to be sorted * @param mediaTypes the list of media types to be sorted
* @see #getQualityValue() * @see #getQualityValue()
*/ */
@ -686,6 +683,7 @@ public class MediaType implements Comparable<MediaType> {
} }
} }
static final Comparator<MediaType> SPECIFICITY_COMPARATOR = new Comparator<MediaType>() { static final Comparator<MediaType> SPECIFICITY_COMPARATOR = new Comparator<MediaType>() {
public int compare(MediaType mediaType1, MediaType mediaType2) { public int compare(MediaType mediaType1, MediaType mediaType2) {
@ -714,7 +712,8 @@ public class MediaType implements Comparable<MediaType> {
int qualityComparison = Double.compare(quality2, quality1); int qualityComparison = Double.compare(quality2, quality1);
if (qualityComparison != 0) { if (qualityComparison != 0) {
return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3 return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3
} else { }
else {
int paramsSize1 = mediaType1.parameters.size(); int paramsSize1 = mediaType1.parameters.size();
int paramsSize2 = mediaType2.parameters.size(); int paramsSize2 = mediaType2.parameters.size();
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic
@ -724,6 +723,7 @@ public class MediaType implements Comparable<MediaType> {
} }
}; };
static final Comparator<MediaType> QUALITY_VALUE_COMPARATOR = new Comparator<MediaType>() { static final Comparator<MediaType> QUALITY_VALUE_COMPARATOR = new Comparator<MediaType>() {
public int compare(MediaType mediaType1, MediaType mediaType2) { public int compare(MediaType mediaType1, MediaType mediaType2) {
@ -751,7 +751,8 @@ public class MediaType implements Comparable<MediaType> {
} }
else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/basic == audio/wave else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/basic == audio/wave
return 0; return 0;
} else { }
else {
int paramsSize1 = mediaType1.parameters.size(); int paramsSize1 = mediaType1.parameters.size();
int paramsSize2 = mediaType2.parameters.size(); int paramsSize2 = mediaType2.parameters.size();
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic
@ -759,4 +760,5 @@ public class MediaType implements Comparable<MediaType> {
} }
} }
}; };
} }

View File

@ -26,7 +26,13 @@ import java.util.Random;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.junit.Test; import org.junit.Test;
/** @author Arjen Poutsma */ import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.ConversionServiceFactory;
/**
* @author Arjen Poutsma
* @author Juergen Hoeller
*/
public class MediaTypeTests { public class MediaTypeTests {
@Test @Test
@ -480,10 +486,14 @@ public class MediaTypeTests {
for (int i = 0; i < result.size(); i++) { for (int i = 0; i < result.size(); i++) {
assertSame("Invalid media type at " + i, expected.get(i), result.get(i)); assertSame("Invalid media type at " + i, expected.get(i), result.get(i));
} }
} }
@Test
public void testWithConversionService() {
ConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
assertTrue(conversionService.canConvert(String.class, MediaType.class));
MediaType mediaType = MediaType.parseMediaType("application/xml");
assertEquals(mediaType, conversionService.convert("application/xml", MediaType.class));
}
} }