Search in sources :

Example 1 with WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest in project alfresco-remote-api by Alfresco.

the class AbortTransferCommandProcessor method process.

/*
     * (non-Javadoc)
     * 
     * @see org.alfresco.repo.web.scripts.transfer.CommandProcessor#process(org.alfresco .web.scripts.WebScriptRequest,
     * org.alfresco.web.scripts.WebScriptResponse)
     */
public int process(WebScriptRequest req, WebScriptResponse resp) {
    String transferRecordId = null;
    // Read the transfer id from the request
    // Unwrap to a WebScriptServletRequest if we have one
    // TODO: Why is this necessary?
    WebScriptServletRequest webScriptServletRequest = null;
    WebScriptRequest current = req;
    do {
        if (current instanceof WebScriptServletRequest) {
            webScriptServletRequest = (WebScriptServletRequest) current;
            current = null;
        } else if (current instanceof WrappingWebScriptRequest) {
            current = ((WrappingWebScriptRequest) req).getNext();
        } else {
            current = null;
        }
    } while (current != null);
    HttpServletRequest servletRequest = webScriptServletRequest.getHttpServletRequest();
    String transferId = servletRequest.getParameter("transferId");
    if ((transferId == null)) {
        resp.setStatus(Status.STATUS_BAD_REQUEST);
        return Status.STATUS_BAD_REQUEST;
    }
    try {
        logger.debug("abort transfer:" + transferId);
        receiver.cancel(transferId);
        // return the unique transfer id (the lock id)
        StringWriter stringWriter = new StringWriter(300);
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        jsonWriter.startObject();
        jsonWriter.writeValue("transferId", transferRecordId);
        jsonWriter.endObject();
        String response = stringWriter.toString();
        resp.setContentType("application/json");
        resp.setContentEncoding("UTF-8");
        int length = response.getBytes("UTF-8").length;
        resp.addHeader("Content-Length", "" + length);
        resp.setStatus(Status.STATUS_OK);
        resp.getWriter().write(response);
        return Status.STATUS_OK;
    } catch (Exception ex) {
        logger.debug("caught exception", ex);
        if (ex instanceof TransferException) {
            throw (TransferException) ex;
        }
        throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
    }
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) TransferException(org.alfresco.service.cmr.transfer.TransferException) StringWriter(java.io.StringWriter) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Example 2 with WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest in project alfresco-remote-api by Alfresco.

the class CommitTransferCommandProcessor method process.

/*
     * (non-Javadoc)
     * 
     * @see org.alfresco.repo.web.scripts.transfer.CommandProcessor#process(org.alfresco .web.scripts.WebScriptRequest,
     * org.alfresco.web.scripts.WebScriptResponse)
     */
public int process(WebScriptRequest req, WebScriptResponse resp) {
    // Read the transfer id from the request
    // Unwrap to a WebScriptServletRequest if we have one
    WebScriptServletRequest webScriptServletRequest = null;
    WebScriptRequest current = req;
    do {
        if (current instanceof WebScriptServletRequest) {
            webScriptServletRequest = (WebScriptServletRequest) current;
            current = null;
        } else if (current instanceof WrappingWebScriptRequest) {
            current = ((WrappingWebScriptRequest) req).getNext();
        } else {
            current = null;
        }
    } while (current != null);
    HttpServletRequest servletRequest = webScriptServletRequest.getHttpServletRequest();
    String transferId = servletRequest.getParameter("transferId");
    if ((transferId == null)) {
        logger.debug("transferId is missing");
        resp.setStatus(Status.STATUS_BAD_REQUEST);
        return Status.STATUS_BAD_REQUEST;
    }
    try {
        receiver.commitAsync(transferId);
        // return the unique transfer id (the lock id)
        StringWriter stringWriter = new StringWriter(300);
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        jsonWriter.startObject();
        jsonWriter.writeValue("transferId", transferId);
        jsonWriter.endObject();
        String response = stringWriter.toString();
        resp.setContentType("application/json");
        resp.setContentEncoding("UTF-8");
        int length = response.getBytes("UTF-8").length;
        resp.addHeader("Content-Length", "" + length);
        resp.setStatus(Status.STATUS_OK);
        resp.getWriter().write(response);
        return Status.STATUS_OK;
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("caught exception :" + ex.toString(), ex);
        }
        if (ex instanceof TransferException) {
            throw (TransferException) ex;
        }
        throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
    }
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) TransferException(org.alfresco.service.cmr.transfer.TransferException) StringWriter(java.io.StringWriter) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Example 3 with WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest in project alfresco-remote-api by Alfresco.

the class PostContentCommandProcessor method process.

/*
     * (non-Javadoc)
     * 
     * @see org.alfresco.repo.web.scripts.transfer.CommandProcessor#process(org.alfresco.web.scripts.WebScriptRequest,
     * org.alfresco.web.scripts.WebScriptResponse)
     */
