/** * This file Copyright (c) 2010-2013 Magnolia International * Ltd. (http://www.magnolia-cms.com). All rights reserved. * * * This file is dual-licensed under both the Magnolia * Network Agreement and the GNU General Public License. * You may elect to use one or the other of these licenses. * * This file is distributed in the hope that it will be * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. * Redistribution, except as permitted by whichever of the GPL * or MNA you select, is prohibited. * * 1. For the GPL license (GPL), you can redistribute and/or * modify this file under the terms of the GNU General * Public License, Version 3, as published by the Free Software * Foundation. You should have received a copy of the GNU * General Public License, Version 3 along with this program; * if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 2. For the Magnolia Network Agreement (MNA), this file * and the accompanying materials are made available under the * terms of the MNA which accompanies this distribution, and * is available at http://www.magnolia-cms.com/mna.html * * Any modifications to this file must keep this entire header * intact. * */ package info.magnolia.module.blossom.render; import java.io.IOException; import java.util.Locale; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.context.ApplicationContext; import org.springframework.util.Assert; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerExecutionChain; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.View; import info.magnolia.context.MgnlContext; import info.magnolia.module.blossom.context.MagnoliaLocaleResolver; import info.magnolia.module.blossom.dialog.DialogExporter; import info.magnolia.module.blossom.dispatcher.BlossomDispatcher; import info.magnolia.module.blossom.dispatcher.BlossomDispatcherAwareBeanPostProcessor; import info.magnolia.module.blossom.dispatcher.BlossomDispatcherInitializedEvent; import info.magnolia.module.blossom.support.BeanFactoryUtils; import info.magnolia.module.blossom.support.SimulatedDispatchRequestWrapper; import info.magnolia.module.blossom.template.TemplateExporter; import info.magnolia.module.blossom.urimapping.AnnotatedVirtualURIMappingExporter; import info.magnolia.module.blossom.urimapping.VirtualURIMappingExporter; /** * Specialization of DispatcherServlet that detects templates, paragraphs and dialogs factories and expose functionality * for rendering and pre-execution. * * @since 0.5 */ public class BlossomDispatcherServlet extends DispatcherServlet implements BlossomDispatcher, BeanFactoryPostProcessor { @Override protected Object getDefaultStrategy(ApplicationContext context, Class strategyInterface) throws BeansException { if (strategyInterface.equals(LocaleResolver.class)) { return super.createDefaultStrategy(context, MagnoliaLocaleResolver.class); } return super.getDefaultStrategy(context, strategyInterface); } @Override protected void postProcessWebApplicationContext(ConfigurableWebApplicationContext wac) { wac.addBeanFactoryPostProcessor(this); } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { Assert.isInstanceOf(BeanDefinitionRegistry.class, beanFactory); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; beanFactory.addBeanPostProcessor(new BlossomDispatcherAwareBeanPostProcessor(this)); BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, TemplateExporter.class); BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, DialogExporter.class); BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, AnnotatedVirtualURIMappingExporter.class); BeanFactoryUtils.registerBeanIfMissing(beanFactory, registry, VirtualURIMappingExporter.class); } @Override protected WebApplicationContext initWebApplicationContext() throws BeansException { WebApplicationContext wac = super.initWebApplicationContext(); wac.publishEvent(new BlossomDispatcherInitializedEvent(this)); return wac; } @Override public void forward(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String contextPath = request.getContextPath(); request = SimulatedDispatchRequestWrapper.forward(request, contextPath + path, contextPath, path, null); MgnlContext.push(request, response); try { super.service(request, response); } finally { MgnlContext.pop(); SimulatedDispatchRequestWrapper.release(request); } } @Override public void include(String path, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String contextPath = request.getContextPath(); request = SimulatedDispatchRequestWrapper.include(request, contextPath + path, contextPath, path, null, request.getQueryString()); MgnlContext.push(request, response); try { super.service(request, response); } finally { MgnlContext.pop(); SimulatedDispatchRequestWrapper.release(request); } } @Override public ModelAndView executeChain(HandlerExecutionChain mappedHandler, HttpServletRequest request, HttpServletResponse response) throws Exception { // This methods mimics the functionality in org.springframework.web.servlet.DispatcherServlet#doDispatch // Apply preHandle methods of registered interceptors. HandlerInterceptor[] interceptors = mappedHandler.getInterceptors(); if (interceptors != null) { for (int i = 0; i < interceptors.length; i++) { HandlerInterceptor interceptor = interceptors[i]; if (!interceptor.preHandle(request, response, mappedHandler.getHandler())) { return null; } } } // Actually invoke the handler. HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler()); ModelAndView mv = ha.handle(request, response, mappedHandler.getHandler()); // Do we need view name translation? if (mv != null && !mv.hasView()) { mv.setViewName(getDefaultViewName(request)); } // Apply postHandle methods of registered interceptors. if (interceptors != null) { for (int i = interceptors.length - 1; i >= 0; i--) { HandlerInterceptor interceptor = interceptors[i]; interceptor.postHandle(request, response, mappedHandler.getHandler(), mv); } } if (mv != null && !mv.wasCleared()) { // Resolving the view mimics org.springframework.web.servlet.DispatcherServlet#render LocaleResolver localeResolver = (LocaleResolver) request.getAttribute(LOCALE_RESOLVER_ATTRIBUTE); // Determine locale for request and apply it to the response. Locale locale = localeResolver.resolveLocale(request); response.setLocale(locale); View view; if (mv.isReference()) { // We need to resolve the view name. view = resolveViewName(mv.getViewName(), mv.getModel(), locale, request); if (view == null) { throw new ServletException( "Could not resolve view with name '" + mv.getViewName() + "' in servlet with name '" + getServletName() + "'"); } } else { // No need to lookup: the ModelAndView object contains the actual View object. view = mv.getView(); if (view == null) { throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " + "View object in servlet with name '" + getServletName() + "'"); } } mv.setView(view); } return mv; } }