Search in sources :

Example 1 with TransferException

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

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

the class BeginTransferCommandProcessor 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 transferId = null;
    try {
        String[] fromRepositoryIdValues = req.getParameterValues(TransferCommons.PARAM_FROM_REPOSITORYID);
        String[] transferToSelfValues = req.getParameterValues(TransferCommons.PARAM_ALLOW_TRANSFER_TO_SELF);
        String[] editionValues = req.getParameterValues(TransferCommons.PARAM_VERSION_EDITION);
        String[] majorValues = req.getParameterValues(TransferCommons.PARAM_VERSION_MAJOR);
        String[] minorValues = req.getParameterValues(TransferCommons.PARAM_VERSION_MINOR);
        String[] revisionValues = req.getParameterValues(TransferCommons.PARAM_VERSION_REVISION);
        String[] rootFileTransfer = req.getParameterValues(TransferCommons.PARAM_ROOT_FILE_TRANSFER);
        String fromRepositoryId = null;
        if (fromRepositoryIdValues != null && fromRepositoryIdValues.length > 0) {
            fromRepositoryId = fromRepositoryIdValues[0];
        }
        boolean transferToSelf = false;
        if (transferToSelfValues != null && transferToSelfValues.length > 0) {
            if (transferToSelfValues[0].equalsIgnoreCase("true")) {
                transferToSelf = true;
            }
        }
        String edition = "Unknown";
        if (editionValues != null && editionValues.length > 0) {
            edition = editionValues[0];
        }
        String major = "0";
        if (majorValues != null && majorValues.length > 0) {
            major = majorValues[0];
        }
        String minor = "0";
        if (minorValues != null && minorValues.length > 0) {
            minor = minorValues[0];
        }
        String revision = "0";
        if (revisionValues != null && revisionValues.length > 0) {
            revision = revisionValues[0];
        }
        TransferVersion fromVersion = new TransferVersionImpl(major, minor, revision, edition);
        // It replaces the root node if transfers on file system
        if (rootFileTransfer != null && rootFileTransfer.length > 0) {
            receiver.setTransferRootNode(rootFileTransfer[0]);
        }
        // attempt to start the transfer
        transferId = receiver.start(fromRepositoryId, transferToSelf, fromVersion);
        // Create a temporary folder into which we can place transferred files
        receiver.getStagingFolder(transferId);
        TransferVersion version = receiver.getVersion();
        // return the unique transfer id (the lock id)
        StringWriter stringWriter = new StringWriter(1000);
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        jsonWriter.startObject();
        jsonWriter.writeValue(TransferCommons.PARAM_TRANSFER_ID, transferId);
        if (version != null) {
            jsonWriter.writeValue(TransferCommons.PARAM_VERSION_EDITION, version.getEdition());
            jsonWriter.writeValue(TransferCommons.PARAM_VERSION_MAJOR, version.getVersionMajor());
            jsonWriter.writeValue(TransferCommons.PARAM_VERSION_MINOR, version.getVersionMinor());
            jsonWriter.writeValue(TransferCommons.PARAM_VERSION_REVISION, version.getVersionRevision());
        }
        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("transfer started" + transferId);
        return 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);
    }
}
Also used : JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) TransferVersionImpl(org.alfresco.repo.transfer.TransferVersionImpl) TransferException(org.alfresco.service.cmr.transfer.TransferException) StringWriter(java.io.StringWriter) TransferVersion(org.alfresco.service.cmr.transfer.TransferVersion) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Example 3 with TransferException

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

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

the class MessagesTransferCommandProcessor 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;
    try {
        // return the unique transfer id (the lock id)
        StringWriter stringWriter = new StringWriter(300);
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        jsonWriter.startValue("data");
        jsonWriter.startArray();
        jsonWriter.startObject();
        // TODO - clearly a dummy message for now.
        jsonWriter.writeValue("message", "hello world");
        jsonWriter.endObject();
        jsonWriter.startObject();
        // TODO - clearly a dummy message for now.
        jsonWriter.writeValue("message", "message2");
        jsonWriter.endObject();
        jsonWriter.endArray();
        jsonWriter.endValue();
        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) {
        receiver.end(transferRecordId);
        if (ex instanceof TransferException) {
            throw (TransferException) ex;
        }
        throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
    }
}
Also used : JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) TransferException(org.alfresco.service.cmr.transfer.TransferException) StringWriter(java.io.StringWriter) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Example 5 with TransferException

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

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