polishing

This commit is contained in:
Juergen Hoeller
2011-12-06 22:50:48 +00:00
parent f8402e9f82
commit df7bdfa7c6
2 changed files with 22 additions and 21 deletions

View File

@@ -73,14 +73,14 @@ import org.springframework.web.util.WebUtils;
* media type. The default name of the parameter is <code>format</code> and it can be configured using the * media type. The default name of the parameter is <code>format</code> and it can be configured using the
* {@link #setParameterName(String) parameterName} property.</li> * {@link #setParameterName(String) parameterName} property.</li>
* <li>If there is no match in the {@link #setMediaTypes(Map) mediaTypes} property and if the Java Activation * <li>If there is no match in the {@link #setMediaTypes(Map) mediaTypes} property and if the Java Activation
* Framework (JAF) is both {@linkplain #setUseJaf enabled} and present on the class path, * Framework (JAF) is both {@linkplain #setUseJaf enabled} and present on the classpath,
* {@link FileTypeMap#getContentType(String)} is used instead.</li> * {@link FileTypeMap#getContentType(String)} is used instead.</li>
* <li>If the previous steps did not result in a media type, and * <li>If the previous steps did not result in a media type, and
* {@link #setIgnoreAcceptHeader ignoreAcceptHeader} is {@code false}, the request {@code Accept} header is * {@link #setIgnoreAcceptHeader ignoreAcceptHeader} is {@code false}, the request {@code Accept} header is
* used.</li> * used.</li>
* </ol> * </ol>
* *
* Once the requested media type has been determined, this resolver queries each delegate view resolver for a * <p>Once the requested media type has been determined, this resolver queries each delegate view resolver for a
* {@link View} and determines if the requested media type is {@linkplain MediaType#includes(MediaType) compatible} * {@link View} and determines if the requested media type is {@linkplain MediaType#includes(MediaType) compatible}
* with the view's {@linkplain View#getContentType() content type}). The most compatible view is returned. * with the view's {@linkplain View#getContentType() content type}). The most compatible view is returned.
* *
@@ -358,7 +358,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
* <p>The default implementation will check the {@linkplain #setMediaTypes(Map) media types} * <p>The default implementation will check the {@linkplain #setMediaTypes(Map) media types}
* property first for a defined mapping. If not present, and if the Java Activation Framework * property first for a defined mapping. If not present, and if the Java Activation Framework
* can be found on the classpath, it will call {@link FileTypeMap#getContentType(String)} * can be found on the classpath, it will call {@link FileTypeMap#getContentType(String)}
* <p>This method can be overriden to provide a different algorithm. * <p>This method can be overridden to provide a different algorithm.
* @param filename the current request file name (i.e. {@code hotels.html}) * @param filename the current request file name (i.e. {@code hotels.html})
* @return the media type, if any * @return the media type, if any
*/ */
@@ -454,6 +454,18 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
} }
private static final View NOT_ACCEPTABLE_VIEW = new View() {
public String getContentType() {
return null;
}
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
}
};
/** /**
* Inner class to avoid hard-coded JAF dependency. * Inner class to avoid hard-coded JAF dependency.
*/ */
@@ -497,22 +509,10 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
return FileTypeMap.getDefaultFileTypeMap(); return FileTypeMap.getDefaultFileTypeMap();
} }
public static MediaType getMediaType(String fileName) { public static MediaType getMediaType(String filename) {
String mediaType = fileTypeMap.getContentType(fileName); String mediaType = fileTypeMap.getContentType(filename);
return (StringUtils.hasText(mediaType) ? MediaType.parseMediaType(mediaType) : null); return (StringUtils.hasText(mediaType) ? MediaType.parseMediaType(mediaType) : null);
} }
} }
private static final View NOT_ACCEPTABLE_VIEW = new View() {
public String getContentType() {
return null;
}
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
}
};
} }

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2010 the original author or authors. * Copyright 2002-2011 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.
@@ -37,9 +37,9 @@ import org.springframework.util.StringUtils;
/** /**
* Implementation of {@link HttpMessageConverter} that can read and write {@link Resource Resources}. * Implementation of {@link HttpMessageConverter} that can read and write {@link Resource Resources}.
* *
* <p>By default, this converter can read all media types. The Java Activation Framework (JAF) - if available - is used * <p>By default, this converter can read all media types. The Java Activation Framework (JAF) -
* to determine the {@code Content-Type} of written resources. If JAF is not available, {@code application/octet-stream} * if available - is used to determine the {@code Content-Type} of written resources.
* is used. * If JAF is not available, {@code application/octet-stream} is used.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @since 3.0.2 * @since 3.0.2
@@ -49,6 +49,7 @@ public class ResourceHttpMessageConverter implements HttpMessageConverter<Resour
private static final boolean jafPresent = private static final boolean jafPresent =
ClassUtils.isPresent("javax.activation.FileTypeMap", ResourceHttpMessageConverter.class.getClassLoader()); ClassUtils.isPresent("javax.activation.FileTypeMap", ResourceHttpMessageConverter.class.getClassLoader());
public boolean canRead(Class<?> clazz, MediaType mediaType) { public boolean canRead(Class<?> clazz, MediaType mediaType) {
return Resource.class.isAssignableFrom(clazz); return Resource.class.isAssignableFrom(clazz);
} }