Search in sources :

Example 1 with TransferProgress

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

HttpServletRequest (javax.servlet.http.HttpServletRequest)1 TransferException (org.alfresco.service.cmr.transfer.TransferException)1 TransferProgress (org.alfresco.service.cmr.transfer.TransferProgress)1 JSONObject (org.json.JSONObject)1 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)1 WrappingWebScriptRequest (org.springframework.extensions.webscripts.WrappingWebScriptRequest)1 WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)1