View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 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.render;
35  
36  import java.io.IOException;
37  import java.util.Locale;
38  
39  import javax.servlet.ServletException;
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  
43  import org.springframework.beans.BeansException;
44  import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
45  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
46  import org.springframework.beans.factory.support.BeanDefinitionRegistry;
47  import org.springframework.context.ApplicationContext;
48  import org.springframework.util.Assert;
49  import org.springframework.web.context.ConfigurableWebApplicationContext;
50  import org.springframework.web.context.WebApplicationContext;
51  import org.springframework.web.servlet.DispatcherServlet;
52  import org.springframework.web.servlet.HandlerAdapter;
53  import org.springframework.web.servlet.HandlerExecutionChain;
54  import org.springframework.web.servlet.HandlerInterceptor;
55  import org.springframework.web.servlet.LocaleResolver;
56  import org.springframework.web.servlet.ModelAndView;
57  import org.springframework.web.servlet.View;
58  
59  import info.magnolia.context.MgnlContext;
60  import info.magnolia.module.blossom.context.MagnoliaLocaleResolver;
61  import info.magnolia.module.blossom.dialog.DialogExporter;
62  import info.magnolia.module.blossom.dispatcher.BlossomDispatcher;
63  import info.magnolia.module.blossom.dispatcher.BlossomDispatcherAwareBeanPostProcessor;
64  import info.magnolia.module.blossom.dispatcher.BlossomDispatcherInitializedEvent;
65  import info.magnolia.module.blossom.support.BeanFactoryUtils;
66  import info.magnolia.module.blossom.support.ForwardRequestWrapper;
67  import info.magnolia.module.blossom.support.IncludeRequestWrapper;
68  import info.magnolia.module.blossom.template.TemplateExporter;
69  import info.magnolia.module.blossom.urimapping.AnnotatedVirtualURIMappingExporter;
70  import info.magnolia.module.blossom.urimapping.VirtualURIMappingExporter;
71  
72  /**
73   * Specialization of DispatcherServlet that detects templates, components and dialogs factories and expose functionality
74   * for rendering and pre-execution.
75   *
76   * @since 0.5
77   */
78  public class BlossomDispatcherServlet extends DispatcherServlet implements BlossomDispatcher, BeanFactoryPostProcessor {
79  
80      public BlossomDispatcherServlet() {
81          super();
82      }
83  
84      public BlossomDispatcherServlet(WebApplicationContext webApplicationContext) {
85          super(webApplicationContext);
86      }
87  
88      @Override
89      protected Object getDefaultStrategy(ApplicationContext context, Class strategyInterface) throws BeansException {
90          if (strategyInterface.equals(LocaleResolver.class)) {
91              return super.createDefaultStrategy(context, MagnoliaLocaleResolver.class);
92          }
93          return super.getDefaultStrategy(context, strategyInterface);
94      }
95  
96      @Override
97      protected void postProcessWebApplicationContext(ConfigurableWebApplicationContext wac) {
98          wac.addBeanFactoryPostProcessor(this);
99      }
100 
101     @Override
102     public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
103 
104         Assert.isInstanceOf(BeanDefinitionRegistry.class, beanFactory);
105 
106         BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
107 
108         beanFactory.addBeanPostProcessor(new BlossomDispatcherAwareBeanPostProcessor(this));
109 
110         BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, TemplateExporter.class);
111         BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, DialogExporter.class);
112         BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, AnnotatedVirtualURIMappingExporter.class);
113         BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, VirtualURIMappingExporter.class);
114     }
115 
116     @Override
117     protected void onRefresh(ApplicationContext context) {
118         super.onRefresh(context);
119         context.publishEvent(new BlossomDispatcherInitializedEvent(this));
120     }
121 
122     @Override
123     public void forward(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
124 
125         String contextPath = request.getContextPath();
126 
127         ForwardRequestWrapperper.html#ForwardRequestWrapper">ForwardRequestWrapper forwardRequestWrapper = new ForwardRequestWrapper(request, contextPath + path, contextPath, path, null);
128 
129         MgnlContext.push(forwardRequestWrapper, response);
130         try {
131             super.service(forwardRequestWrapper, response);
132         } finally {
133             forwardRequestWrapper.restore();
134             MgnlContext.pop();
135         }
136     }
137 
138     @Override
139     public void include(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
140 
141         String contextPath = request.getContextPath();
142 
143         IncludeRequestWrapperper.html#IncludeRequestWrapper">IncludeRequestWrapper includeRequestWrapper = new IncludeRequestWrapper(request, contextPath + path, contextPath, path, null, request.getQueryString());
144 
145         MgnlContext.push(includeRequestWrapper, response);
146         try {
147             super.service(includeRequestWrapper, response);
148         } finally {
149             includeRequestWrapper.restore();
150             MgnlContext.pop();
151         }
152     }
153 
154     @Override
155     public ModelAndView executeChain(HandlerExecutionChain mappedHandler, HttpServletRequest request, HttpServletResponse response) throws Exception {
156 
157         // This methods mimics the functionality in org.springframework.web.servlet.DispatcherServlet#doDispatch
158 
159         // Apply preHandle methods of registered interceptors.
160         HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
161         if (interceptors != null) {
162             for (int i = 0; i < interceptors.length; i++) {
163                 HandlerInterceptor interceptor = interceptors[i];
164                 if (!interceptor.preHandle(request, response, mappedHandler.getHandler())) {
165                     return null;
166                 }
167             }
168         }
169 
170         // Actually invoke the handler.
171         HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
172         ModelAndView mv = ha.handle(request, response, mappedHandler.getHandler());
173 
174         // Do we need view name translation?
175         if (mv != null && !mv.hasView()) {
176             mv.setViewName(getDefaultViewName(request));
177         }
178 
179         // Apply postHandle methods of registered interceptors.
180         if (interceptors != null) {
181             for (int i = interceptors.length - 1; i >= 0; i--) {
182                 HandlerInterceptor interceptor = interceptors[i];
183                 interceptor.postHandle(request, response, mappedHandler.getHandler(), mv);
184             }
185         }
186 
187         if (mv != null && !mv.wasCleared()) {
188 
189             // Resolving the view mimics org.springframework.web.servlet.DispatcherServlet#render
190 
191             LocaleResolver localeResolver = (LocaleResolver) request.getAttribute(LOCALE_RESOLVER_ATTRIBUTE);
192 
193             // Determine locale for request and apply it to the response.
194             Locale locale = localeResolver.resolveLocale(request);
195             response.setLocale(locale);
196 
197             View view;
198             if (mv.isReference()) {
199                 // We need to resolve the view name.
200                 view = resolveViewName(mv.getViewName(), mv.getModel(), locale, request);
201                 if (view == null) {
202                     throw new ServletException(
203                             "Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" +
204                                     getServletName() + "'");
205                 }
206             }
207             else {
208                 // No need to lookup: the ModelAndView object contains the actual View object.
209                 view = mv.getView();
210                 if (view == null) {
211                     throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
212                             "View object in servlet with name '" + getServletName() + "'");
213                 }
214             }
215 
216             mv.setView(view);
217         }
218 
219         return mv;
220     }
221 }