Search in sources :

Example 36 with ContentReader

use of org.alfresco.service.cmr.repository.ContentReader in project SearchServices by Alfresco.

the class SolrContentStoreTest method exampleUsage.

/**
 * A demonstration of how the store might be used.
 */
@Test
public void exampleUsage() {
    SolrContentStore store = new SolrContentStore(solrHome);
    String tenant = "alfresco.com";
    long dbId = 12345;
    String otherData = "sdfklsfdl";
    ContentContext ctxWrite = SolrContentUrlBuilder.start().add(SolrContentUrlBuilder.KEY_DB_ID, String.valueOf(dbId)).add(SolrContentUrlBuilder.KEY_TENANT, tenant).add("otherData", otherData).getContentContext();
    ContentWriter writer = store.getWriter(ctxWrite);
    writer.putContent("a document in plain text");
    // The URL can be reliably rebuilt in any order
    String urlRead = SolrContentUrlBuilder.start().add("otherData", otherData).add(SolrContentUrlBuilder.KEY_TENANT, tenant).add(SolrContentUrlBuilder.KEY_DB_ID, String.valueOf(dbId)).get();
    ContentReader reader = store.getReader(urlRead);
    String documentText = reader.getContentString();
    Assert.assertEquals("a document in plain text", documentText);
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentContext(org.alfresco.repo.content.ContentContext) Test(org.junit.Test)

Example 37 with ContentReader

use of org.alfresco.service.cmr.repository.ContentReader in project SearchServices by Alfresco.

the class SolrContentStore method retrieveDocFromSolrContentStore.

/**
 * Retrieve document from SolrContentStore.
 * @param tenant identifier
 * @param dbId identifier
 * @return {@link SolrInputDocument} searched document
 * @throws IOException if error
 */
public SolrInputDocument retrieveDocFromSolrContentStore(String tenant, long dbId) throws IOException {
    String contentUrl = SolrContentUrlBuilder.start().add(SolrContentUrlBuilder.KEY_TENANT, tenant).add(SolrContentUrlBuilder.KEY_DB_ID, String.valueOf(dbId)).get();
    ContentReader reader = this.getReader(contentUrl);
    SolrInputDocument cachedDoc = null;
    if (reader.exists()) {
        // try-with-resources statement closes all these InputStreams
        try (InputStream contentInputStream = reader.getContentInputStream();
            // Uncompresses the document
            GZIPInputStream gzip = new GZIPInputStream(contentInputStream)) {
            cachedDoc = (SolrInputDocument) new JavaBinCodec(resolver).unmarshal(gzip);
        } catch (Exception e) {
            // Don't fail for this
            log.warn("Failed to get doc from store using URL: " + contentUrl, e);
            return null;
        }
    }
    return cachedDoc;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) SolrInputDocument(org.apache.solr.common.SolrInputDocument) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) ContentReader(org.alfresco.service.cmr.repository.ContentReader) IOException(java.io.IOException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 38 with ContentReader

use of org.alfresco.service.cmr.repository.ContentReader in project alfresco-remote-api by Alfresco.

the class PeopleImpl method processPersonProperties.

protected void processPersonProperties(String userName, final Map<QName, Serializable> nodeProps) {
    if (!contentUsageService.getEnabled()) {
        // quota used will always be 0 in this case so remove from the person properties
        nodeProps.remove(ContentModel.PROP_SIZE_QUOTA);
        nodeProps.remove(ContentModel.PROP_SIZE_CURRENT);
    }
    // The person description is located in a separate content file located at cm:persondescription
    // "Inline" this data, by removing the cm:persondescription property and adding a temporary property
    // (Person.PROP_PERSON_DESCRIPTION) containing the actual content as a string.
    final ContentData personDescription = (ContentData) nodeProps.get(ContentModel.PROP_PERSONDESC);
    if (personDescription != null) {
        nodeProps.remove(ContentModel.PROP_PERSONDESC);
        AuthenticationUtil.runAsSystem(new RunAsWork<Void>() {

            @Override
            public Void doWork() throws Exception {
                ContentReader reader = contentService.getRawReader(personDescription.getContentUrl());
                if (reader != null && reader.exists()) {
                    String description = reader.getContentString();
                    nodeProps.put(Person.PROP_PERSON_DESCRIPTION, description);
                }
                return null;
            }
        });
    }
}
Also used : ContentData(org.alfresco.service.cmr.repository.ContentData) ContentReader(org.alfresco.service.cmr.repository.ContentReader) PermissionDeniedException(org.alfresco.rest.framework.core.exceptions.PermissionDeniedException) ResetPasswordWorkflowInvalidUserException(org.alfresco.repo.security.authentication.ResetPasswordServiceImpl.ResetPasswordWorkflowInvalidUserException) EntityNotFoundException(org.alfresco.rest.framework.core.exceptions.EntityNotFoundException) ConstraintViolatedException(org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) DisabledServiceException(org.alfresco.rest.framework.core.exceptions.DisabledServiceException) ResetPasswordWorkflowException(org.alfresco.repo.security.authentication.ResetPasswordServiceImpl.ResetPasswordWorkflowException) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException)

Example 39 with ContentReader

use of org.alfresco.service.cmr.repository.ContentReader in project alfresco-remote-api by Alfresco.

the class CommentsImpl method toComment.

