. Generic "command servlet" implementation

- Replaces specific command servlets, such as the WorkflowActionServlet with a generic CommandServlet

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@2618 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Kevin Roast
2006-04-04 13:18:11 +00:00
parent f4a53302aa
commit 54c9595dc6
10 changed files with 657 additions and 413 deletions

View File

@@ -0,0 +1,197 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
import org.alfresco.config.Config;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.web.app.Application;
import org.alfresco.web.app.servlet.command.CommandFactory;
import org.alfresco.web.app.servlet.command.CommandProcessor;
import org.alfresco.web.config.CommandServletConfigElement;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Servlet responsible for executing commands upon node(s).
* <p>
* The URL to the servlet should be generated thus:
* <pre>/alfresco/command/processor-name/command-name/args/...</pre>
* <p>
* The 'processor-name' identifies the command processor to execute the command. For example the
* 'workflow' processor will execute workflow commands upon a node (e.g. "approve" or "reject").
* For example:
* <pre>/alfresco/command/workflow/approve/workspace/SpacesStore/0000-0000-0000-0000
* The store protocol, followed by the store ID, followed by the content Node Id used to
* identify the node to execute the workflow action upon.
* <p>
* A 'return-page' URL argument can be specified as the redirect page to navigate too after processing.
* <p>
* Like most Alfresco servlets, the URL may be followed by a valid 'ticket' argument for authentication:
* ?ticket=1234567890
* <p>
* And/or also followed by the "?guest=true" argument to force guest access login for the URL.
*
* @author Kevin Roast
*/
public class CommandServlet extends BaseServlet
{
private static final long serialVersionUID = -5432407921038376133L;
private static Log logger = LogFactory.getLog(CommandServlet.class);
private static CommandFactory commandfactory = CommandFactory.getInstance();
public static final String ARG_RETURNPAGE = "return-page";
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String uri = req.getRequestURI();
if (logger.isDebugEnabled())
logger.debug("Processing URL: " + uri + (req.getQueryString() != null ? ("?" + req.getQueryString()) : ""));
AuthenticationStatus status = servletAuthenticate(req, res);
if (status == AuthenticationStatus.Failure)
{
return;
}
StringTokenizer t = new StringTokenizer(uri, "/");
int tokenCount = t.countTokens();
if (tokenCount < 4)
{
throw new IllegalArgumentException("Command Servlet URL did not contain all required args: " + uri);
}
t.nextToken(); // skip web app name
t.nextToken(); // skip servlet name
// get the command processor to execute the command e.g. "workflow"
String procName = t.nextToken();
// get the command to perform
String command = t.nextToken();
// get any remaining uri elements to pass to the processor
String[] args = new String[tokenCount - 4];
for (int i=0; i<tokenCount-4; i++)
{
args[i] = t.nextToken();
}
try
{
// get configured command processor by name from Config Service
CommandProcessor processor = createCommandProcessor(procName);
// validate that the processor has everything it needs to run the command
ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
if (processor.validateArguments(serviceRegistry, args) == false)
{
redirectToLoginPage(req, res, getServletContext());
return;
}
UserTransaction txn = null;
try
{
txn = serviceRegistry.getTransactionService().getUserTransaction();
txn.begin();
// inform the processor to execute the specified command
processor.process(serviceRegistry, command);
// commit the transaction
txn.commit();
}
catch (Throwable txnErr)
{
try { if (txn != null) {txn.rollback();} } catch (Exception tex) {}
throw txnErr;
}
String returnPage = req.getParameter(ARG_RETURNPAGE);
if (returnPage != null && returnPage.length() != 0)
{
if (logger.isDebugEnabled())
logger.debug("Redirecting to specified return page: " + returnPage);
res.sendRedirect(returnPage);
}
else
{
if (logger.isDebugEnabled())
logger.debug("No return page specified, displaying status output.");
res.setContentType("text/html");
// request that the processor output a useful status message
PrintWriter out = res.getWriter();
processor.outputStatus(out);
out.close();
}
}
catch (Throwable err)
{
throw new AlfrescoRuntimeException("Error during command servlet processing: " + err.getMessage(), err);
}
}
/**
* @param procName
* @throws InstantiationException
* @throws IllegalAccessException
*/
private CommandProcessor createCommandProcessor(String procName) throws InstantiationException, IllegalAccessException
{
Config config = Application.getConfigService(getServletContext()).getConfig("Command Servlet");
if (config == null)
{
throw new AlfrescoRuntimeException("No command processors configured - unable to process any commands.");
}
CommandServletConfigElement configElement = (CommandServletConfigElement)
config.getConfigElement(CommandServletConfigElement.CONFIG_ELEMENT_ID);
if (configElement == null)
{
throw new AlfrescoRuntimeException("No command processors configured - unable to process any commands.");
}
Class clazz = configElement.getCommandProcessor(procName);
Object obj = clazz.newInstance();
if (obj instanceof CommandProcessor == false)
{
throw new AlfrescoRuntimeException("Configured command processor '" + procName + "' is does not implement interface CommandProcessor!");
}
return (CommandProcessor)obj;
}
}

