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.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   * @version $Revision$ ($Author$)
53   */
54  public abstract class CommandBasedMVCServletHandler extends MVCServletHandlerImpl {
55  
56      /**
57       * @param name
58       * @param request
59       * @param response
60       */
61      protected CommandBasedMVCServletHandler(String name, HttpServletRequest request, HttpServletResponse response) {
62          super(name, request, response);
63          this.setCatalogueName(name);
64      }
65  
66      /**
67       * Try to get the command from this catalog.
68       */
69      private String catalogueName;
70  
71      private static Logger log = LoggerFactory.getLogger(CommandBasedMVCServletHandler.class);
72  
73      /**
74       * Try to get the command from the catalog.
75       */
76      @Override
77      public String execute(String commandName) {
78          // get command from command map in JCR repository
79          Command command = findCommand(commandName);
80          if (command == null) { // not found, do in the old ways
81              if (log.isDebugEnabled()) {
82                  log.debug("can not find command named " + commandName + " in tree command map");
83              }
84              return super.execute(commandName);
85          }
86  
87          if (log.isDebugEnabled()) {
88              log.debug("found command for " + commandName + ": " + command);
89          }
90  
91          // now prepare the context
92          Context ctx = getCommandContext(commandName);
93  
94          // execute the command
95          try {
96              command.execute(ctx);
97          }
98          catch (Exception e) {
99              log.error("can't execute command", e);
100             AlertUtil.setException(e);
101         }
102         return getViewNameAfterExecution(commandName, ctx);
103     }
104 
105     /**
106      * Default implementation returns the commandName itself.
107      * @return the view name returned by this execution
108      */
109     protected String getViewNameAfterExecution(String commandName, Context ctx) {
110         return commandName;
111     }
112 
113     /**
114      * Used to get the command object.
115      * @return the callable command object
116      */
117     protected Command findCommand(String commandName) {
118         return CommandsManager.getInstance().getCommand(this.getCatalogueName(), commandName);
119     }
120 
121     /**
122      * The default implementation returns the current context.
123      * @param commandName the name of the command to be called
124      * @return the context to pass to the command
125      */
126     protected Context getCommandContext(String commandName) {
127         return MgnlContext.getInstance();
128     }
129 
130     /**
131      * @return Returns the catalogueName.
132      */
133     public String getCatalogueName() {
134         return this.catalogueName;
135     }
136 
137     /**
138      * @param catalogueName The catalogueName to set.
139      */
140     public void setCatalogueName(String catalogueName) {
141         this.catalogueName = catalogueName;
142     }
143 }