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