Search in sources :

Example 1 with ExtCommandProcessor

use of org.alfresco.web.app.servlet.command.ExtCommandProcessor 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

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 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 ServiceRegistry (org.alfresco.service.ServiceRegistry)1 CommandProcessor (org.alfresco.web.app.servlet.command.CommandProcessor)1 ExtCommandProcessor (org.alfresco.web.app.servlet.command.ExtCommandProcessor)1