View File

@@ -49,9 +49,9 @@ import org.apache.commons.logging.LogFactory;
* to the response stream.
* <p>
* The URL to the servlet should be generated thus:
* <pre>/alfresco/template/workspace/SpacesStore/0000-0000-0000-0000
* <pre>/alfresco/template/workspace/SpacesStore/0000-0000-0000-0000</pre>
* or
* <pre>/alfresco/template/workspace/SpacesStore/0000-0000-0000-0000/workspace/SpacesStore/0000-0000-0000-0000
* <pre>/alfresco/template/workspace/SpacesStore/0000-0000-0000-0000/workspace/SpacesStore/0000-0000-0000-0000</pre>
* <p>
* The store protocol, followed by the store ID, followed by the content Node Id used to
* identify the node to execute the default template for. The second set of elements encode

View File

@@ -1,411 +0,0 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.AccessStatus;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.web.bean.WorkflowUtil;
import org.alfresco.web.ui.common.Utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Servlet responsible for executing workflow commands upon a node.
* <p>
* The URL to the servlet should be generated thus:
* <pre>/alfresco/workflow/command/workspace/SpacesStore/0000-0000-0000-0000
* <p>
* The 'command' identifies the workflow action to execute upon the node (e.g. "approve" or "reject").
* The store protocol, followed by the store ID, followed by the content Node Id used to
* identify the node to execute the workflow action upon.
* <p>
* A 'return-page' URL argument can be specified as the redirect page to navigate too after processing.
* <p>
* Like most Alfresco servlets, the URL may be followed by a valid 'ticket' argument for authentication:
* ?ticket=1234567890
* <p>
* And/or also followed by the "?guest=true" argument to force guest access login for the URL.
*
* @author Kevin Roast
*/
public class WorkflowActionServlet extends BaseServlet
{
private static final long serialVersionUID = -3111407921997365999L;
private static Log logger = LogFactory.getLog(WorkflowActionServlet.class);
private static CommandFactory commandfactory = CommandFactory.getInstance();
public static final String ARG_RETURNPAGE = "return-page";
public static final String CMD_APPROVE = "approve";
public static final String CMD_REJECT = "reject";
private static final String DEFAULT_URL = "/workflow/{0}/{1}/{2}/{3}";
private static final String RETURN_URL = "/workflow/{0}/{1}/{2}/{3}?" + ARG_RETURNPAGE + "={4}";
static
{
// register the available Workflow commands
commandfactory.registerCommand(CMD_APPROVE, ApproveCommand.class);
commandfactory.registerCommand(CMD_REJECT, RejectCommand.class);
}
/**
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String uri = req.getRequestURI();
if (logger.isDebugEnabled())
logger.debug("Processing URL: " + uri + (req.getQueryString() != null ? ("?" + req.getQueryString()) : ""));
AuthenticationStatus status = servletAuthenticate(req, res);
if (status == AuthenticationStatus.Failure)
{
return;
}
StringTokenizer t = new StringTokenizer(uri, "/");
int tokenCount = t.countTokens();
if (tokenCount < 5)
{
throw new IllegalArgumentException("Workflow Servlet URL did not contain all required args: " + uri);
}
t.nextToken(); // skip web app name
t.nextToken(); // skip servlet name
// get the command to perform e.g. "approve"
String command = t.nextToken();
// get NodeRef to the node with the workflow attached to it
StoreRef storeRef = new StoreRef(t.nextToken(), t.nextToken());
NodeRef nodeRef = new NodeRef(storeRef, t.nextToken());
// get the services we need to execute the workflow command
ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
PermissionService permissionService = serviceRegistry.getPermissionService();
// check that the user has at least READ access on the node - else redirect to the login page
if (permissionService.hasPermission(nodeRef, PermissionService.READ) == AccessStatus.DENIED)
{
redirectToLoginPage(req, res, getServletContext());
return;
}
try
{
UserTransaction txn = null;
try
{
txn = serviceRegistry.getTransactionService().getUserTransaction();
txn.begin();
// find the workflow command from the registered list of commands
// the CommandFactory supplies use with an instance of the command to use
Map<String, Object> properties = new HashMap<String, Object>(1, 1.0f);
properties.put("target", nodeRef);
Command cmd = commandfactory.createCommand(command, properties);
if (cmd == null)
{
throw new AlfrescoRuntimeException("Unknown workflow command specified: " + command);
}
cmd.execute(serviceRegistry);
// commit the transaction
txn.commit();
}
catch (Throwable txnErr)
{
try { if (txn != null) {txn.rollback();} } catch (Exception tex) {}
throw txnErr;
}
}
catch (Throwable err)
{
// TODO: could show error status output here instead of throwing an exception?
throw new AlfrescoRuntimeException("Error during workflow servlet processing: " + err.getMessage(), err);
}
String returnPage = req.getParameter(ARG_RETURNPAGE);
if (returnPage != null && returnPage.length() != 0)
{
if (logger.isDebugEnabled())
logger.debug("Redirecting to specified return page: " + returnPage);
res.sendRedirect(returnPage);
}
else
{
if (logger.isDebugEnabled())
logger.debug("No return page specified, displaying status output.");
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.print("Workflow command: '");
out.print(command);
out.print("' executed against node: ");
out.println(nodeRef.toString());
out.close();
}
}
/**
* Helper to generate a URL to process a workflow action against a node.
* <p>
* The result of the workflow action is supplied returned as the response.
*
* @param nodeRef NodeRef of the node to generate URL for (cannot be null)
* @param action Workflow action (See constants) to execute on the node
*
* @return URL to process the workflow action
*/
public final static String generateURL(NodeRef nodeRef, String action)
{
return MessageFormat.format(DEFAULT_URL, new Object[] {
action,
nodeRef.getStoreRef().getProtocol(),
nodeRef.getStoreRef().getIdentifier(),
nodeRef.getId() } );
}
/**
* Helper to generate a URL to process a workflow action against a node.
* <p>
* The result of the workflow action is supplied returned as the response.
*
* @param nodeRef NodeRef of the node to generate URL for (cannot be null)
* @param action Workflow action (See constants) to execute on the node
* @param returnPage Return page URL to redirect after success (e.g. /alfresco/navigate/browse)
*
* @return URL to process the workflow action
*/
public final static String generateURL(NodeRef nodeRef, String action, String returnPage)
{
try
{
return MessageFormat.format(RETURN_URL, new Object[] {
action,
nodeRef.getStoreRef().getProtocol(),
nodeRef.getStoreRef().getIdentifier(),
nodeRef.getId(),
Utils.replace(URLEncoder.encode(returnPage, "UTF-8"), "+", "%20")} );
}
catch (UnsupportedEncodingException uee)
{
throw new AlfrescoRuntimeException("Failed to encode workflow URL for node: " + nodeRef, uee);
}
}
/**
* Simple command pattern interface
*/
interface Command
{
/**
* Execute the command
*
* @param serviceRegistry The ServiceRegistry instance
*/
void execute(ServiceRegistry serviceRegistry);
/**
* @param properties bag of named properties for the command
*/
void setProperties(Map<String, Object> properties);
}
/**
* Base command class
*/
static abstract class BaseCommand implements Command
{
Map<String, Object> properties = Collections.<String, Object>emptyMap();
/**
* @see org.alfresco.web.app.servlet.WorkflowActionServlet.Command#setProperties(java.util.Map)
*/
public void setProperties(Map<String, Object> properties)
{
if (properties != null)
{
this.properties = properties;
}
}
}
/**
* Approve Workflow command implementation
*/
static final class ApproveCommand extends BaseCommand
{
static final String PROP_TARGET = "target";
/**
* @see org.alfresco.web.app.servlet.WorkflowActionServlet.Command#execute(org.alfresco.service.ServiceRegistry)
*/
public void execute(ServiceRegistry serviceRegistry)
{
NodeRef nodeRef = (NodeRef)this.properties.get(PROP_TARGET);
if (nodeRef == null)
{
throw new IllegalArgumentException(
"Unable to execute ApproveCommand - mandatory parameter not supplied: " + PROP_TARGET);
}
WorkflowUtil.approve(nodeRef, serviceRegistry.getNodeService(), serviceRegistry.getCopyService());
}
}
/**
* Reject Workflow command implementation
*/
static final class RejectCommand extends BaseCommand
{
static final String PROP_TARGET = "target";
/**
* @see org.alfresco.web.app.servlet.WorkflowActionServlet.Command#execute(org.alfresco.service.ServiceRegistry)
*/
public void execute(ServiceRegistry serviceRegistry)
{
NodeRef nodeRef = (NodeRef)this.properties.get(PROP_TARGET);
if (nodeRef == null)
{
throw new IllegalArgumentException(
"Unable to execute RejectCommand - mandatory parameter not supplied: " + PROP_TARGET);
}
WorkflowUtil.reject(nodeRef, serviceRegistry.getNodeService(), serviceRegistry.getCopyService());
}
}
/**
* Command Factory helper
*/
static final class CommandFactory
{
private static CommandFactory instance = new CommandFactory();
private static Map<String, Class> registry = new HashMap<String, Class>(4, 1.0f);
/**
* Private constructor - protect the singleton instance
*/
private CommandFactory()
{
}
/**
* @return the singleton CommandFactory instance
*/
static CommandFactory getInstance()
{
return instance;
}
/**
* Register a command name against an implementation
*
* @param name Unique name of the command
* @param clazz Class implementation of the command
*/
void registerCommand(String name, Class clazz)
{
registry.put(name, clazz);
}
/**
* Create a command instance of the specified command name
*
* @param name Name of the command to create (must be registered)
*
* @return the Command instance or null if not found
*/
Command createCommand(String name)
{
return createCommand(name, null);
}
/**
* Create a command instance of the specified command name
*
* @param name Name of the command to create (must be registered)
* @param properties Bag of name/value properties to pass to command upon creation
*
* @return the Command instance or null if not found
*/
Command createCommand(String name, Map<String, Object> properties)
{
Command result = null;
// lookup command by name in the registry
Class clazz = registry.get(name);
if (clazz != null)
{
try
{
Object obj = clazz.newInstance();
if (obj instanceof Command)
{
result = (Command)obj;
if (properties != null)
{
result.setProperties(properties);
}
}
}
catch (Throwable err)
{
// return default if this occurs
logger.warn("Unable to create workflow command instance '" + name +
"' with classname '" + clazz.getName() + "' due to error: " + err.getMessage());
}
}
return result;
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet.command;
import java.util.Map;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.web.bean.WorkflowUtil;
/**
* Approve Workflow command implementation
*
* @author Kevin Roast
*/
public final class ApproveWorkflowCommand implements Command
{
public static final String PROP_TARGET = "target";
private static final String[] PROPERTIES = new String[] {PROP_TARGET};
/**
* @see org.alfresco.web.app.servlet.command.Command#getPropertyNames()
*/
public String[] getPropertyNames()
{
return PROPERTIES;
}
/**
* @see org.alfresco.web.app.servlet.command.Command#execute(org.alfresco.service.ServiceRegistry, java.util.Map)
*/
public void execute(ServiceRegistry serviceRegistry, Map<String, Object> properties)
{
// get the target Node for the command
NodeRef nodeRef = (NodeRef)properties.get(PROP_TARGET);
if (nodeRef == null)
{
throw new IllegalArgumentException(
"Unable to execute ApproveCommand - mandatory parameter not supplied: " + PROP_TARGET);
}
WorkflowUtil.approve(nodeRef, serviceRegistry.getNodeService(), serviceRegistry.getCopyService());
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet.command;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.security.AccessStatus;
import org.alfresco.service.cmr.security.PermissionService;
/**
* Initial implementation of a Command Processor that is always passed enough URL elements
* to construct a single NodeRef argument. The NodeRef is checked against READ permissions
* for the current user during the validateArguments() call.
* <p>
* This class should be enough to form the base of Command Processor objects that only require
* a single NodeRef passed on the URL.
*
* @author Kevin Roast
*/
public abstract class BaseNodeCommandProcessor implements CommandProcessor
{
protected NodeRef targetRef;
/**
* @see org.alfresco.web.app.servlet.command.CommandProcessor#validateArguments(org.alfresco.service.ServiceRegistry, java.lang.String[])
*/
public boolean validateArguments(ServiceRegistry serviceRegistry, String[] args)
{
if (args.length < 3)
{
throw new IllegalArgumentException("Not enough URL arguments passed to command servlet.");
}
// get NodeRef to the node with the workflow attached to it
StoreRef storeRef = new StoreRef(args[0], args[1]);
this.targetRef = new NodeRef(storeRef, args[2]);
// get the services we need to execute the workflow command
PermissionService permissionService = serviceRegistry.getPermissionService();
// check that the user has at least READ access on the node - else redirect to the login page
return (permissionService.hasPermission(this.targetRef, PermissionService.READ) == AccessStatus.ALLOWED);
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet.command;
import java.util.Map;
import org.alfresco.service.ServiceRegistry;
/**
* Simple servlet command pattern interface.
*
* @author Kevin Roast
*/
public interface Command
{
/**
* Execute the command
*
* @param serviceRegistry The ServiceRegistry instance
* @param properties Bag of named properties for the command
*/
public void execute(ServiceRegistry serviceRegistry, Map<String, Object> properties);
/**
* @return the names of the properties required for this command
*/
public String[] getPropertyNames();
}

View File

@@ -0,0 +1,97 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet.command;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Command Factory helper
*
* @author Kevin Roast
*/
public final class CommandFactory
{
private static Log logger = LogFactory.getLog(CommandFactory.class);
private static CommandFactory instance = new CommandFactory();
private static Map<String, Class> registry = new HashMap<String, Class>(16, 1.0f);
/**
* Private constructor - protect the singleton instance
*/
private CommandFactory()
{
}
/**
* @return the singleton CommandFactory instance
*/
public static CommandFactory getInstance()
{
return instance;
}
/**
* Register a command name against an implementation
*
* @param name Unique name of the command
* @param clazz Class implementation of the command
*/
public void registerCommand(String name, Class clazz)
{
registry.put(name, clazz);
}
/**
* Create a command instance of the specified command name
*
* @param name Name of the command to create (must be registered)
*
* @return the Command instance or null if not found
*/
public Command createCommand(String name)
{
Command result = null;
// lookup command by name in the registry
Class clazz = registry.get(name);
if (clazz != null)
{
try
{
Object obj = clazz.newInstance();
if (obj instanceof Command)
{
result = (Command)obj;
}
}
catch (Throwable err)
{
// return default if this occurs
logger.warn("Unable to create workflow command instance '" + name +
"' with classname '" + clazz.getName() + "' due to error: " + err.getMessage());
}
}
return result;
}
}

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet.command;
import java.io.PrintWriter;
import org.alfresco.service.ServiceRegistry;
/**
* This interfaces defines the contract and lifecycle of a Servlet Command Processor.
* <p>
* A command processor is defined as a class capable of executing a set of related Command
* objects. It performs the bulk of the work for the command servlet. The processor impl
* is responsible for validating that the command can be processed (given the supplied remaining
* URL arguments from the servlet) and processing the command. It is also responsible for
* supply an output status page on successfuly execution of the command.
* <p>
* The arguments passed to a Command Processor are the remaining URL elements from the command
* servlet URL after removing the web-app name, servlet name and command processor name.
*
* @author Kevin Roast
*/
public interface CommandProcessor
{
/**
* Pass and validate URL arguments for the command processor. Validate if the command can be
* executed given the arguments supplied. Generally at this post a Command Processor will
* convert the supplied arguments to the objects it expects, and also check any permissions
* that are required by the current user to execute the command.
*
* @param args String[] of the remaining URL arguments to the command servlet.
*
* @return true if the command can be executed by the current user given the supplied args.
*/
public boolean validateArguments(ServiceRegistry serviceRegistry, String[] args);
/**
* Process the supplied command name. It is the responsibility of the Command Processor
* to lookup the specified command name using the CommandFactory registry. For that reason
* it also has the responsiblity to initially register commands it is responsible for so
* they can be constructed later. If the supplied command is unknown to it then an
* exception should be thrown to indicate this.
*
* @param serviceRegistry serviceRegistry
* @param command Name of the command to construct and execute
*/
public void process(ServiceRegistry serviceRegistry, String command);
/**
* Output a simple status message to the supplied PrintWriter.
* It can be assumed that the process() method was successful if this method is called.
*
* @param out PrintWriter
*/
public void outputStatus(PrintWriter out);
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet.command;
import java.util.Map;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.web.bean.WorkflowUtil;
/**
* Reject Workflow command implementation
*
* @author Kevin Roast
*/
public final class RejectWorkflowCommand implements Command
{
public static final String PROP_TARGET = "target";
private static final String[] PROPERTIES = new String[] {PROP_TARGET};
/**
* @see org.alfresco.web.app.servlet.command.Command#getPropertyNames()
*/
public String[] getPropertyNames()
{
return PROPERTIES;
}
/**
* @see org.alfresco.web.app.servlet.command.Command#execute(org.alfresco.service.ServiceRegistry, java.util.Map)
*/
public void execute(ServiceRegistry serviceRegistry, Map<String, Object> properties)
{
// get the target Node for the command
NodeRef nodeRef = (NodeRef)properties.get(PROP_TARGET);
if (nodeRef == null)
{
throw new IllegalArgumentException(
"Unable to execute RejectCommand - mandatory parameter not supplied: " + PROP_TARGET);
}
WorkflowUtil.reject(nodeRef, serviceRegistry.getNodeService(), serviceRegistry.getCopyService());
}
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.web.app.servlet.command;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.service.ServiceRegistry;
/**
* Workflow specific command processor implementation.
* <p>
* Responsible for executing 'approve' and 'reject' workflow commands on a Node.
*
* @author Kevin Roast
*/
public final class WorkflowCommandProcessor extends BaseNodeCommandProcessor
{
private String command;
static
{
// add our commands to the command registry
CommandFactory.getInstance().registerCommand("approve", ApproveWorkflowCommand.class);
CommandFactory.getInstance().registerCommand("reject", RejectWorkflowCommand.class);
}
/**
* @see org.alfresco.web.app.servlet.command.CommandProcessor#process(org.alfresco.service.ServiceRegistry, java.lang.String)
*/
public void process(ServiceRegistry serviceRegistry, String command)
{
Map<String, Object> properties = new HashMap<String, Object>(1, 1.0f);
// all workflow commands use a "target" Node property as an argument
properties.put(ApproveWorkflowCommand.PROP_TARGET, this.targetRef);
Command cmd = CommandFactory.getInstance().createCommand(command);
if (cmd == null)
{
throw new AlfrescoRuntimeException("Unregistered workflow command specified: " + command);
}
cmd.execute(serviceRegistry, properties);
this.command = command;
}
/**
* @see org.alfresco.web.app.servlet.command.CommandProcessor#outputStatus(java.io.PrintWriter)
*/
public void outputStatus(PrintWriter out)
{
out.print("Workflow command: '");
out.print(this.command);
out.print("' executed against node: ");
out.println(this.targetRef.toString());
}
}