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.cms.servlets;
35  
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import javax.servlet.ServletException;
40  import javax.servlet.http.HttpServlet;
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  import java.io.IOException;
44  
45  
46  /**
47   * The servlet gets a {@link MVCServletHandler} with the method
48   * {@link #getHandler(HttpServletRequest, HttpServletResponse)}.
49   * {@link MVCServletHandler#getCommand()} is called and the returned command name is passed to the
50   * {@link MVCServletHandler#execute(String)} method wich returns the view name. Finally it calls
51   * {@link MVCServletHandler#renderHtml(String)} passing the view name.
52   * <p>
53   * Make a subclass to provide you own handler(s).
54   * @version $Id$
55   */
56  public abstract class MVCServlet extends HttpServlet {
57  
58      /**
59       * Stable serialVersionUID.
60       */
61      private static final long serialVersionUID = 222L;
62  
63      private static Logger log = LoggerFactory.getLogger(MVCServlet.class);
64  
65      /**
66       * @see HttpServlet#doGet(HttpServletRequest,HttpServletResponse)
67       */
68      @Override
69      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
70          doPost(request, response);
71      }
72  
73      /**
74       * @see HttpServlet#doPost(HttpServletRequest, HttpServletResponse)
75       */
76      @Override
77      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
78          IOException {
79  
80          // http://issues.apache.org/bugzilla/show_bug.cgi?id=22666
81          //
82          // 1. The Coyote HTTP/1.1 connector has a useBodyEncodingForURI attribute which
83          // if set to true will use the request body encoding to decode the URI query
84          // parameters.
85          // - The default value is true for TC4 (breaks spec but gives consistent
86          // behaviour across TC4 versions)
87          // - The default value is false for TC5 (spec compliant but there may be
88          // migration issues for some apps)
89          // 2. The Coyote HTTP/1.1 connector has a URIEncoding attribute which defaults to
90          // ISO-8859-1.
91          // 3. The parameters class (o.a.t.u.http.Parameters) has a QueryStringEncoding
92          // field which defaults to the URIEncoding. It must be set before the parameters
93          // are parsed to have an effect.
94          //
95          // Things to note regarding the servlet API:
96          // 1. HttpServletRequest.setCharacterEncoding() normally only applies to the
97          // request body NOT the URI.
98          // 2. HttpServletRequest.getPathInfo() is decoded by the web container.
99          // 3. HttpServletRequest.getRequestURI() is not decoded by container.
100         //
101         // Other tips:
102         // 1. Use POST with forms to return parameters as the parameters are then part of
103         // the request body.
104 
105         // this can throw an exception in jetty
106         try {
107             request.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
108         }
109         catch (java.lang.IllegalStateException e) {
110             log.error("can't set character encoding for the request", e); //$NON-NLS-1$
111         }
112 
113         MVCServletHandler handler = getHandler(request, response);
114 
115         if (handler == null) {
116             log.warn("no handler found for url {}", request.getRequestURI()); //$NON-NLS-1$
117             response.sendError(404);
118             return;
119         }
120 
121         // why do i have to change it if request was setted? But i have to!
122         response.setCharacterEncoding("UTF-8"); //$NON-NLS-1$
123 
124         String command = handler.getCommand();
125         String view = handler.execute(command);
126         handler.renderHtml(view);
127     }
128 
129     /**
130      * @param request
131      * @return
132      */
133     protected abstract MVCServletHandler getHandler(HttpServletRequest request, HttpServletResponse response);
134 }