1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.render

File BlossomDispatcherServlet.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart3.png
62% of files have more coverage

Code metrics

22
57
9
1
221
137
22
0.39
6.33
9
2.44

Classes

Class Line # Actions
BlossomDispatcherServlet 78 57 0% 22 67
0.2386363623.9%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /**
2    * This file Copyright (c) 2010-2016 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  3 toggle public BlossomDispatcherServlet() {
81  3 super();
82    }
83   
 
84  0 toggle public BlossomDispatcherServlet(WebApplicationContext webApplicationContext) {
85  0 super(webApplicationContext);
86    }
87   
 
88  12 toggle @Override
89    protected Object getDefaultStrategy(ApplicationContext context, Class strategyInterface) throws BeansException {
90  12 if (strategyInterface.equals(LocaleResolver.class)) {
91  3 return super.createDefaultStrategy(context, MagnoliaLocaleResolver.class);
92    }
93  9 return super.getDefaultStrategy(context, strategyInterface);
94    }
95   
 
96  3 toggle @Override
97    protected void postProcessWebApplicationContext(ConfigurableWebApplicationContext wac) {
98  3 wac.addBeanFactoryPostProcessor(this);
99    }
100   
 
101  3 toggle @Override
102    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
103   
104  3 Assert.isInstanceOf(BeanDefinitionRegistry.class, beanFactory);
105   
106  3 BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
107   
108  3 beanFactory.addBeanPostProcessor(new BlossomDispatcherAwareBeanPostProcessor(this));
109   
110  3 BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, TemplateExporter.class);
111  3 BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, DialogExporter.class);
112  3 BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, AnnotatedVirtualURIMappingExporter.class);
113  3 BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, VirtualURIMappingExporter.class);
114    }
115   
 
116  3 toggle @Override
117    protected void onRefresh(ApplicationContext context) {
118  3 super.onRefresh(context);
119  3 context.publishEvent(new BlossomDispatcherInitializedEvent(this));
120    }
121   
 
122  0 toggle @Override
123    public void forward(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
124   
125  0 String contextPath = request.getContextPath();
126   
127  0 ForwardRequestWrapper forwardRequestWrapper = new ForwardRequestWrapper(request, contextPath + path, contextPath, path, null);
128   
129  0 MgnlContext.push(forwardRequestWrapper, response);
130  0 try {
131  0 super.service(forwardRequestWrapper, response);
132    } finally {
133  0 forwardRequestWrapper.restore();
134  0 MgnlContext.pop();
135    }
136    }
137   
 
138  0 toggle @Override
139    public void include(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
140   
141  0 String contextPath = request.getContextPath();
142   
143  0 IncludeRequestWrapper includeRequestWrapper = new IncludeRequestWrapper(request, contextPath + path, contextPath, path, null, request.getQueryString());
144   
145  0 MgnlContext.push(includeRequestWrapper, response);
146  0 try {
147  0 super.service(includeRequestWrapper, response);
148    } finally {
149  0 includeRequestWrapper.restore();
150  0 MgnlContext.pop();
151    }
152    }
153   
 
154  0 toggle @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  0 HandlerInterceptor[] interceptors = mappedHandler.getInterceptors();
161  0 if (interceptors != null) {
162  0 for (int i = 0; i < interceptors.length; i++) {
163  0 HandlerInterceptor interceptor = interceptors[i];
164  0 if (!interceptor.preHandle(request, response, mappedHandler.getHandler())) {
165  0 return null;
166    }
167    }
168    }
169   
170    // Actually invoke the handler.
171  0 HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
172  0 ModelAndView mv = ha.handle(request, response, mappedHandler.getHandler());
173   
174    // Do we need view name translation?
175  0 if (mv != null && !mv.hasView()) {
176  0 mv.setViewName(getDefaultViewName(request));
177    }
178   
179    // Apply postHandle methods of registered interceptors.
180  0 if (interceptors != null) {
181  0 for (int i = interceptors.length - 1; i >= 0; i--) {
182  0 HandlerInterceptor interceptor = interceptors[i];
183  0 interceptor.postHandle(request, response, mappedHandler.getHandler(), mv);
184    }
185    }
186   
187  0 if (mv != null && !mv.wasCleared()) {
188   
189    // Resolving the view mimics org.springframework.web.servlet.DispatcherServlet#render
190   
191  0 LocaleResolver localeResolver = (LocaleResolver) request.getAttribute(LOCALE_RESOLVER_ATTRIBUTE);
192   
193    // Determine locale for request and apply it to the response.
194  0 Locale locale = localeResolver.resolveLocale(request);
195  0 response.setLocale(locale);
196   
197  0 View view;
198  0 if (mv.isReference()) {
199    // We need to resolve the view name.
200  0 view = resolveViewName(mv.getViewName(), mv.getModel(), locale, request);
201  0 if (view == null) {
202  0 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  0 view = mv.getView();
210  0 if (view == null) {
211  0 throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
212    "View object in servlet with name '" + getServletName() + "'");
213    }
214    }
215   
216  0 mv.setView(view);
217    }
218   
219  0 return mv;
220    }
221    }