Search in sources :

Example 6 with WrappingWebScriptRequest

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

the class CMISHttpServletRequest method getBaseRequest.

/*
	 * Recursively unwrap req if it is a WrappingWebScriptRequest
	 */
private WebScriptRequest getBaseRequest(WebScriptRequest req) {
    WebScriptRequest ret = req;
    while (ret instanceof WrappingWebScriptRequest) {
        WrappingWebScriptRequest wrapping = (WrappingWebScriptRequest) req;
        ret = wrapping.getNext();
    }
    return ret;
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest)

Example 7 with WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest 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 WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest 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 WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest 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)

Example 10 with WrappingWebScriptRequest

use of org.springframework.extensions.webscripts.WrappingWebScriptRequest in project records-management by Alfresco.

the class ImportPost method executeImpl.

@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache) {
    // 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);
    // get the content type of request and ensure it's multipart/form-data
    String contentType = req.getContentType();
    if (MULTIPART_FORMDATA.equals(contentType) && webScriptServletRequest != null) {
        String nodeRef = req.getParameter(PARAM_DESTINATION);
        if (nodeRef == null || nodeRef.length() == 0) {
            throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'destination' parameter was not provided in form data");
        }
        // create and check noderef
        final NodeRef destination = new NodeRef(nodeRef);
        if (nodeService.exists(destination)) {
            // check the destination is an RM container
            if (!nodeService.hasAspect(destination, RecordsManagementModel.ASPECT_FILE_PLAN_COMPONENT) || !dictionaryService.isSubClass(nodeService.getType(destination), ContentModel.TYPE_FOLDER)) {
                throw new WebScriptException(Status.STATUS_BAD_REQUEST, "NodeRef '" + destination + "' does not represent an Records Management container node.");
            }
        } else {
            status.setCode(HttpServletResponse.SC_NOT_FOUND, "NodeRef '" + destination + "' does not exist.");
        }
        // as there is no 'import capability' and the RM admin user is different from
        // the DM admin user (meaning the webscript 'admin' authentication can't be used)
        // perform a manual check here to ensure the current user has the RM admin role.
        boolean isAdmin = filePlanRoleService.hasRMAdminRole(filePlanService.getFilePlan(destination), AuthenticationUtil.getRunAsUser());
        if (!isAdmin) {
            throw new WebScriptException(Status.STATUS_FORBIDDEN, "Access Denied");
        }
        File acpFile = null;
        try {
            // create a temporary file representing uploaded ACP file
            FormField acpContent = webScriptServletRequest.getFileField(PARAM_ARCHIVE);
            if (acpContent == null) {
                acpContent = webScriptServletRequest.getFileField(PARAM_FILEDATA);
                if (acpContent == null) {
                    throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Mandatory 'archive' file content was not provided in form data");
                }
            }
            acpFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, "." + ACPExportPackageHandler.ACP_EXTENSION);
            // copy contents of uploaded file to temp ACP file
            FileOutputStream fos = new FileOutputStream(acpFile);
            // NOTE: this method closes both streams
            FileCopyUtils.copy(acpContent.getInputStream(), fos);
            if (logger.isDebugEnabled()) {
                logger.debug("Importing uploaded ACP (" + acpFile.getAbsolutePath() + ") into " + nodeRef);
            }
            // setup the import handler
            final ACPImportPackageHandler importHandler = new ACPImportPackageHandler(acpFile, "UTF-8");
            // import the ACP file as the system user
            AuthenticationUtil.runAs(new RunAsWork<NodeRef>() {

                public NodeRef doWork() {
                    importerService.importView(importHandler, new Location(destination), null, null);
                    return null;
                }
            }, AuthenticationUtil.getSystemUserName());
            // create and return model
            Map<String, Object> model = new HashMap<String, Object>(1);
            model.put("success", true);
            return model;
        } catch (FileNotFoundException fnfe) {
            throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Failed to import ACP file", fnfe);
        } catch (IOException ioe) {
            throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR, "Failed to import ACP file", ioe);
        } finally {
            if (acpFile != null) {
                acpFile.delete();
            }
        }
    } else {
        throw new WebScriptException(Status.STATUS_BAD_REQUEST, "Request is not " + MULTIPART_FORMDATA + " encoded");
    }
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) HashMap(java.util.HashMap) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ACPImportPackageHandler(org.alfresco.repo.importer.ACPImportPackageHandler) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) FileOutputStream(java.io.FileOutputStream) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) File(java.io.File) FormField(org.springframework.extensions.webscripts.servlet.FormData.FormField) Location(org.alfresco.service.cmr.view.Location)

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