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.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$
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     @Override
111     public void init() {
112         super.init();
113         if (StringUtils.isEmpty(this.getCommand())) {
114             this.setCommand(COMMAND_SHOW);
115         }
116     }
117 
118     /**
119      * This is an empty implementation return the default show view.
120      * @return the view name
121      */
122     public String show() {
123         return VIEW_SHOW;
124     }
125 
126     /**
127      * @param form The form to set.
128      */
129     protected void setForm(MultipartForm form) {
130         this.form = form;
131     }
132 
133     /**
134      * @return Returns the form.
135      */
136     protected MultipartForm getForm() {
137         return form;
138     }
139 
140     /**
141      * @param msgs The msgs to set.
142      */
143     protected void setMsgs(Messages msgs) {
144         this.msgs = msgs;
145     }
146 
147     /**
148      * @return Returns the msgs.
149      */
150     protected Messages getMsgs() {
151         if(msgs == null){
152             msgs = MessagesManager.getMessages(getI18nBasename());
153         }
154         return msgs;
155     }
156 
157     /**
158      * @param params The params to set.
159      */
160     protected void setParams(RequestFormUtil params) {
161         this.params = params;
162     }
163 
164     /**
165      * @return Returns the params.
166      */
167     protected RequestFormUtil getParams() {
168         return params;
169     }
170 
171 
172     public String getI18nBasename() {
173         return this.i18nBasename;
174     }
175 
176 
177     public void setI18nBasename(String basename) {
178         this.i18nBasename = basename;
179     }
180 }