Search in sources :

Example 1 with TransferVersionImpl

use of org.alfresco.repo.transfer.TransferVersionImpl 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)

Aggregations

StringWriter (java.io.StringWriter)1 TransferVersionImpl (org.alfresco.repo.transfer.TransferVersionImpl)1 TransferException (org.alfresco.service.cmr.transfer.TransferException)1 TransferVersion (org.alfresco.service.cmr.transfer.TransferVersion)1 JSONWriter (org.springframework.extensions.webscripts.json.JSONWriter)1