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.AlertUtil;
37  import info.magnolia.commands.CommandsManager;
38  import info.magnolia.context.Context;
39  import info.magnolia.context.MgnlContext;
40  
41  import javax.servlet.http.HttpServletRequest;
42  import javax.servlet.http.HttpServletResponse;
43  
44  import org.apache.commons.chain.Command;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  
49  /**
50   * This implementation tries first to get a command form it's command catalogue. If none is found it will call the
51   * execute method of the default MVCServletHandlerImpl, which tries to execute through reflection a related method.
52   * @author Philipp Bracher
53   * @version $Revision: 32667 $ ($Author: gjoseph $)
54   */
55  public abstract class CommandBasedMVCServletHandler extends MVCServletHandlerImpl {
56  
57      /**
58       * @param name
59       * @param request
60       * @param response
61       */
62      protected CommandBasedMVCServletHandler(String name, HttpServletRequest request, HttpServletResponse response) {
63          super(name, request, response);
64          this.setCatalogueName(name);
65      }
66  
67      /**
68       * Try to get the command from this catalogue
69       */
70      private String catalogueName;
71  
72      /**
73       * The logger use in this class
74       */
75      private static Logger log = LoggerFactory.getLogger(CommandBasedMVCServletHandler.class);
76  
77      /**
78       * Try to get the command from the catalogue
79       */
80      public String execute(String commandName) {
81          // get command from command map in JCR repository
82          Command command = findCommand(commandName);
83          if (command == null) { // not found, do in the old ways
84              if (log.isDebugEnabled()) {
85                  log.debug("can not find command named " + commandName + " in tree command map");
86              }
87              return super.execute(commandName);
88          }
89  
90          if (log.isDebugEnabled()) {
91              log.debug("found command for " + commandName + ": " + command);
92          }
93  
94          // now prepare the context
95          Context ctx = getCommandContext(commandName);
96  
97          // execute the command
98          try {
99              command.execute(ctx);
100         }
101         catch (Exception e) {
102             log.error("can't execute command", e);
103             AlertUtil.setException(e);
104         }
105         return getViewNameAfterExecution(commandName, ctx);
106     }
107 
108     /**
109      * Default implemenation returns the commandName itself
110      * @param commandName
111      * @param ctx
112      * @return the view name returned by this execution
113      */
114     protected String getViewNameAfterExecution(String commandName, Context ctx) {
115         return commandName;
116     }
117 
118     /**
119      * Used to get the command object
120      * @param commandName
121      * @return the callable command object
122      */
123     protected Command findCommand(String commandName) {
124         return CommandsManager.getInstance().getCommand(this.getCatalogueName(), commandName);
125     }
126 
127     /**
128      * The default implementation returns the current context
129      * @param commandName the name of the command to be called
130      * @return the context to pass to the command
131      */
132     protected Context getCommandContext(String commandName) {
133         return MgnlContext.getInstance();
134     }
135 
136     /**
137      * @return Returns the catalogueName.
138      */
139     public String getCatalogueName() {
140         return this.catalogueName;
141     }
142 
143     /**
144      * @param catalogueName The catalogueName to set.
145      */
146     public void setCatalogueName(String catalogueName) {
147         this.catalogueName = catalogueName;
148     }
149 }