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