Search in sources :

Example 1 with CommandProcessor

use of org.alfresco.web.app.servlet.command.CommandProcessor in project acs-community-packaging by Alfresco.

the class CommandServlet method createCommandProcessor.

/**
 * 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;
}
Also used : Config(org.springframework.extensions.config.Config) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ExtCommandProcessor(org.alfresco.web.app.servlet.command.ExtCommandProcessor) CommandProcessor(org.alfresco.web.app.servlet.command.CommandProcessor) CommandServletConfigElement(org.alfresco.web.config.CommandServletConfigElement)

Example 2 with CommandProcessor

use of org.alfresco.web.app.servlet.command.CommandProcessor in project acs-community-packaging by Alfresco.

the class CommandServlet method service.

/**
 * @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);
    }
    // skip servlet name
    t.nextToken();
    // 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 < tokenCount - 3; i++) {
        urlElements[i] = t.nextToken();
    }
    // retrieve the URL arguments to pass to the processor
    Map<String, String> args = new HashMap<String, String>(8, 1.0f);
    Enumeration names = req.getParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        args.put(name, req.getParameter(name));
    }
    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
        if (processor.validateArguments(getServletContext(), command, args, urlElements) == false) {
            redirectToLoginPage(req, res, getServletContext());
            return;
        }
        ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
        UserTransaction txn = null;
        try {
            txn = serviceRegistry.getTransactionService().getUserTransaction();
            txn.begin();
            // inform the processor to execute the specified command
            if (processor instanceof ExtCommandProcessor) {
                ((ExtCommandProcessor) processor).process(serviceRegistry, req, res, command);
            } else {
                processor.process(serviceRegistry, req, 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) {
            validateReturnPage(returnPage, req);
            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.");
            if (res.getContentType() == null && !res.isCommitted()) {
                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);
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) ExtCommandProcessor(org.alfresco.web.app.servlet.command.ExtCommandProcessor) MalformedURLException(java.net.MalformedURLException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) StringTokenizer(java.util.StringTokenizer) ExtCommandProcessor(org.alfresco.web.app.servlet.command.ExtCommandProcessor) CommandProcessor(org.alfresco.web.app.servlet.command.CommandProcessor) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ServiceRegistry(org.alfresco.service.ServiceRegistry) PrintWriter(java.io.PrintWriter)

Aggregations

AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)2 CommandProcessor (org.alfresco.web.app.servlet.command.CommandProcessor)2 ExtCommandProcessor (org.alfresco.web.app.servlet.command.ExtCommandProcessor)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 MalformedURLException (java.net.MalformedURLException)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 ServletException (javax.servlet.ServletException)1 UserTransaction (javax.transaction.UserTransaction)1 ServiceRegistry (org.alfresco.service.ServiceRegistry)1 CommandServletConfigElement (org.alfresco.web.config.CommandServletConfigElement)1 Config (org.springframework.extensions.config.Config)1