Search in sources :

Example 1 with ACPImportPackageHandler

use of org.alfresco.repo.importer.ACPImportPackageHandler in project alfresco-repository by Alfresco.

the class ImapFoldersPatch method importInternal.

private void importInternal(String acpName, NodeRef space) throws IOException {
    File acpFile = getFile(acpName);
    ACPImportPackageHandler acpHandler = new ACPImportPackageHandler(acpFile, null);
    Location importLocation = new Location(space);
    importerService.importView(acpHandler, importLocation, null, null);
}
Also used : ACPImportPackageHandler(org.alfresco.repo.importer.ACPImportPackageHandler) File(java.io.File) Location(org.alfresco.service.cmr.view.Location)

Example 2 with ACPImportPackageHandler

use of org.alfresco.repo.importer.ACPImportPackageHandler in project alfresco-repository by Alfresco.

the class ExporterComponentTest method importContent.

/**
 * @param acpFile File
 * @param destRootNode NodeRef
 * @return FileInfo
 */
private FileInfo importContent(File acpFile, NodeRef destRootNode) {
    FileInfo importFolderFileInfo = fileFolderService.create(destRootNode, "Import Folder", ContentModel.TYPE_FOLDER);
    ImportPackageHandler importHandler = new ACPImportPackageHandler(acpFile, ACPImportPackageHandler.DEFAULT_ENCODING);
    importerService.importView(importHandler, new Location(importFolderFileInfo.getNodeRef()), null, null);
    return importFolderFileInfo;
}
Also used : ACPImportPackageHandler(org.alfresco.repo.importer.ACPImportPackageHandler) FileInfo(org.alfresco.service.cmr.model.FileInfo) ACPImportPackageHandler(org.alfresco.repo.importer.ACPImportPackageHandler) ImportPackageHandler(org.alfresco.service.cmr.view.ImportPackageHandler) Location(org.alfresco.service.cmr.view.Location)

Example 3 with ACPImportPackageHandler

use of org.alfresco.repo.importer.ACPImportPackageHandler in project alfresco-repository by Alfresco.

the class ImapMessageTest method importInternal.

private void importInternal(String acpName, NodeRef space) throws IOException {
    // Importing IMAP test acp
    ClassPathResource acpResource = new ClassPathResource(acpName);
    ACPImportPackageHandler acpHandler = new ACPImportPackageHandler(acpResource.getFile(), null);
    Location importLocation = new Location(space);
    importerService.importView(acpHandler, importLocation, null, null);
}
Also used : ACPImportPackageHandler(org.alfresco.repo.importer.ACPImportPackageHandler) ClassPathResource(org.springframework.core.io.ClassPathResource) Location(org.alfresco.service.cmr.view.Location)

Example 4 with ACPImportPackageHandler

use of org.alfresco.repo.importer.ACPImportPackageHandler 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)

Example 5 with ACPImportPackageHandler

use of org.alfresco.repo.importer.ACPImportPackageHandler in project alfresco-repository by Alfresco.

the class ImporterActionExecuter method executeImpl.

/**
 * @see org.alfresco.repo.action.executer.ActionExecuter#execute(Action, NodeRef)
 */
public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
    if (this.nodeService.exists(actionedUponNodeRef) == true) {
        // The node being passed in should be an Alfresco content package
        ContentReader reader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
        if (reader != null) {
            NodeRef importDest = (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
            if (MimetypeMap.MIMETYPE_ACP.equals(reader.getMimetype())) {
                // perform an import of an Alfresco ACP file (special format ZIP structure)
                File zipFile = null;
                try {
                    // unfortunately a ZIP file can not be read directly from an input stream so we have to create
                    // a temporary file first
                    zipFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX_ACP);
                    reader.getContent(zipFile);
                    ACPImportPackageHandler importHandler = new ACPImportPackageHandler(zipFile, (String) ruleAction.getParameterValue(PARAM_ENCODING));
                    this.importerService.importView(importHandler, new Location(importDest), null, null);
                } finally {
                    // now the import is done, delete the temporary file
                    if (zipFile != null) {
                        zipFile.delete();
                    }
                }
            } else if (MimetypeMap.MIMETYPE_ZIP.equals(reader.getMimetype())) {
                // perform an import of a standard ZIP file
                ZipFile zipFile = null;
                File tempFile = null;
                try {
                    tempFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX_ACP);
                    reader.getContent(tempFile);
                    // NOTE: This encoding allows us to workaround bug:
                    // http://bugs.sun.com/bugdatabase/view_bug.do;:WuuT?bug_id=4820807
                    // We also try to use the extra encoding information if present
                    String encoding = (String) ruleAction.getParameterValue(PARAM_ENCODING);
                    if (encoding == null) {
                        encoding = "UTF-8";
                    } else {
                        if (encoding.equalsIgnoreCase("default")) {
                            encoding = null;
                        }
                    }
                    zipFile = new ZipFile(tempFile, encoding, true);
                    // build a temp dir name based on the ID of the noderef we are importing
                    // also use the long life temp folder as large ZIP files can take a while
                    File alfTempDir = TempFileProvider.getLongLifeTempDir("import");
                    File tempDir = new File(alfTempDir.getPath() + File.separatorChar + actionedUponNodeRef.getId());
                    try {
                        // TODO: improve this code to directly pipe the zip stream output into the repo objects -
                        // to remove the need to expand to the filesystem first?
                        extractFile(zipFile, tempDir.getPath());
                        importDirectory(tempDir.getPath(), importDest);
                    } finally {
                        deleteDir(tempDir);
                    }
                } catch (IOException ioErr) {
                    throw new AlfrescoRuntimeException("Failed to import ZIP file.", ioErr);
                } finally {
                    // now the import is done, delete the temporary file
                    if (tempFile != null) {
                        tempFile.delete();
                    }
                    if (zipFile != null) {
                        try {
                            zipFile.close();
                        } catch (IOException e) {
                            throw new AlfrescoRuntimeException("Failed to close zip package.", e);
                        }
                    }
                }
            }
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ACPImportPackageHandler(org.alfresco.repo.importer.ACPImportPackageHandler) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ContentReader(org.alfresco.service.cmr.repository.ContentReader) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) IOException(java.io.IOException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) Location(org.alfresco.service.cmr.view.Location)

Aggregations

ACPImportPackageHandler (org.alfresco.repo.importer.ACPImportPackageHandler)8 Location (org.alfresco.service.cmr.view.Location)8 File (java.io.File)4 ClassPathResource (org.springframework.core.io.ClassPathResource)3 IOException (java.io.IOException)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 ImportPackageHandler (org.alfresco.service.cmr.view.ImportPackageHandler)2 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 FileInfo (org.alfresco.service.cmr.model.FileInfo)1 ContentReader (org.alfresco.service.cmr.repository.ContentReader)1 ImporterBinding (org.alfresco.service.cmr.view.ImporterBinding)1 QName (org.alfresco.service.namespace.QName)1 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)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