private Comment toComment(NodeRef nodeRef, NodeRef commentNodeRef, List<String> include) {
    Map<QName, Serializable> nodeProps = nodeService.getProperties(commentNodeRef);
    ContentReader reader = contentService.getReader(commentNodeRef, ContentModel.PROP_CONTENT);
    if (reader != null) {
        String content = reader.getContentString();
        nodeProps.put(Comment.PROP_COMMENT_CONTENT, content);
        nodeProps.remove(ContentModel.PROP_CONTENT);
    }
    Map<String, Boolean> map = commentService.getCommentPermissions(nodeRef, commentNodeRef);
    boolean canEdit = map.get(CommentService.CAN_EDIT);
    boolean canDelete = map.get(CommentService.CAN_DELETE);
    Person createdBy = people.getPerson((String) nodeProps.get(ContentModel.PROP_CREATOR), include);
    nodeProps.put(Comment.PROP_COMMENT_CREATED_BY, createdBy);
    Person modifiedBy = people.getPerson((String) nodeProps.get(ContentModel.PROP_MODIFIER), include);
    nodeProps.put(Comment.PROP_COMMENT_MODIFIED_BY, modifiedBy);
    Comment comment = new Comment(commentNodeRef.getId(), nodeProps, canEdit, canDelete);
    return comment;
}
Also used : Comment(org.alfresco.rest.api.model.Comment) Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) Person(org.alfresco.rest.api.model.Person)

Example 40 with ContentReader

use of org.alfresco.service.cmr.repository.ContentReader in project alfresco-remote-api by Alfresco.

the class ContentStreamer method streamContent.

/**
 * Streams the content on a given node's content property to the response of the web script.
 *
 * @param req            Request
 * @param res            Response
 * @param nodeRef        The node reference
 * @param propertyQName  The content property name
 * @param attach         Indicates whether the content should be streamed as an attachment or not
 * @param attachFileName Optional file name to use when attach is <code>true</code>
 * @throws IOException
 */
public void streamContent(WebScriptRequest req, WebScriptResponse res, NodeRef nodeRef, QName propertyQName, boolean attach, String attachFileName, Map<String, Object> model) throws IOException {
    if (logger.isDebugEnabled())
        logger.debug("Retrieving content from node ref " + nodeRef.toString() + " (property: " + propertyQName.toString() + ") (attach: " + attach + ")");
    // TODO
    // This was commented out to accomadate records management permissions.  We need to review how we cope with this
    // hard coded permission checked.
    // check that the user has at least READ_CONTENT access - else redirect to the login page
    // if (permissionService.hasPermission(nodeRef, PermissionService.READ_CONTENT) == AccessStatus.DENIED)
    // {
    // throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "Permission denied");
    // }
    // check If-Modified-Since header and set Last-Modified header as appropriate
    Date modified = (Date) nodeService.getProperty(nodeRef, ContentModel.PROP_MODIFIED);
    if (modified != null) {
        long modifiedSince = -1;
        String modifiedSinceStr = req.getHeader("If-Modified-Since");
        if (modifiedSinceStr != null) {
            try {
                modifiedSince = dateFormat.parse(modifiedSinceStr).getTime();
            } catch (Throwable e) {
                if (logger.isInfoEnabled())
                    logger.info("Browser sent badly-formatted If-Modified-Since header: " + modifiedSinceStr);
            }
            if (modifiedSince > 0L) {
                // round the date to the ignore millisecond value which is not supplied by header
                long modDate = (modified.getTime() / 1000L) * 1000L;
                if (modDate <= modifiedSince) {
                    res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
                    return;
                }
            }
        }
    }
    // get the content reader
    ContentReader reader = contentService.getReader(nodeRef, propertyQName);
    if (reader == null || !reader.exists()) {
        throw new WebScriptException(HttpServletResponse.SC_NOT_FOUND, "Unable to locate content for node ref " + nodeRef + " (property: " + propertyQName.toString() + ")");
    }
    // Stream the content
    streamContentImpl(req, res, reader, nodeRef, propertyQName, attach, modified, modified == null ? null : Long.toString(modified.getTime()), attachFileName, model);
}
Also used : WebScriptException(org.springframework.extensions.webscripts.WebScriptException) FileContentReader(org.alfresco.repo.content.filestore.FileContentReader) ContentReader(org.alfresco.service.cmr.repository.ContentReader) Date(java.util.Date)

Aggregations

ContentReader (org.alfresco.service.cmr.repository.ContentReader)53 NodeRef (org.alfresco.service.cmr.repository.NodeRef)25 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)20 Test (org.junit.Test)17 HashMap (java.util.HashMap)16 AlfrescoDocument (org.alfresco.cmis.client.AlfrescoDocument)13 AlfrescoFolder (org.alfresco.cmis.client.AlfrescoFolder)13 FileContentWriter (org.alfresco.repo.content.filestore.FileContentWriter)13 VersionableAspectTest (org.alfresco.repo.version.VersionableAspectTest)13 TestNetwork (org.alfresco.rest.api.tests.RepoService.TestNetwork)13 CmisSession (org.alfresco.rest.api.tests.client.PublicApiClient.CmisSession)13 RequestContext (org.alfresco.rest.api.tests.client.RequestContext)13 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)13 TestPerson (org.alfresco.rest.api.tests.RepoService.TestPerson)12 CmisUpdateConflictException (org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException)12 PublicApiException (org.alfresco.rest.api.tests.client.PublicApiException)11 CmisConstraintException (org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException)11 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)11 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)11 CmisPermissionDeniedException (org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException)11