public int process(WebScriptRequest req, WebScriptResponse resp) {
    logger.debug("post content start");
    // Unwrap to a WebScriptServletRequest if we have one
    WebScriptServletRequest webScriptServletRequest = null;
    WebScriptRequest current = req;
    do {
        if (current instanceof WebScriptServletRequest) {
            webScriptServletRequest = (WebScriptServletRequest) current;
            current = null;
        } else if (current instanceof WrappingWebScriptRequest) {
            current = ((WrappingWebScriptRequest) req).getNext();
        } else {
            current = null;
        }
    } while (current != null);
    if (webScriptServletRequest == null) {
        resp.setStatus(Status.STATUS_BAD_REQUEST);
        return Status.STATUS_BAD_REQUEST;
    }
    HttpServletRequest servletRequest = webScriptServletRequest.getHttpServletRequest();
    // Read the transfer id from the request
    String transferId = servletRequest.getParameter("transferId");
    if ((transferId == null) || !ServletFileUpload.isMultipartContent(servletRequest)) {
        resp.setStatus(Status.STATUS_BAD_REQUEST);
        return Status.STATUS_BAD_REQUEST;
    }
    try {
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iter = upload.getItemIterator(servletRequest);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            String name = item.getFieldName();
            if (!item.isFormField()) {
                logger.debug("got content Mime Part : " + name);
                receiver.saveContent(transferId, item.getName(), item.openStream());
            }
        }
        // WebScriptServletRequest alfRequest = (WebScriptServletRequest)req;
        // String[] names = alfRequest.getParameterNames();
        // for(String name : names)
        // {
        // FormField item = alfRequest.getFileField(name);
        // 
        // if(item != null)
        // {
        // logger.debug("got content Mime Part : " + name);
        // receiver.saveContent(transferId, item.getName(), item.getInputStream());
        // }
        // else
        // {
        // //TODO - should this be an exception?
        // logger.debug("Unable to get content for Mime Part : " + name);
        // }
        // }
        logger.debug("success");
        resp.setStatus(Status.STATUS_OK);
    } catch (Exception ex) {
        logger.debug("exception caught", ex);
        if (transferId != null) {
            logger.debug("ending transfer", ex);
            receiver.end(transferId);
        }
        if (ex instanceof TransferException) {
            throw (TransferException) ex;
        }
        throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
    }
    resp.setStatus(Status.STATUS_OK);
    return Status.STATUS_OK;
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) TransferException(org.alfresco.service.cmr.transfer.TransferException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Example 4 with WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest in project alfresco-remote-api by Alfresco.

the class PostSnapshotCommandProcessor method process.

/* (non-Javadoc)
     * @see org.alfresco.repo.web.scripts.transfer.CommandProcessor#process(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse)
     */
public int process(WebScriptRequest req, WebScriptResponse resp) {
    int result = Status.STATUS_OK;
    // Unwrap to a WebScriptServletRequest if we have one
    WebScriptServletRequest webScriptServletRequest = null;
    WebScriptRequest current = req;
    do {
        if (current instanceof WebScriptServletRequest) {
            webScriptServletRequest = (WebScriptServletRequest) current;
            current = null;
        } else if (current instanceof WrappingWebScriptRequest) {
            current = ((WrappingWebScriptRequest) req).getNext();
        } else {
            current = null;
        }
    } while (current != null);
    if (webScriptServletRequest == null) {
        logger.debug("bad request, not assignable from");
        resp.setStatus(Status.STATUS_BAD_REQUEST);
        return Status.STATUS_BAD_REQUEST;
    }
    // We can't use the WebScriptRequest version of getParameter, since that may cause the content stream
    // to be parsed. Get hold of the raw HttpServletRequest and work with that.
    HttpServletRequest servletRequest = webScriptServletRequest.getHttpServletRequest();
    // Read the transfer id from the request
    String transferId = servletRequest.getParameter("transferId");
    if ((transferId == null) || !ServletFileUpload.isMultipartContent(servletRequest)) {
        logger.debug("bad request, not multipart");
        resp.setStatus(Status.STATUS_BAD_REQUEST);
        return Status.STATUS_BAD_REQUEST;
    }
    try {
        logger.debug("about to upload manifest file");
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iter = upload.getItemIterator(servletRequest);
        while (iter.hasNext()) {
            FileItemStream item = iter.next();
            if (!item.isFormField() && TransferCommons.PART_NAME_MANIFEST.equals(item.getFieldName())) {
                logger.debug("got manifest file");
                receiver.saveSnapshot(transferId, item.openStream());
            }
        }
        logger.debug("success");
        resp.setStatus(Status.STATUS_OK);
        OutputStream out = resp.getOutputStream();
        resp.setContentType("text/xml");
        resp.setContentEncoding("utf-8");
        receiver.generateRequsite(transferId, out);
        out.close();
    } catch (Exception ex) {
        logger.debug("exception caught", ex);
        if (transferId != null) {
            logger.debug("ending transfer", ex);
            receiver.end(transferId);
        }
        if (ex instanceof TransferException) {
            throw (TransferException) ex;
        }
        throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
    }
    return result;
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) TransferException(org.alfresco.service.cmr.transfer.TransferException) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileItemStream(org.apache.commons.fileupload.FileItemStream) OutputStream(java.io.OutputStream) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Example 5 with WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest in project alfresco-remote-api by Alfresco.

the class BaseRemoteStore method execute.

/**
 * Execute the webscript based on the request parameters
 */
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
    // NOTE: This web script must be executed in a HTTP Servlet environment
    // Unwrap to a WebScriptServletRequest if we have one
    WebScriptServletRequest webScriptServletRequest = null;
    WebScriptRequest current = req;
    do {
        if (current instanceof WebScriptServletRequest) {
            webScriptServletRequest = (WebScriptServletRequest) current;
            current = null;
        } else if (current instanceof WrappingWebScriptRequest) {
            current = ((WrappingWebScriptRequest) req).getNext();
        } else {
            current = null;
        }
    } while (current != null);
    if (webScriptServletRequest == null) {
        throw new WebScriptException("Remote Store access must be executed in HTTP Servlet environment");
    }
    HttpServletRequest httpReq = webScriptServletRequest.getHttpServletRequest();
    // the request path for the remote store
    String extPath = req.getExtensionPath();
    // values that we need to determine
    String methodName = null;
    String store = null;
    StringBuilder pathBuilder = new StringBuilder(128);
    // tokenize the path and figure out tokenized values
    StringTokenizer tokenizer = new StringTokenizer(extPath, "/");
    if (tokenizer.hasMoreTokens()) {
        methodName = tokenizer.nextToken();
        if (tokenizer.hasMoreTokens()) {
            String el = tokenizer.nextToken();
            if (TOKEN_STORE.equals(el)) {
                // if the token is TOKEN_STORE, then the next token is the id of the store
                store = tokenizer.nextToken();
                // reset element
                el = (tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null);
            }
            while (el != null) {
                pathBuilder.append('/');
                pathBuilder.append(el);
                el = (tokenizer.hasMoreTokens() ? tokenizer.nextToken() : null);
            }
        }
    } else {
        throw new WebScriptException("Unable to tokenize web path: " + extPath);
    }
    // if we don't have a store, check whether it came in on a request parameter
    if (store == null) {
        store = req.getParameter(REQUEST_PARAM_STORE);
        if (store == null) {
            store = this.defaultStore;
        }
        if (store == null) {
            // this means that a store was not passed in and that we also didn't have a configured store
            throw new WebScriptException("Unable to determine which store to operate against." + " A store was not specified and a default was not provided.");
        }
    }
    String path = pathBuilder.toString();
    long start = 0;
    if (logger.isDebugEnabled()) {
        logger.debug("Remote method: " + methodName.toUpperCase() + "   Store Id: " + store + "   Path: " + path);
        start = System.nanoTime();
    }
    try {
        // generate enum from string method name - so we can use a fast switch table lookup
        APIMethod method = APIMethod.valueOf(methodName.toUpperCase());
        switch(method) {
            case LASTMODIFIED:
                validatePath(path);
                lastModified(res, store, path);
                break;
            case HAS:
                validatePath(path);
                hasDocument(res, store, path);
                break;
            case GET:
                validatePath(path);
                getDocument(res, store, path);
                break;
            case LIST:
                listDocuments(res, store, path, false);
                break;
            case LISTALL:
                listDocuments(res, store, path, true);
                break;
            case LISTPATTERN:
                listDocuments(res, store, path, req.getParameter("m"));
                break;
            case CREATE:
                validatePath(path);
                if (logger.isDebugEnabled())
                    logger.debug("CREATE: content length=" + httpReq.getContentLength());
                createDocument(res, store, path, httpReq.getInputStream());
                break;
            case CREATEMULTI:
                if (logger.isDebugEnabled())
                    logger.debug("CREATEMULTI: content length=" + httpReq.getContentLength());
                createDocuments(res, store, httpReq.getInputStream());
                break;
            case UPDATE:
                validatePath(path);
                if (logger.isDebugEnabled())
                    logger.debug("UPDATE: content length=" + httpReq.getContentLength());
                updateDocument(res, store, path, httpReq.getInputStream());
                break;
            case DELETE:
                validatePath(path);
                deleteDocument(res, store, path);
                break;
        }
    } catch (IllegalArgumentException enumErr) {
        throw new WebScriptException("Unknown method specified to remote store API: " + methodName);
    } catch (IOException ioErr) {
        throw new WebScriptException("Error during remote store API: " + ioErr.getMessage());
    }
    if (logger.isDebugEnabled()) {
        long end = System.nanoTime();
        logger.debug("Time to execute method: " + (end - start) / 1000000f + "ms");
    }
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) StringTokenizer(java.util.StringTokenizer) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) IOException(java.io.IOException)

Aggregations

WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)10 WrappingWebScriptRequest (org.springframework.extensions.webscripts.WrappingWebScriptRequest)10 WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 TransferException (org.alfresco.service.cmr.transfer.TransferException)7 StringWriter (java.io.StringWriter)3 JSONWriter (org.springframework.extensions.webscripts.json.JSONWriter)3 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)2 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)2 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 StringTokenizer (java.util.StringTokenizer)1 ACPImportPackageHandler (org.alfresco.repo.importer.ACPImportPackageHandler)1