Browse Source

polishing

3.0.x
Juergen Hoeller 14 years ago
parent
commit
df7bdfa7c6
  1. 34
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java
  2. 9
      org.springframework.web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

34
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/ContentNegotiatingViewResolver.java

@ -73,14 +73,14 @@ import org.springframework.web.util.WebUtils; @@ -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
* {@link #setParameterName(String) parameterName} property.</li>
* <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>
* <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
* used.</li>
* </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}
* with the view's {@linkplain View#getContentType() content type}). The most compatible view is returned.
*
@ -358,7 +358,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport @@ -358,7 +358,7 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
* <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
* 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})
* @return the media type, if any
*/
@ -454,6 +454,18 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport @@ -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.
*/
@ -497,22 +509,10 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport @@ -497,22 +509,10 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
return FileTypeMap.getDefaultFileTypeMap();
}
public static MediaType getMediaType(String fileName) {
String mediaType = fileTypeMap.getContentType(fileName);
public static MediaType getMediaType(String filename) {
String mediaType = fileTypeMap.getContentType(filename);
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);
}
};
}

9
org.springframework.web/src/main/java/org/springframework/http/converter/ResourceHttpMessageConverter.java

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

Loading…
Cancel
Save