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.module.admininterface;
35  
36  import info.magnolia.cms.beans.runtime.MultipartForm;
37  import info.magnolia.cms.i18n.Messages;
38  import info.magnolia.cms.i18n.MessagesManager;
39  import info.magnolia.cms.servlets.CommandBasedMVCServletHandler;
40  import info.magnolia.cms.util.RequestFormUtil;
41  import info.magnolia.context.MgnlContext;
42  import org.apache.commons.lang.StringUtils;
43  import org.slf4j.Logger;
44  import org.slf4j.LoggerFactory;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  
50  /**
51   * This is the MVCHandler for simple pages. The properties with coresponding request parameters are set with BeanUtils.
52   * @author Philipp Bracher
53   * @version $Revision: 32667 $
54   */
55  
56  public abstract class PageMVCHandler extends CommandBasedMVCServletHandler {
57  
58      /**
59       * Logger
60       */
61      Logger log = LoggerFactory.getLogger(PageMVCHandler.class);
62  
63      /**
64       * The name of the parameter passed by the request. Not used for simple pages.
65       */
66      protected static final String COMMAND_PARAMETER_NAME = "command";
67  
68      protected static final String COMMAND_SHOW = "show"; //$NON-NLS-1$
69  
70      protected static final String VIEW_SHOW = "show"; //$NON-NLS-1$
71  
72      /**
73       * The posted multipart form. Use params for easy access.
74       */
75      private MultipartForm form;
76  
77      /**
78       * The messages used for this page
79       */
80      private Messages msgs;
81  
82      /**
83       * Parameters passed by the request.
84       */
85      private RequestFormUtil params;
86  
87      /**
88       * Basename used to get messages
89       */
90      private String i18nBasename = MessagesManager.DEFAULT_BASENAME;
91  
92      /**
93       * Constuctor
94       * @param name
95       * @param request
96       * @param response
97       */
98      public PageMVCHandler(String name, HttpServletRequest request, HttpServletResponse response) {
99          super(name, request, response);
100 
101         setForm(MgnlContext.getPostedForm());
102         setParams(new RequestFormUtil(request, getForm()));
103     }
104 
105     /**
106      * @see info.magnolia.cms.servlets.MVCServletHandlerImpl#init()
107      * 
108      * TODO ! init is called twice !!! once by content2bean, once by MVCServlet !
109      */
110     public void init() {
111         super.init();
112         if (StringUtils.isEmpty(this.getCommand())) {
113             this.setCommand(COMMAND_SHOW);
114         }
115     }
116 
117     /**
118      * This is an empty implementation return the default show view.
119      * @return the view name
120      */
121     public String show() {
122         return VIEW_SHOW;
123     }
124 
125     /**
126      * @param form The form to set.
127      */
128     protected void setForm(MultipartForm form) {
129         this.form = form;
130     }
131 
132     /**
133      * @return Returns the form.
134      */
135     protected MultipartForm getForm() {
136         return form;
137     }
138 
139     /**
140      * @param msgs The msgs to set.
141      */
142     protected void setMsgs(Messages msgs) {
143         this.msgs = msgs;
144     }
145 
146     /**
147      * @return Returns the msgs.
148      */
149     protected Messages getMsgs() {
150         if(msgs == null){
151             msgs = MessagesManager.getMessages(getI18nBasename());
152         }
153         return msgs;
154     }
155 
156     /**
157      * @param params The params to set.
158      */
159     protected void setParams(RequestFormUtil params) {
160         this.params = params;
161     }
162 
163     /**
164      * @return Returns the params.
165      */
166     protected RequestFormUtil getParams() {
167         return params;
168     }
169 
170 
171     public String getI18nBasename() {
172         return this.i18nBasename;
173     }
174 
175 
176     public void setI18nBasename(String basename) {
177         this.i18nBasename = basename;
178     }
179 }