Search in sources :

Example 6 with TransferException

use of org.alfresco.service.cmr.transfer.TransferException 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 7 with TransferException

use of org.alfresco.service.cmr.transfer.TransferException in project alfresco-remote-api by Alfresco.

the class PrepareTransferCommandProcessor 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
    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 {
        logger.debug("prepare transferId: " + transferId);
        receiver.prepare(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);
        logger.debug("prepared transferId: " + transferId);
        return Status.STATUS_OK;
    } catch (Exception ex) {
        logger.debug("in exception handler", ex);
        receiver.end(transferRecordId);
        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 8 with TransferException

use of org.alfresco.service.cmr.transfer.TransferException in project alfresco-remote-api by Alfresco.

the class ReportCommandProcessor 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 {
        OutputStream out = resp.getOutputStream();
        try {
            resp.setContentType("text/xml");
            resp.setContentEncoding("utf-8");
            BufferedInputStream br = new BufferedInputStream(receiver.getProgressMonitor().getLogInputStream(transferId));
            try {
                byte[] buffer = new byte[1000];
                int i = br.read(buffer);
                while (i > 0) {
                    out.write(buffer, 0, i);
                    i = br.read(buffer);
                }
            } finally {
                br.close();
            }
        } finally {
            out.flush();
            out.close();
        }
        return Status.STATUS_OK;
    } catch (TransferException ex) {
        throw ex;
    } catch (Exception 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) TransferException(org.alfresco.service.cmr.transfer.TransferException) BufferedInputStream(java.io.BufferedInputStream) OutputStream(java.io.OutputStream) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Example 9 with TransferException

use of org.alfresco.service.cmr.transfer.TransferException in project alfresco-remote-api by Alfresco.

the class StatusCommandProcessor 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 {
        TransferProgress progress = receiver.getProgressMonitor().getProgress(transferId);
        if (logger.isDebugEnabled()) {
            logger.debug(progress);
        }
        JSONObject progressObject = new JSONObject();
        progressObject.put("transferId", transferId);
        progressObject.put("status", progress.getStatus().toString());
        progressObject.put("currentPosition", progress.getCurrentPosition());
        progressObject.put("endPosition", progress.getEndPosition());
        if (progress.getError() != null) {
            JSONObject errorObject = errorSerializer.serialize(progress.getError());
            progressObject.put("error", errorObject);
        }
        String response = progressObject.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 (TransferException ex) {
        throw ex;
    } catch (Exception 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) TransferException(org.alfresco.service.cmr.transfer.TransferException) JSONObject(org.json.JSONObject) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) TransferException(org.alfresco.service.cmr.transfer.TransferException) TransferProgress(org.alfresco.service.cmr.transfer.TransferProgress)

Aggregations

TransferException (org.alfresco.service.cmr.transfer.TransferException)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)7 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)7 WrappingWebScriptRequest (org.springframework.extensions.webscripts.WrappingWebScriptRequest)7 WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)7 StringWriter (java.io.StringWriter)5 JSONWriter (org.springframework.extensions.webscripts.json.JSONWriter)5 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 BufferedInputStream (java.io.BufferedInputStream)1 TransferVersionImpl (org.alfresco.repo.transfer.TransferVersionImpl)1 TransferProgress (org.alfresco.service.cmr.transfer.TransferProgress)1 TransferVersion (org.alfresco.service.cmr.transfer.TransferVersion)1 JSONObject (org.json.JSONObject)1