View Javadoc
1   /**
2    * This file Copyright (c) 2003-2017 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 info.magnolia.cms.util.DeprecationUtil;
37  import info.magnolia.cms.util.RequestFormUtil;
38  
39  import java.io.PrintWriter;
40  import java.io.StringWriter;
41  import java.lang.reflect.InvocationTargetException;
42  import java.lang.reflect.Method;
43  import java.util.HashMap;
44  import java.util.Map;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.commons.beanutils.BeanUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  
54  /**
55   * Default implementation of a MVCHandler. Calls the command (method) through reflection.
56   * @deprecated since 5.5.3 not used
57   */
58  @Deprecated
59  public abstract class MVCServletHandlerImpl implements MVCServletHandler {
60  
61      protected static final String VIEW_ERROR = "error";
62  
63      protected static final String VIEW_SUCCESS = "success";
64  
65      private static Logger log = LoggerFactory.getLogger(MVCServletHandlerImpl.class);
66  
67      protected HttpServletRequest request;
68  
69      protected HttpServletResponse response;
70  
71      protected Throwable exception;
72  
73      private String name;
74  
75      private String command;
76  
77      protected MVCServletHandlerImpl(String name, HttpServletRequest request, HttpServletResponse response) {
78          this.name = name;
79          this.setRequest(request);
80          this.setResponse(response);
81      }
82  
83      @Override
84      public void init() {
85          DeprecationUtil.isDeprecated();
86          populateFromRequest(this);
87      }
88  
89      protected void populateFromRequest(Object bean) {
90          RequestFormUtil requestFormUtil = new RequestFormUtil(this.getRequest());
91          Map parameters = new HashMap(); // needed, can't directly modify the map returned by request.getParameterMap()
92          parameters.putAll(requestFormUtil.getParameters());
93          parameters.putAll(requestFormUtil.getDocuments()); // handle uploaded files too
94  
95          try {
96              // TODO : we could filter the parameters
97              BeanUtils.populate(bean, parameters);
98          } catch (Exception e) {
99              log.error("can't set properties on the handler", e);
100         }
101     }
102 
103     /**
104      * @see info.magnolia.cms.servlets.MVCServletHandler#getName()
105      */
106     @Override
107     public String getName() {
108         return name;
109     }
110 
111     /**
112      * Call the method through reflection.
113      */
114     @Override
115     public String execute(String command) {
116         String view = VIEW_ERROR;
117         Method method;
118 
119         try {
120             method = this.getClass().getMethod(command);
121             // method.setAccessible(true);
122             view = (String) method.invoke(this);
123         } catch (InvocationTargetException e) {
124             log.error("can't call command: {}", command, e.getTargetException());
125             exception = e.getTargetException();
126         } catch (Exception e) {
127             log.error("can't call command: {}", command, e);
128             exception = e;
129         }
130 
131         return view;
132     }
133 
134     /**
135      * @param request The request to set.
136      */
137     public void setRequest(HttpServletRequest request) {
138         this.request = request;
139     }
140 
141     /**
142      * @return Returns the request.
143      */
144     public HttpServletRequest getRequest() {
145         return request;
146     }
147 
148     /**
149      * @param response The response to set.
150      */
151     public void setResponse(HttpServletResponse response) {
152         this.response = response;
153     }
154 
155     /**
156      * @return Returns the response.
157      */
158     public HttpServletResponse getResponse() {
159         return response;
160     }
161 
162     /**
163      * @return Returns the command.
164      */
165     @Override
166     public String getCommand() {
167         return this.command;
168     }
169 
170     /**
171      * @param command The command to set.
172      */
173     public void setCommand(String command) {
174         this.command = command;
175     }
176 
177     /**
178      * Getter for <code>exception</code>.
179      *
180      * @return Returns the exception.
181      */
182     public Throwable getException() {
183         return this.exception;
184     }
185 
186     /**
187      * Returns the stacktrace from the exception as a String.
188      */
189     public String getExceptionStackTrace() {
190         if (this.exception == null) {
191             return null;
192         }
193         StringWriter writer = new StringWriter();
194         this.exception.printStackTrace(new PrintWriter(writer));
195         return writer.toString();
196     }
197 }