View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 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.admininterface;
35  
36  import info.magnolia.cms.servlets.MVCServlet;
37  import info.magnolia.cms.servlets.MVCServletHandler;
38  import info.magnolia.cms.util.RequestFormUtil;
39  
40  import javax.servlet.ServletConfig;
41  import javax.servlet.ServletException;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  import info.magnolia.context.MgnlContext;
46  import org.apache.commons.lang.StringUtils;
47  import org.slf4j.Logger;
48  import org.slf4j.LoggerFactory;
49  
50  
51  /**
52   * @author Philipp Bracher
53   * @version $Id$
54   */
55  public class PageMVCServlet extends MVCServlet {
56  
57      /**
58       * Stable serialVersionUID.
59       */
60      private static final long serialVersionUID = 222L;
61  
62      /**
63       * Logger.
64       */
65      private static Logger log = LoggerFactory.getLogger(PageMVCServlet.class);
66  
67      private static final String HANDLE_PREFIX_PARAMETER = "handlePrefix";
68  
69      private static final String DEFAULT_HANDLE_PREFIX = ".magnolia/pages/";
70  
71      private String handlePrefix;
72  
73      @Override
74      public void init(ServletConfig config) throws ServletException {
75          super.init(config);
76          String handlePrefix = this.getInitParameter(HANDLE_PREFIX_PARAMETER);
77          this.handlePrefix = StringUtils.isEmpty(handlePrefix) ? DEFAULT_HANDLE_PREFIX : handlePrefix;
78      }
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      protected MVCServletHandler getHandler(HttpServletRequest request, HttpServletResponse response) {
85  
86          String pageName = MgnlContext.getAggregationState().getCurrentURI();
87          pageName = StringUtils.substringAfter(pageName, handlePrefix);
88          // strip any extension
89          pageName = StringUtils.substringBeforeLast(pageName, ".");
90  
91          PageMVCHandler handler = null;
92  
93          if (StringUtils.isNotEmpty(pageName)) {
94              // try to get a registered handler
95              try {
96                  handler = PageHandlerManager.getInstance().getPageHandler(pageName, request, response);
97              }
98              catch (InvalidDialogPageHandlerException e) {
99                  log.error("Could not instantiate page handler with name " + pageName, e);
100             }
101         }
102         else {
103             log.warn("No dialog page name passed for url {}", request.getRequestURI());
104         }
105 
106         return handler;
107     }
108 
109 }