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