1 13 package info.magnolia.cms.servlets; 14 15 import info.magnolia.cms.util.AlertUtil; 16 import info.magnolia.commands.CommandsManager; 17 import info.magnolia.context.Context; 18 import info.magnolia.context.MgnlContext; 19 20 import javax.servlet.http.HttpServletRequest ; 21 import javax.servlet.http.HttpServletResponse ; 22 23 import org.apache.commons.chain.Command; 24 import org.slf4j.Logger; 25 import org.slf4j.LoggerFactory; 26 27 28 34 public abstract class CommandBasedMVCServletHandler extends MVCServletHandlerImpl { 35 36 41 protected CommandBasedMVCServletHandler(String name, HttpServletRequest request, HttpServletResponse response) { 42 super(name, request, response); 43 this.setCatalogueName(name); 44 } 45 46 49 private String catalogueName; 50 51 54 private static Logger log = LoggerFactory.getLogger(CommandBasedMVCServletHandler.class); 55 56 59 public String execute(String commandName) { 60 Command command = findCommand(commandName); 62 if (command == null) { if (log.isDebugEnabled()) { 64 log.debug("can not find command named " + commandName + " in tree command map"); 65 } 66 return super.execute(commandName); 67 } 68 69 if (log.isDebugEnabled()) { 70 log.debug("found command for " + commandName + ": " + command); 71 } 72 73 Context ctx = getCommandContext(commandName); 75 76 try { 78 command.execute(ctx); 79 } 80 catch (Exception e) { 81 log.error("can't execute command", e); 82 AlertUtil.setException(e); 83 } 84 return getViewNameAfterExecution(commandName, ctx); 85 } 86 87 93 protected String getViewNameAfterExecution(String commandName, Context ctx) { 94 return commandName; 95 } 96 97 102 protected Command findCommand(String commandName) { 103 return CommandsManager.getInstance().getCommand(this.getCatalogueName(), commandName); 104 } 105 106 111 protected Context getCommandContext(String commandName) { 112 return MgnlContext.getInstance(); 113 } 114 115 118 public String getCatalogueName() { 119 return this.catalogueName; 120 } 121 122 125 public void setCatalogueName(String catalogueName) { 126 this.catalogueName = catalogueName; 127 } 128 } 129 | Popular Tags |