Browse Source

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

pull/1234/head
Juergen Hoeller 15 years ago
parent
commit
96b1dc9db4
  1. 2
      org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java
  2. 44
      org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java
  3. 90
      org.springframework.web/src/main/java/org/springframework/http/MediaType.java
  4. 18
      org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java

2
org.springframework.core/src/main/java/org/springframework/core/convert/ConversionFailedException.java

@ -40,7 +40,7 @@ public final class ConversionFailedException extends ConversionException { @@ -40,7 +40,7 @@ public final class ConversionFailedException extends ConversionException {
* @param cause the cause of the conversion failure
*/
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);
this.sourceType = sourceType;
this.targetType = targetType;

44
org.springframework.core/src/main/java/org/springframework/core/convert/support/ObjectToObjectConverter.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -54,37 +54,27 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter { @@ -54,37 +54,27 @@ final class ObjectToObjectConverter implements ConditionalGenericConverter {
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
Class<?> sourceClass = sourceType.getObjectType();
Class<?> targetClass = targetType.getObjectType();
Object target;
Method method = getValueOfMethodOn(targetClass, sourceClass);
if (method != null) {
ReflectionUtils.makeAccessible(method);
target = ReflectionUtils.invokeMethod(method, 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);
}
try {
if (method != null) {
ReflectionUtils.makeAccessible(method);
return method.invoke(null, source);
}
else {
throw new IllegalStateException("No static valueOf(" + sourceClass.getName() +
") method or Constructor(" + sourceClass.getName() + ") exists on " + targetClass.getName());
Constructor<?> constructor = getConstructor(targetClass, sourceClass);
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());
}

90
org.springframework.web/src/main/java/org/springframework/http/MediaType.java

@ -37,14 +37,14 @@ import org.springframework.util.StringUtils; @@ -37,14 +37,14 @@ import org.springframework.util.StringUtils;
/**
* 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)}.
* <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
* @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
* @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> {
@ -118,6 +118,7 @@ public class MediaType implements Comparable<MediaType> { @@ -118,6 +118,7 @@ public class MediaType implements Comparable<MediaType> {
* */
public final static MediaType TEXT_XML;
private static final BitSet TOKEN;
private static final String WILDCARD_TYPE = "*";
@ -184,11 +185,10 @@ public class MediaType implements Comparable<MediaType> { @@ -184,11 +185,10 @@ public class MediaType implements Comparable<MediaType> {
TEXT_XML = new MediaType("text","xml");
}
/**
* Create a new {@link MediaType} for the given primary type.
*
* <p>The {@linkplain #getSubtype() subtype} is set to <code>&#42;</code>, parameters empty.
*
* @param type the primary type
* @throws IllegalArgumentException if any of the parameters contain illegal characters
*/
@ -197,8 +197,8 @@ public class MediaType implements Comparable<MediaType> { @@ -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 subtype the subtype
* @throws IllegalArgumentException if any of the parameters contain illegal characters
@ -209,7 +209,6 @@ public class MediaType implements Comparable<MediaType> { @@ -209,7 +209,6 @@ public class MediaType implements Comparable<MediaType> {
/**
* Create 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
@ -232,9 +231,8 @@ public class MediaType implements Comparable<MediaType> { @@ -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
* parameter.
*
* Copy-constructor that copies the type and subtype of the given {@link MediaType},
* and allows for different parameter.
* @param other the other media type
* @param parameters the parameters, may be <code>null</code>
* @throws IllegalArgumentException if any of the parameters contain illegal characters
@ -245,7 +243,6 @@ public class MediaType implements Comparable<MediaType> { @@ -245,7 +243,6 @@ public class MediaType implements Comparable<MediaType> {
/**
* Create a new {@link MediaType} for the given type, subtype, and parameters.
*
* @param type the primary type
* @param subtype the subtype
* @param parameters the parameters, may be <code>null</code>
@ -275,7 +272,6 @@ public class MediaType implements Comparable<MediaType> { @@ -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.
*
* @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>
*/
@ -295,7 +291,8 @@ public class MediaType implements Comparable<MediaType> { @@ -295,7 +291,8 @@ public class MediaType implements Comparable<MediaType> {
if (PARAM_QUALITY_FACTOR.equals(attribute)) {
value = unquote(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)) {
value = unquote(value);
@ -317,24 +314,29 @@ public class MediaType implements Comparable<MediaType> { @@ -317,24 +314,29 @@ public class MediaType implements Comparable<MediaType> {
return isQuotedString(s) ? s.substring(1, s.length() - 1) : s;
}
/** Return the primary type. */
/**
* Return the primary type.
*/
public String getType() {
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() {
return WILDCARD_TYPE.equals(type);
}
/** Return the subtype. */
/**
* Return the subtype.
*/
public String getSubtype() {
return this.subtype;
}
/**
* Indicate whether the {@linkplain #getSubtype() subtype} is the wildcard character <code>&#42;</code> or not.
*
* @return whether the subtype is <code>&#42;</code>
*/
public boolean isWildcardSubtype() {
@ -343,7 +345,6 @@ public class MediaType implements Comparable<MediaType> { @@ -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; or <code>null</code> if not available
*/
public Charset getCharSet() {
@ -352,8 +353,8 @@ public class MediaType implements Comparable<MediaType> { @@ -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
*/
public double getQualityValue() {
@ -363,7 +364,6 @@ public class MediaType implements Comparable<MediaType> { @@ -363,7 +364,6 @@ public class MediaType implements Comparable<MediaType> {
/**
* Return a generic parameter value, given a parameter name.
*
* @param name the parameter name
* @return the parameter value; or <code>null</code> if not present
*/
@ -373,10 +373,8 @@ public class MediaType implements Comparable<MediaType> { @@ -373,10 +373,8 @@ public class MediaType implements Comparable<MediaType> {
/**
* 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}
* includes {@code application/soap+xml}, etc. This method is non-symmetic.
*
* @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
*/
@ -410,10 +408,8 @@ public class MediaType implements Comparable<MediaType> { @@ -410,10 +408,8 @@ public class MediaType implements Comparable<MediaType> {
/**
* 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. In
* effect, this method is similar to {@link #includes(MediaType)}, except that it's symmetric.
*
* <p>For instance, {@code text/*} is compatible with {@code text/plain}, {@code text/html}, and vice versa.
* In effect, this method is similar to {@link #includes(MediaType)}, except that it's symmetric.
* @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
*/
@ -449,7 +445,6 @@ public class MediaType implements Comparable<MediaType> { @@ -449,7 +445,6 @@ public class MediaType implements Comparable<MediaType> {
/**
* Compares this {@link MediaType} to another alphabetically.
*
* @param other media type to compare to
* @see #sortBySpecificity(List)
*/
@ -527,7 +522,7 @@ public class MediaType implements Comparable<MediaType> { @@ -527,7 +522,7 @@ public class MediaType implements Comparable<MediaType> {
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()) {
builder.append(';');
builder.append(entry.getKey());
@ -536,9 +531,19 @@ public class MediaType implements Comparable<MediaType> { @@ -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}.
*
* @param mediaType the string to parse
* @return the media type
* @throws IllegalArgumentException if the string cannot be parsed
@ -581,9 +586,8 @@ public class MediaType implements Comparable<MediaType> { @@ -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 an Accept or Content-Type header.
*
* Parse the given, comma-seperated string into a list of {@link MediaType} objects.
* <p>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
@ -602,9 +606,7 @@ public class MediaType implements Comparable<MediaType> { @@ -602,9 +606,7 @@ public class MediaType implements Comparable<MediaType> {
/**
* 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.
*
* @param mediaTypes the string to parse
* @return the list of media types
* @throws IllegalArgumentException if the String cannot be parsed
@ -623,7 +625,6 @@ public class MediaType implements Comparable<MediaType> { @@ -623,7 +625,6 @@ public class MediaType implements Comparable<MediaType> {
/**
* Sorts the given list of {@link MediaType} objects by specificity.
*
* <p>Given two media types:
* <ol>
* <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> { @@ -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
* media type with the most parameters is ordered before the other.</li>
* </ol>
*
* <p>For example:
* <blockquote>audio/basic &lt; audio/* &lt; *&#047;*</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 == text/html</blockquote>
* <blockquote>audio/basic == audio/wave</blockquote>
*
* @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>
*/
@ -659,7 +658,6 @@ public class MediaType implements Comparable<MediaType> { @@ -659,7 +658,6 @@ public class MediaType implements Comparable<MediaType> {
/**
* Sorts the given list of {@link MediaType} objects by quality value.
*
* <p>Given two media types:
* <ol>
* <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> { @@ -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
* media type with the most parameters is ordered before the other.</li>
* </ol>
*
* @param mediaTypes the list of media types to be sorted
* @see #getQualityValue()
*/
@ -686,6 +683,7 @@ public class MediaType implements Comparable<MediaType> { @@ -686,6 +683,7 @@ public class MediaType implements Comparable<MediaType> {
}
}
static final Comparator<MediaType> SPECIFICITY_COMPARATOR = new Comparator<MediaType>() {
public int compare(MediaType mediaType1, MediaType mediaType2) {
@ -714,7 +712,8 @@ public class MediaType implements Comparable<MediaType> { @@ -714,7 +712,8 @@ public class MediaType implements Comparable<MediaType> {
int qualityComparison = Double.compare(quality2, quality1);
if (qualityComparison != 0) {
return qualityComparison; // audio/*;q=0.7 < audio/*;q=0.3
} else {
}
else {
int paramsSize1 = mediaType1.parameters.size();
int paramsSize2 = mediaType2.parameters.size();
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic
@ -724,6 +723,7 @@ public class MediaType implements Comparable<MediaType> { @@ -724,6 +723,7 @@ public class MediaType implements Comparable<MediaType> {
}
};
static final Comparator<MediaType> QUALITY_VALUE_COMPARATOR = new Comparator<MediaType>() {
public int compare(MediaType mediaType1, MediaType mediaType2) {
@ -751,7 +751,8 @@ public class MediaType implements Comparable<MediaType> { @@ -751,7 +751,8 @@ public class MediaType implements Comparable<MediaType> {
}
else if (!mediaType1.getSubtype().equals(mediaType2.getSubtype())) { // audio/basic == audio/wave
return 0;
} else {
}
else {
int paramsSize1 = mediaType1.parameters.size();
int paramsSize2 = mediaType2.parameters.size();
return (paramsSize2 < paramsSize1 ? -1 : (paramsSize2 == paramsSize1 ? 0 : 1)); // audio/basic;level=1 < audio/basic
@ -759,4 +760,5 @@ public class MediaType implements Comparable<MediaType> { @@ -759,4 +760,5 @@ public class MediaType implements Comparable<MediaType> {
}
}
};
}

18
org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java

@ -26,7 +26,13 @@ import java.util.Random; @@ -26,7 +26,13 @@ import java.util.Random;
import static org.junit.Assert.*;
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 {
@Test
@ -480,10 +486,14 @@ public class MediaTypeTests { @@ -480,10 +486,14 @@ public class MediaTypeTests {
for (int i = 0; i < result.size(); 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));
}
}

Loading…
Cancel
Save