Search in sources :

Example 1 with FormField

use of org.springframework.extensions.webscripts.servlet.FormData.FormField 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

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ACPImportPackageHandler (org.alfresco.repo.importer.ACPImportPackageHandler)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 Location (org.alfresco.service.cmr.view.Location)1 WebScriptException (org.springframework.extensions.webscripts.WebScriptException)1 WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)1 WrappingWebScriptRequest (org.springframework.extensions.webscripts.WrappingWebScriptRequest)1 FormField (org.springframework.extensions.webscripts.servlet.FormData.FormField)1 WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)1