Search in sources :

Example 31 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.

the class ADMRemoteStore method resolveNodePath.

/**
 * @param path      cm:name based root relative path
 *                  example: /alfresco/site-data/pages/customise-user-dashboard.xml
 *                           /alfresco/site-data/components
 * @param pattern   optional pattern that is used as part of the match to aquire the surf-config
 *                  folder under the appropriate sites or user location.
 * @param create    if true create the config and folder dirs for the given path returning
 *                  the FileInfo for the last parent in the path, if false only attempt to
 *                  resolve the folder path if it exists returning the last element.
 * @param isFolder  True if the path is for a folder, false if it ends in a filename
 *
 * @return FileInfo representing the file/folder at the specified path location (see create
 *         parameter above) or null if the supplied path does not exist in the store.
 */
private FileInfo resolveNodePath(final String path, final String pattern, final boolean create, final boolean isFolder) {
    if (logger.isDebugEnabled())
        logger.debug("Resolving path: " + path);
    final String adminUserName = AuthenticationUtil.getAdminUserName();
    FileInfo result = null;
    if (path != null) {
        // break down the path into its component elements
        List<String> pathElements = new ArrayList<String>(4);
        final StringTokenizer t = new StringTokenizer(path, "/");
        // the store requires paths of the form /alfresco/site-data/<objecttype>[/<folder>]/<file>.xml
        if (t.countTokens() >= 3) {
            // skip /alfresco
            t.nextToken();
            // skip /site-data
            t.nextToken();
            // collect remaining folder path (and file)
            while (t.hasMoreTokens()) {
                pathElements.add(t.nextToken());
            }
            NodeRef surfConfigRef = aquireSurfConfigRef(path + (pattern != null ? ("/" + pattern) : ""), create);
            try {
                if (surfConfigRef != null) {
                    if (create) {
                        List<String> folders = isFolder ? pathElements : pathElements.subList(0, pathElements.size() - 1);
                        List<FileFolderUtil.PathElementDetails> folderDetails = new ArrayList<>(pathElements.size());
                        Map<QName, Serializable> prop = new HashMap<>(2);
                        prop.put(ContentModel.PROP_IS_INDEXED, false);
                        prop.put(ContentModel.PROP_IS_CONTENT_INDEXED, false);
                        for (String element : folders) {
                            Map<QName, Map<QName, Serializable>> aspects = Collections.singletonMap(ContentModel.ASPECT_INDEX_CONTROL, prop);
                            folderDetails.add(new FileFolderUtil.PathElementDetails(element, aspects));
                        }
                        // ensure folders exist down to the specified parent
                        // ALF-17729 / ALF-17796 - disable auditable on parent folders
                        Set<NodeRef> allCreatedFolders = new LinkedHashSet<>();
                        result = FileFolderUtil.makeFolders(this.fileFolderService, nodeService, surfConfigRef, folderDetails, ContentModel.TYPE_FOLDER, behaviourFilter, new HashSet<QName>(Arrays.asList(new QName[] { ContentModel.ASPECT_AUDITABLE })), allCreatedFolders);
                        // MNT-16371: Revoke ownership privileges for surf-config folder, to tighten access for former SiteManagers.
                        for (NodeRef nodeRef : allCreatedFolders) {
                            ownableService.setOwner(nodeRef, adminUserName);
                        }
                    } else {
                        // perform the cm:name path lookup against our config root node
                        result = this.fileFolderService.resolveNamePath(surfConfigRef, pathElements);
                    }
                }
            } catch (FileNotFoundException fnfErr) {
            // this is a valid condition - we return null to indicate failed lookup
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ArrayList(java.util.ArrayList) FileNotFoundException(org.alfresco.service.cmr.model.FileNotFoundException) FileFolderUtil(org.alfresco.service.cmr.model.FileFolderUtil) NodeRef(org.alfresco.service.cmr.repository.NodeRef) StringTokenizer(java.util.StringTokenizer) FileInfo(org.alfresco.service.cmr.model.FileInfo) Map(java.util.Map) MimetypeMap(org.alfresco.repo.content.MimetypeMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 32 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project alfresco-remote-api by Alfresco.

the class ADMRemoteStore method outputFileNodes.

/**
 * Output the matching file paths a node contains based on a pattern search.
 *
 * @param out Writer for output - relative paths separated by newline characters
 * @param surfConfigRef Surf-Config folder
 * @param fileInfo The FileInfo node to use as the parent
 * @param pattern Optional pattern to match filenames against ("*" is match all)
 * @param recurse True to recurse sub-directories
 * @throws IOException
 */
private void outputFileNodes(Writer out, FileInfo fileInfo, NodeRef surfConfigRef, String pattern, boolean recurse) throws IOException {
    if (surfConfigRef != null) {
        final boolean debug = logger.isDebugEnabled();
        PagingResults<FileInfo> files = getFileNodes(fileInfo, pattern, recurse);
        final Map<NodeRef, String> nameCache = new HashMap<NodeRef, String>();
        for (final FileInfo file : files.getPage()) {
            // walking up the parent tree manually until the "surf-config" parent is hit
            // and manually appending the rest of the cm:name path down to the node.
            final StringBuilder displayPath = new StringBuilder(64);
            NodeRef ref = unprotNodeService.getPrimaryParent(file.getNodeRef()).getParentRef();
            while (!ref.equals(surfConfigRef)) {
                String name = nameCache.get(ref);
                if (name == null) {
                    name = (String) unprotNodeService.getProperty(ref, ContentModel.PROP_NAME);
                    nameCache.put(ref, name);
                }
                displayPath.insert(0, '/');
                displayPath.insert(0, name);
                ref = unprotNodeService.getPrimaryParent(ref).getParentRef();
            }
            out.write("/alfresco/site-data/");
            out.write(URLDecoder.decode(displayPath.toString()));
            out.write(URLDecoder.decode(file.getName()));
            out.write('\n');
            if (debug)
                logger.debug("   /alfresco/site-data/" + displayPath.toString() + file.getName());
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) HashMap(java.util.HashMap)

Example 33 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project acs-community-packaging by Alfresco.

the class BrowseBean method queryBrowseNodes.

// ------------------------------------------------------------------------------
// Helper methods
/**
 * Query a list of nodes for the specified parent node Id
 *
 * @param parentNodeId     Id of the parent node or null for the root node
 */
private void queryBrowseNodes(String parentNodeId) {
    long startTime = 0;
    if (logger.isDebugEnabled())
        startTime = System.currentTimeMillis();
    UserTransaction tx = null;
    try {
        FacesContext context = FacesContext.getCurrentInstance();
        tx = Repository.getUserTransaction(context, true);
        tx.begin();
        NodeRef parentRef;
        if (parentNodeId == null) {
            // no specific parent node specified - use the root node
            parentRef = this.getNodeService().getRootNode(Repository.getStoreRef());
        } else {
            // build a NodeRef for the specified Id and our store
            parentRef = new NodeRef(Repository.getStoreRef(), parentNodeId);
        }
        List<FileInfo> children = null;
        FileFilterMode.setClient(Client.webclient);
        try {
            children = this.getFileFolderService().list(parentRef);
        } finally {
            FileFilterMode.clearClient();
        }
        this.containerNodes = new ArrayList<Node>(children.size());
        this.contentNodes = new ArrayList<Node>(children.size());
        // in case of dynamic config, only lookup once
        Set<NodeEventListener> nodeEventListeners = getNodeEventListeners();
        for (FileInfo fileInfo : children) {
            // create our Node representation from the NodeRef
            NodeRef nodeRef = fileInfo.getNodeRef();
            // find it's type so we can see if it's a node we are interested in
            QName type = this.getNodeService().getType(nodeRef);
            // make sure the type is defined in the data dictionary
            TypeDefinition typeDef = this.getDictionaryService().getType(type);
            if (typeDef != null) {
                MapNode node = null;
                // look for File content node
                if (this.getDictionaryService().isSubClass(type, ContentModel.TYPE_CONTENT)) {
                    // create our Node representation
                    node = new MapNode(nodeRef, this.getNodeService(), fileInfo.getProperties());
                    setupCommonBindingProperties(node);
                    this.contentNodes.add(node);
                } else // look for Space folder node
                if (this.getDictionaryService().isSubClass(type, ContentModel.TYPE_FOLDER) == true && this.getDictionaryService().isSubClass(type, ContentModel.TYPE_SYSTEM_FOLDER) == false) {
                    // create our Node representation
                    node = new MapNode(nodeRef, this.getNodeService(), fileInfo.getProperties());
                    node.addPropertyResolver("icon", this.resolverSpaceIcon);
                    node.addPropertyResolver("smallIcon", this.resolverSmallIcon);
                    this.containerNodes.add(node);
                } else // look for File Link object node
                if (ApplicationModel.TYPE_FILELINK.equals(type)) {
                    // create our File Link Node representation
                    node = new MapNode(nodeRef, this.getNodeService(), fileInfo.getProperties());
                    // only display the user has the permissions to navigate to the target of the link
                    NodeRef destRef = (NodeRef) node.getProperties().get(ContentModel.PROP_LINK_DESTINATION);
                    if (destRef != null && new Node(destRef).hasPermission(PermissionService.READ) == true) {
                        node.addPropertyResolver("url", this.resolverLinkUrl);
                        node.addPropertyResolver("downloadUrl", this.resolverLinkDownload);
                        node.addPropertyResolver("webdavUrl", this.resolverLinkWebdavUrl);
                        node.addPropertyResolver("cifsPath", this.resolverLinkCifsPath);
                        node.addPropertyResolver("fileType16", this.resolverFileType16);
                        node.addPropertyResolver("fileType32", this.resolverFileType32);
                        node.addPropertyResolver("lang", this.resolverLang);
                        this.contentNodes.add(node);
                    }
                } else if (ApplicationModel.TYPE_FOLDERLINK.equals(type)) {
                    // create our Folder Link Node representation
                    node = new MapNode(nodeRef, this.getNodeService(), fileInfo.getProperties());
                    // only display the user has the permissions to navigate to the target of the link
                    NodeRef destRef = (NodeRef) node.getProperties().get(ContentModel.PROP_LINK_DESTINATION);
                    if (destRef != null && new Node(destRef).hasPermission(PermissionService.READ) == true) {
                        node.addPropertyResolver("icon", this.resolverSpaceIcon);
                        node.addPropertyResolver("smallIcon", this.resolverSmallIcon);
                        this.containerNodes.add(node);
                    }
                }
                // inform any listeners that a Node wrapper has been created
                if (node != null) {
                    for (NodeEventListener listener : nodeEventListeners) {
                        listener.created(node, type);
                    }
                }
            } else {
                if (logger.isWarnEnabled())
                    logger.warn("Found invalid object in database: id = " + nodeRef + ", type = " + type);
            }
        }
        // commit the transaction
        tx.commit();
    } catch (InvalidNodeRefException refErr) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object[] { refErr.getNodeRef() }), refErr);
        this.containerNodes = Collections.<Node>emptyList();
        this.contentNodes = Collections.<Node>emptyList();
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
    } catch (Throwable err) {
        Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err);
        this.containerNodes = Collections.<Node>emptyList();
        this.contentNodes = Collections.<Node>emptyList();
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception tex) {
        }
    }
    if (logger.isDebugEnabled()) {
        long endTime = System.currentTimeMillis();
        logger.debug("Time to query and build map nodes: " + (endTime - startTime) + "ms");
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) FacesContext(javax.faces.context.FacesContext) QName(org.alfresco.service.namespace.QName) Node(org.alfresco.web.bean.repository.Node) MapNode(org.alfresco.web.bean.repository.MapNode) MapNode(org.alfresco.web.bean.repository.MapNode) SearcherException(org.alfresco.repo.search.SearcherException) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException) AbortProcessingException(javax.faces.event.AbortProcessingException) IOException(java.io.IOException) TypeDefinition(org.alfresco.service.cmr.dictionary.TypeDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) InvalidNodeRefException(org.alfresco.service.cmr.repository.InvalidNodeRefException)

Example 34 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project acs-community-packaging by Alfresco.

the class FileUploadBean method uploadFile.

/**
 * Ajax method to upload file content. A multi-part form is required as the input.
 *
 * "return-page" = javascript to execute on return from the upload request
 * "currentPath" = the cm:name based server path to upload the content into
 * and the file item content.
 *
 * @throws Exception
 */
@InvokeCommand.ResponseMimetype(value = MimetypeMap.MIMETYPE_HTML)
public void uploadFile() throws Exception {
    FacesContext fc = FacesContext.getCurrentInstance();
    ExternalContext externalContext = fc.getExternalContext();
    HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
    ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
    upload.setHeaderEncoding("UTF-8");
    List<FileItem> fileItems = upload.parseRequest(request);
    FileUploadBean bean = new FileUploadBean();
    String currentPath = null;
    String filename = null;
    String returnPage = null;
    File file = null;
    for (FileItem item : fileItems) {
        if (item.isFormField() && item.getFieldName().equals("return-page")) {
            returnPage = item.getString();
        } else if (item.isFormField() && item.getFieldName().equals("currentPath")) {
            currentPath = URLDecoder.decode(item.getString());
        } else {
            filename = FilenameUtils.getName(item.getName());
            file = TempFileProvider.createTempFile("alfresco", ".upload");
            item.write(file);
        }
    }
    if (logger.isDebugEnabled())
        logger.debug("Ajax file upload request: " + filename + " to path: " + currentPath + " return page: " + returnPage);
    try {
        if (file != null && currentPath != null && currentPath.length() != 0) {
            NodeRef containerRef = pathToNodeRef(fc, currentPath);
            if (containerRef != null) {
                // Guess the mimetype
                String mimetype = Repository.getMimeTypeForFileName(fc, filename);
                // Now guess the encoding
                String encoding = "UTF-8";
                InputStream is = null;
                try {
                    is = new BufferedInputStream(new FileInputStream(file));
                    encoding = Repository.guessEncoding(fc, is, mimetype);
                } catch (Throwable e) {
                    // Bad as it is, it's not terminal
                    logger.error("Failed to guess character encoding of file: " + file, e);
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (Throwable e) {
                        }
                    }
                }
                // Try and extract metadata from the file
                ContentReader cr = new FileContentReader(file);
                cr.setMimetype(mimetype);
                // create properties for content type
                String author = null;
                String title = null;
                String description = null;
                Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>(5, 1.0f);
                if (Repository.extractMetadata(fc, cr, contentProps)) {
                    author = (String) (contentProps.get(ContentModel.PROP_AUTHOR));
                    title = DefaultTypeConverter.INSTANCE.convert(String.class, contentProps.get(ContentModel.PROP_TITLE));
                    description = DefaultTypeConverter.INSTANCE.convert(String.class, contentProps.get(ContentModel.PROP_DESCRIPTION));
                }
                // default the title to the file name if not set
                if (title == null) {
                    title = filename;
                }
                ServiceRegistry services = Repository.getServiceRegistry(fc);
                FileInfo fileInfo = services.getFileFolderService().create(containerRef, filename, ContentModel.TYPE_CONTENT);
                NodeRef fileNodeRef = fileInfo.getNodeRef();
                // set the author aspect
                if (author != null) {
                    Map<QName, Serializable> authorProps = new HashMap<QName, Serializable>(1, 1.0f);
                    authorProps.put(ContentModel.PROP_AUTHOR, author);
                    services.getNodeService().addAspect(fileNodeRef, ContentModel.ASPECT_AUTHOR, authorProps);
                }
                // apply the titled aspect - title and description
                Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>(2, 1.0f);
                titledProps.put(ContentModel.PROP_TITLE, title);
                titledProps.put(ContentModel.PROP_DESCRIPTION, description);
                services.getNodeService().addAspect(fileNodeRef, ContentModel.ASPECT_TITLED, titledProps);
                // get a writer for the content and put the file
                ContentWriter writer = services.getContentService().getWriter(fileNodeRef, ContentModel.PROP_CONTENT, true);
                writer.setMimetype(mimetype);
                writer.setEncoding(encoding);
                writer.putContent(file);
            }
        }
    } catch (Exception e) {
        returnPage = returnPage.replace("${UPLOAD_ERROR}", e.getMessage());
    } finally {
        if (file != null) {
            logger.debug("delete temporary file:" + file.getPath());
            // Delete the temporary file
            file.delete();
        }
    }
    Document result = XMLUtil.newDocument();
    Element htmlEl = result.createElement("html");
    result.appendChild(htmlEl);
    Element bodyEl = result.createElement("body");
    htmlEl.appendChild(bodyEl);
    Element scriptEl = result.createElement("script");
    bodyEl.appendChild(scriptEl);
    scriptEl.setAttribute("type", "text/javascript");
    Node scriptText = result.createTextNode(returnPage);
    scriptEl.appendChild(scriptText);
    if (logger.isDebugEnabled()) {
        logger.debug("File upload request complete.");
    }
    ResponseWriter out = fc.getResponseWriter();
    XMLUtil.print(result, out);
}
Also used : FacesContext(javax.faces.context.FacesContext) Serializable(java.io.Serializable) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) HttpServletRequest(javax.servlet.http.HttpServletRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FileContentReader(org.alfresco.repo.content.filestore.FileContentReader) FileInfo(org.alfresco.service.cmr.model.FileInfo) BufferedInputStream(java.io.BufferedInputStream) ExternalContext(javax.faces.context.ExternalContext) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) QName(org.alfresco.service.namespace.QName) FileContentReader(org.alfresco.repo.content.filestore.FileContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) FileInputStream(java.io.FileInputStream) FileItem(org.apache.commons.fileupload.FileItem) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ResponseWriter(javax.faces.context.ResponseWriter) ServiceRegistry(org.alfresco.service.ServiceRegistry) File(java.io.File)

