/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see
* The URL to the servlet should be generated thus: *
/alfresco/command/processor-name/command-name/args/...*
* 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: *
/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. *
* A 'return-page' URL argument can be specified as the redirect page to navigate too after processing. *
* Like most Alfresco servlets, the URL may be followed by a valid 'ticket' argument for authentication: * ?ticket=1234567890 *
* 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 service(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;
}
setNoCacheHeaders(res);
uri = uri.substring(req.getContextPath().length());
StringTokenizer t = new StringTokenizer(uri, "/");
int tokenCount = t.countTokens();
if (tokenCount < 3)
{
throw new IllegalArgumentException("Command Servlet URL did not contain all required args: " + uri);
}
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[] urlElements = new String[tokenCount - 3];
for (int i=0; i
*
*
* @param pageUrl
* @param req
* @throws MalformedURLException
* @throws IllegalArgumentException
*/
private void validateReturnPage(String pageUrl, HttpServletRequest req) throws MalformedURLException
{
if (pageUrl.indexOf(':') != -1)
{
// ':' only allowed in a URL as part of a scheme prefix
throw new IllegalArgumentException("The redirect URL doesn't support absolute URls");
}
// Evaluate it relative to the request URL and strip out .. and .
pageUrl = new URL(new URL(req.getRequestURL().toString()), pageUrl).getPath();
if (!pageUrl.startsWith(req.getContextPath()))
{
throw new IllegalArgumentException("The redirect URL must be in the same context.");
}
}
/**
* Created the specified CommandProcessor instance. The name of the processor is looked up
* in the client config, it should find a valid class impl and then create it.
*
* @param procName Name of the CommandProcessor to lookup in the client config.
*
* @return CommandProcessor
*
* @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;
}
}