View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.blossom.web;
35  
36  import info.magnolia.module.blossom.view.UuidRedirectViewResolver;
37  import info.magnolia.repository.RepositoryConstants;
38  
39  import java.lang.reflect.InvocationTargetException;
40  import java.util.ArrayList;
41  import java.util.List;
42  
43  import org.springframework.beans.factory.BeanInitializationException;
44  import org.springframework.core.MethodParameter;
45  import org.springframework.util.PatternMatchUtils;
46  import org.springframework.web.context.request.NativeWebRequest;
47  import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
48  import org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite;
49  import org.springframework.web.method.support.ModelAndViewContainer;
50  import org.springframework.web.servlet.ModelAndView;
51  import org.springframework.web.servlet.mvc.method.annotation.ModelAndViewMethodReturnValueHandler;
52  import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
53  import org.springframework.web.servlet.mvc.method.annotation.ViewNameMethodReturnValueHandler;
54  
55  /**
56   * Custom RequestMappingHandlerAdapter with support for detecting views that will result in redirects and marking them
57   * for redirect scenario handling, this is necessary for flash attributes to function properly. Typical view names
58   * used in Blossom are automatically supported and a set of custom patterns can also be set.
59   * <p>
60   * For more information see http://jira.magnolia-cms.com/browse/BLOSSOM-181 and https://jira.spring.io/browse/SPR-12054.
61   *
62   * @see ModelAndViewContainer#setRedirectModelScenario(boolean)
63   * @since 3.0.4
64   */
65  public class BlossomRequestMappingHandlerAdapter extends RequestMappingHandlerAdapter {
66  
67      public String[] redirectPatterns = new String[]{RepositoryConstants.WEBSITE + ":*", "dms:*"};
68  
69      public String[] getRedirectPatterns() {
70          return redirectPatterns;
71      }
72  
73      /**
74       * Sets the view name patterns that will result in a redirect, in a pattern format supporting wildcards.
75       *
76       * @see PatternMatchUtils#simpleMatch(String, String)
77       */
78      public void setRedirectPatterns(String... redirectPatterns) {
79          this.redirectPatterns = redirectPatterns;
80      }
81  
82      @Override
83      public void afterPropertiesSet() {
84          super.afterPropertiesSet();
85  
86          List<HandlerMethodReturnValueHandler> handlers = new ArrayList<HandlerMethodReturnValueHandler>(getExistingReturnValueHandlers());
87  
88          for (int i = 0; i < handlers.size(); i++) {
89              HandlerMethodReturnValueHandler handler = handlers.get(i);
90              if (handler instanceof ViewNameMethodReturnValueHandler) {
91                  handlers.set(i, new ViewNameMethodReturnValueHandler() {
92  
93                      @Override
94                      protected boolean isRedirectViewName(String viewName) {
95                          return super.isRedirectViewName(viewName) || BlossomRequestMappingHandlerAdapter.this.isRedirectViewName(viewName);
96                      }
97                  });
98              }
99              if (handler instanceof ModelAndViewMethodReturnValueHandler) {
100                 handlers.set(i, new ModelAndViewMethodReturnValueHandler() {
101 
102                     @Override
103                     public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
104                         if (returnValue != null) {
105                             ModelAndView mav = (ModelAndView) returnValue;
106                             if (mav.isReference()) {
107                                 String viewName = mav.getViewName();
108                                 if (viewName != null && BlossomRequestMappingHandlerAdapter.this.isRedirectViewName(viewName)) {
109                                     mavContainer.setRedirectModelScenario(true);
110                                 }
111                             }
112                         }
113                         super.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
114                     }
115                 });
116             }
117         }
118 
119         super.setReturnValueHandlers(handlers);
120     }
121 
122     /**
123      * The method getReturnValueHandlers changed return type in Spring 4. Doing it this way we can remain compatible with both.
124      * <ul>
125      * <li>In Spring 3 it returned HandlerMethodReturnValueHandlerComposite
126      * <li>In Spring 4 it returns List&lt;HandlerMethodReturnValueHandler&gt;
127      * </ul>
128      */
129     private List<HandlerMethodReturnValueHandler> getExistingReturnValueHandlers() {
130 
131         Object returnValueHandlers;
132         try {
133             returnValueHandlers = getClass().getMethod("getReturnValueHandlers").invoke(this);
134         } catch (IllegalAccessException e) {
135             throw new BeanInitializationException("Could not get existing return value handlers", e);
136         } catch (InvocationTargetException e) {
137             throw new BeanInitializationException("Could not get existing return value handlers", e.getTargetException());
138         } catch (NoSuchMethodException e) {
139             throw new BeanInitializationException("Could not get existing return value handlers", e);
140         }
141 
142         if (returnValueHandlers instanceof List) {
143             return (List<HandlerMethodReturnValueHandler>) returnValueHandlers;
144         }
145 
146         if (returnValueHandlers instanceof HandlerMethodReturnValueHandlerComposite) {
147             return ((HandlerMethodReturnValueHandlerComposite) returnValueHandlers).getHandlers();
148         }
149 
150         throw new BeanInitializationException("Could not get existing return value handlers");
151     }
152 
153     protected boolean isRedirectViewName(String viewName) {
154         return viewName.equals(UuidRedirectViewResolver.REDIRECT_MAIN_CONTENT_PLACEHOLDER) ||
155                 viewName.equals(UuidRedirectViewResolver.REDIRECT_CURRENT_CONTENT_PLACEHOLDER) ||
156                 (redirectPatterns != null && PatternMatchUtils.simpleMatch(redirectPatterns, viewName));
157     }
158 }