Example 35 with FileInfo

use of org.alfresco.service.cmr.model.FileInfo in project acs-community-packaging by Alfresco.

the class MySpacesBean method createSpace.

@InvokeCommand.ResponseMimetype(value = MimetypeMap.MIMETYPE_HTML)
public void createSpace() throws Exception {
    FacesContext fc = FacesContext.getCurrentInstance();
    ResponseWriter out = fc.getResponseWriter();
    Map<String, String> requestMap = fc.getExternalContext().getRequestParameterMap();
    String path = (String) requestMap.get("path");
    String name = (String) requestMap.get("name");
    String title = (String) requestMap.get("title");
    String description = (String) requestMap.get("description");
    if (logger.isDebugEnabled())
        logger.debug("MySpacesBean.createSpace() path=" + path + " name=" + name + " title=" + title + " description=" + description);
    try {
        if (path != null && name != null) {
            NodeRef containerRef = FileUploadBean.pathToNodeRef(fc, path);
            if (containerRef != null) {
                NodeService nodeService = Repository.getServiceRegistry(fc).getNodeService();
                FileFolderService ffService = Repository.getServiceRegistry(fc).getFileFolderService();
                FileInfo folderInfo = ffService.create(containerRef, name, ContentModel.TYPE_FOLDER);
                if (logger.isDebugEnabled())
                    logger.debug("Created new folder: " + folderInfo.getNodeRef().toString());
                // apply the uifacets aspect - icon, title and description properties
                Map<QName, Serializable> uiFacetsProps = new HashMap<QName, Serializable>(4, 1.0f);
                uiFacetsProps.put(ApplicationModel.PROP_ICON, CreateSpaceWizard.DEFAULT_SPACE_ICON_NAME);
                uiFacetsProps.put(ContentModel.PROP_TITLE, title);
                uiFacetsProps.put(ContentModel.PROP_DESCRIPTION, description);
                nodeService.addAspect(folderInfo.getNodeRef(), ApplicationModel.ASPECT_UIFACETS, uiFacetsProps);
                out.write("OK: " + folderInfo.getNodeRef().toString());
            }
        }
    } catch (FileExistsException ferr) {
        out.write("ERROR: A file with that name already exists.");
    } catch (Throwable err) {
        out.write("ERROR: " + err.getMessage());
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) NodeService(org.alfresco.service.cmr.repository.NodeService) FileFolderService(org.alfresco.service.cmr.model.FileFolderService) NodeRef(org.alfresco.service.cmr.repository.NodeRef) FileInfo(org.alfresco.service.cmr.model.FileInfo) ResponseWriter(javax.faces.context.ResponseWriter) FileExistsException(org.alfresco.service.cmr.model.FileExistsException)

Aggregations

FileInfo (org.alfresco.service.cmr.model.FileInfo)101 NodeRef (org.alfresco.service.cmr.repository.NodeRef)82 WebApiDescription (org.alfresco.rest.framework.WebApiDescription)34 FileNotFoundException (org.alfresco.service.cmr.model.FileNotFoundException)26 HashMap (java.util.HashMap)20 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)20 QName (org.alfresco.service.namespace.QName)20 AbstractList (java.util.AbstractList)13 UserInfo (org.alfresco.rest.api.model.UserInfo)13 WebApiParam (org.alfresco.rest.framework.WebApiParam)11 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 AccessDeniedException (org.alfresco.repo.security.permissions.AccessDeniedException)10 FileFolderService (org.alfresco.service.cmr.model.FileFolderService)9 IOException (java.io.IOException)8 EntityNotFoundException (org.alfresco.rest.framework.core.exceptions.EntityNotFoundException)8 Serializable (java.io.Serializable)7 List (java.util.List)7 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)7 FacesContext (javax.faces.context.FacesContext)6