View Javadoc

1   /**
2    * This file Copyright (c) 2003-2010 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: MVCServletHandlerImpl.java 32667 2010-03-13 00:37:06Z gjoseph $
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      /**
65       * Logger.
66       */
67      private static Logger log = LoggerFactory.getLogger(MVCServletHandlerImpl.class);
68  
69      protected HttpServletRequest request;
70  
71      protected HttpServletResponse response;
72  
73      protected Throwable exception;
74  
75      private String name;
76  
77      private String command;
78  
79      protected MVCServletHandlerImpl(String name, HttpServletRequest request, HttpServletResponse response) {
80          this.name = name;
81          this.setRequest(request);
82          this.setResponse(response);
83      }
84  
85      public void init() {
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          }
99          catch (Exception e) {
100             log.error("can't set properties on the handler", e);
101         }
102     }
103 
104     /**
105      * @see info.magnolia.cms.servlets.MVCServletHandler#getName()
106      */
107     public String getName() {
108         return name;
109     }
110 
111     /**
112      * Call the method through reflection
113      * @param command
114      * @return the name of the view to show (used in renderHtml)
115      */
116     public String execute(String command) {
117         String view = VIEW_ERROR;
118         Method method;
119 
120         try {
121             method = this.getClass().getMethod(command, new Class[]{});
122             // method.setAccessible(true);
123             view = (String) method.invoke(this, new Object[]{});
124         }
125         catch (InvocationTargetException e) {
126             log.error("can't call command: " + command, e.getTargetException()); //$NON-NLS-1$
127             exception = e.getTargetException();
128         }
129         catch (Exception e) {
130             log.error("can't call command: " + command, e); //$NON-NLS-1$
131             exception = e;
132         }
133 
134         return view;
135     }
136 
137     /**
138      * @param request The request to set.
139      */
140     public void setRequest(HttpServletRequest request) {
141         this.request = request;
142     }
143 
144     /**
145      * @return Returns the request.
146      */
147     public HttpServletRequest getRequest() {
148         return request;
149     }
150 
151     /**
152      * @param response The response to set.
153      */
154     public void setResponse(HttpServletResponse response) {
155         this.response = response;
156     }
157 
158     /**
159      * @return Returns the response.
160      */
161     public HttpServletResponse getResponse() {
162         return response;
163     }
164 
165     /**
166      * @return Returns the command.
167      */
168     public String getCommand() {
169         return this.command;
170     }
171 
172     /**
173      * @param command The command to set.
174      */
175     public void setCommand(String command) {
176         this.command = command;
177     }
178 
179     /**
180      * Getter for <code>exception</code>.
181      * @return Returns the exception.
182      */
183     public Throwable getException() {
184         return this.exception;
185     }
186 
187     /**
188      * Returns the stacktrace from the exception as a String
189      * @return
190      */
191     public String getExceptionStackTrace() {
192         if (this.exception == null) {
193             return null;
194         }
195         StringWriter writer = new StringWriter();
196         this.exception.printStackTrace(new PrintWriter(writer));
197         return writer.toString();
198     }
199 }