From 7f1e5a49a4494a313a26a6dcf90b5b90313cfa59 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 22 Nov 2014 18:05:35 +0100 Subject: [PATCH] Polishing (cherry picked from commit 2675ce7) --- .../freemarker/FreeMarkerTemplateUtils.java | 3 +- .../ui/freemarker/SpringTemplateLoader.java | 10 +-- .../method/AbstractHandlerMethodAdapter.java | 26 +++---- .../RequestMappingHandlerAdapter.java | 70 +++++++++---------- 4 files changed, 54 insertions(+), 55 deletions(-) diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java index b2e7a85214..751ee13dc2 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/FreeMarkerTemplateUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -45,6 +45,7 @@ public abstract class FreeMarkerTemplateUtils { */ public static String processTemplateIntoString(Template template, Object model) throws IOException, TemplateException { + StringWriter result = new StringWriter(); template.process(model, result); return result.toString(); diff --git a/spring-context-support/src/main/java/org/springframework/ui/freemarker/SpringTemplateLoader.java b/spring-context-support/src/main/java/org/springframework/ui/freemarker/SpringTemplateLoader.java index 7ed965ddb5..d2509aa87f 100644 --- a/spring-context-support/src/main/java/org/springframework/ui/freemarker/SpringTemplateLoader.java +++ b/spring-context-support/src/main/java/org/springframework/ui/freemarker/SpringTemplateLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2014 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. @@ -28,9 +28,9 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; /** - * FreeMarker TemplateLoader adapter that loads via a Spring ResourceLoader. - * Used by FreeMarkerConfigurationFactory for any resource loader path that - * cannot be resolved to a java.io.File. + * FreeMarker {@link TemplateLoader} adapter that loads via a Spring {@link ResourceLoader}. + * Used by {@link FreeMarkerConfigurationFactory} for any resource loader path that cannot + * be resolved to a {@link java.io.File}. * * @author Juergen Hoeller * @since 14.03.2004 @@ -63,6 +63,7 @@ public class SpringTemplateLoader implements TemplateLoader { } } + public Object findTemplateSource(String name) throws IOException { if (logger.isDebugEnabled()) { logger.debug("Looking for FreeMarker template with name [" + name + "]"); @@ -84,7 +85,6 @@ public class SpringTemplateLoader implements TemplateLoader { } } - public long getLastModified(Object templateSource) { Resource resource = (Resource) templateSource; try { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/AbstractHandlerMethodAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/AbstractHandlerMethodAdapter.java index 9824b80e0e..342ad958f1 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/AbstractHandlerMethodAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/AbstractHandlerMethodAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 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. @@ -36,11 +36,13 @@ public abstract class AbstractHandlerMethodAdapter extends WebContentGenerator i private int order = Ordered.LOWEST_PRECEDENCE; + public AbstractHandlerMethodAdapter() { // no restriction of HTTP methods by default super(false); } + /** * Specify the order value for this HandlerAdapter bean. *

Default value is {@code Integer.MAX_VALUE}, meaning that it's non-ordered. @@ -54,49 +56,47 @@ public abstract class AbstractHandlerMethodAdapter extends WebContentGenerator i return this.order; } + /** - * {@inheritDoc}

This implementation expects the handler to be an {@link HandlerMethod}. - * + * This implementation expects the handler to be an {@link HandlerMethod}. * @param handler the handler instance to check * @return whether or not this adapter can adapt the given handler */ public final boolean supports(Object handler) { - return handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler); + return (handler instanceof HandlerMethod && supportsInternal((HandlerMethod) handler)); } /** * Given a handler method, return whether or not this adapter can support it. - * * @param handlerMethod the handler method to check * @return whether or not this adapter can adapt the given method */ protected abstract boolean supportsInternal(HandlerMethod handlerMethod); /** - * {@inheritDoc}

This implementation expects the handler to be an {@link HandlerMethod}. + * This implementation expects the handler to be an {@link HandlerMethod}. */ public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + return handleInternal(request, response, (HandlerMethod) handler); } /** * Use the given handler method to handle the request. - * * @param request current HTTP request * @param response current HTTP response * @param handlerMethod handler method to use. This object must have previously been passed to the * {@link #supportsInternal(HandlerMethod)} this interface, which must have returned {@code true}. - * @return ModelAndView object with the name of the view and the required model data, or {@code null} if - * the request has been handled directly + * @return ModelAndView object with the name of the view and the required model data, + * or {@code null} if the request has been handled directly * @throws Exception in case of errors */ protected abstract ModelAndView handleInternal(HttpServletRequest request, - HttpServletResponse response, - HandlerMethod handlerMethod) throws Exception; + HttpServletResponse response, HandlerMethod handlerMethod) throws Exception; /** - * {@inheritDoc}

This implementation expects the handler to be an {@link HandlerMethod}. + * This implementation expects the handler to be an {@link HandlerMethod}. */ public final long getLastModified(HttpServletRequest request, Object handler) { return getLastModifiedInternal(request, (HandlerMethod) handler); @@ -104,10 +104,10 @@ public abstract class AbstractHandlerMethodAdapter extends WebContentGenerator i /** * Same contract as for {@link javax.servlet.http.HttpServlet#getLastModified(HttpServletRequest)}. - * * @param request current HTTP request * @param handlerMethod handler method to use * @return the lastModified value for the given handler */ protected abstract long getLastModifiedInternal(HttpServletRequest request, HandlerMethod handlerMethod); + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java index f77b724f69..38b593bd62 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java @@ -137,9 +137,9 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter private Long asyncRequestTimeout; - private CallableProcessingInterceptor[] callableInterceptors = new CallableProcessingInterceptor[] {}; + private CallableProcessingInterceptor[] callableInterceptors = new CallableProcessingInterceptor[0]; - private DeferredResultProcessingInterceptor[] deferredResultInterceptors = new DeferredResultProcessingInterceptor[] {}; + private DeferredResultProcessingInterceptor[] deferredResultInterceptors = new DeferredResultProcessingInterceptor[0]; private boolean ignoreDefaultModelOnRedirect = false; @@ -168,21 +168,18 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter new LinkedHashMap>(); - /** - * Default constructor. - */ public RequestMappingHandlerAdapter() { - StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(); - stringHttpMessageConverter.setWriteAcceptCharset(false); // See SPR-7316 + stringHttpMessageConverter.setWriteAcceptCharset(false); // see SPR-7316 - this.messageConverters = new ArrayList>(); + this.messageConverters = new ArrayList>(4); this.messageConverters.add(new ByteArrayHttpMessageConverter()); this.messageConverters.add(stringHttpMessageConverter); this.messageConverters.add(new SourceHttpMessageConverter()); this.messageConverters.add(new AllEncompassingFormHttpMessageConverter()); } + /** * Provide resolvers for custom argument types. Custom resolvers are ordered * after built-in ones. To override the built-in support for argument @@ -305,6 +302,14 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter return modelAndViewResolvers; } + /** + * Set the {@link ContentNegotiationManager} to use to determine requested media types. + * If not set, the default constructor is used. + */ + public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) { + this.contentNegotiationManager = contentNegotiationManager; + } + /** * Provide the converters to use in argument resolvers and return value * handlers that support reading and/or writing to the body of the @@ -314,19 +319,11 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter this.messageConverters = messageConverters; } - /** - * Set the {@link ContentNegotiationManager} to use to determine requested media types. - * If not set, the default constructor is used. - */ - public void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) { - this.contentNegotiationManager = contentNegotiationManager; - } - /** * Return the configured message body converters. */ public List> getMessageConverters() { - return messageConverters; + return this.messageConverters; } /** @@ -338,10 +335,10 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter } /** - * Return the configured WebBindingInitializer, or {@code null}. + * Return the configured WebBindingInitializer, or {@code null} if none. */ public WebBindingInitializer getWebBindingInitializer() { - return webBindingInitializer; + return this.webBindingInitializer; } /** @@ -460,9 +457,8 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter } /** - * {@inheritDoc} - *

A {@link ConfigurableBeanFactory} is expected for resolving - * expressions in method argument default values. + * A {@link ConfigurableBeanFactory} is expected for resolving expressions + * in method argument default values. */ public void setBeanFactory(BeanFactory beanFactory) { if (beanFactory instanceof ConfigurableBeanFactory) { @@ -471,12 +467,13 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter } /** - * Return the owning factory of this bean instance, or {@code null}. + * Return the owning factory of this bean instance, or {@code null} if none. */ protected ConfigurableBeanFactory getBeanFactory() { return this.beanFactory; } + public void afterPropertiesSet() { if (this.argumentResolvers == null) { List resolvers = getDefaultArgumentResolvers(); @@ -634,6 +631,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter } } + /** * Always return {@code true} since any method argument and return value * type will be processed in some way. A method argument not recognized @@ -647,17 +645,6 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter return true; } - /** - * This implementation always returns -1. An {@code @RequestMapping} - * method can calculate the lastModified value, call - * {@link WebRequest#checkNotModified(long)}, and return {@code null} - * if the result of that call is {@code true}. - */ - @Override - protected long getLastModifiedInternal(HttpServletRequest request, HandlerMethod handlerMethod) { - return -1; - } - @Override protected final ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception { @@ -686,8 +673,19 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter } /** - * Return the {@link SessionAttributesHandler} instance for the given - * handler type, never {@code null}. + * This implementation always returns -1. An {@code @RequestMapping} method can + * calculate the lastModified value, call {@link WebRequest#checkNotModified(long)}, + * and return {@code null} if the result of that call is {@code true}. + */ + @Override + protected long getLastModifiedInternal(HttpServletRequest request, HandlerMethod handlerMethod) { + return -1; + } + + + /** + * Return the {@link SessionAttributesHandler} instance for the given handler type + * (never {@code null}). */ private SessionAttributesHandler getSessionAttributesHandler(HandlerMethod handlerMethod) { Class handlerType = handlerMethod.getBeanType();