Search in sources :

Example 31 with ContentReader

use of org.alfresco.service.cmr.repository.ContentReader in project acs-community-packaging by Alfresco.

the class DownloadRawContentServlet method processRequest.

/**
 * Processes the request using the current context i.e. no
 * authentication checks are made, it is presumed they have already
 * been done.
 *
 * @param req The HTTP request
 * @param res The HTTP response
 */
private void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String uri = req.getRequestURI();
    String contentUrl = req.getParameter(ARG_CONTENT_URL);
    if (contentUrl == null || contentUrl.length() == 0) {
        throw new IllegalArgumentException("Download URL did not contain parameter '" + ARG_CONTENT_URL + "':" + uri);
    }
    String infoOnlyStr = req.getParameter(ARG_INFO_ONLY);
    boolean infoOnly = (infoOnlyStr == null) ? false : Boolean.parseBoolean(infoOnlyStr);
    ServiceRegistry serviceRegistry = getServiceRegistry(getServletContext());
    ContentService contentService = serviceRegistry.getContentService();
    // Attempt to get the reader
    ContentReader reader = null;
    try {
        reader = contentService.getRawReader(contentUrl);
        // If the content doesn't exist, generate an error
        if (!reader.exists()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Returning 204 Not Found error...");
            }
            res.sendError(HttpServletResponse.SC_NO_CONTENT);
            return;
        }
    } catch (AccessDeniedException e) {
        if (logger.isDebugEnabled()) {
            logger.debug("Returning 403 Forbidden error after exception: ", e);
        }
        res.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    long readerSize = reader.getSize();
    Date readerLastModified = new Date(reader.getLastModified());
    String readerMimetype = reader.getMimetype();
    String readerEncoding = reader.getEncoding();
    Locale readerLocale = reader.getLocale();
    // Set the content info
    res.setHeader("alfresco.dr.size", DefaultTypeConverter.INSTANCE.convert(String.class, readerSize));
    res.setHeader("alfresco.dr.lastModified", DefaultTypeConverter.INSTANCE.convert(String.class, readerLastModified));
    res.setHeader("alfresco.dr.mimetype", readerMimetype);
    res.setHeader("alfresco.dr.encoding", readerEncoding);
    res.setHeader("alfresco.dr.locale", DefaultTypeConverter.INSTANCE.convert(String.class, readerLocale));
    // Pass the stream to the response, unless only the content info was requested
    if (infoOnly) {
        // Fill response details
        res.setContentType(DEFAULT_MIMETYPE);
        res.setCharacterEncoding(DEFAULT_ENCODING);
    } else {
        // Fill response details
        res.setContentType(readerMimetype);
        res.setCharacterEncoding(readerEncoding);
        try {
            OutputStream clientOs = res.getOutputStream();
            // Streams closed for us
            reader.getContent(clientOs);
        } catch (SocketException e1) {
            // Not a problem
            if (logger.isDebugEnabled()) {
                logger.debug("Client aborted stream read:\n" + "   Content URL: " + contentUrl);
            }
        } catch (ContentIOException e2) {
            // Not a problem
            if (logger.isDebugEnabled()) {
                logger.debug("Client aborted stream read:\n" + "   Content URL: " + contentUrl);
            }
        }
    }
}
Also used : Locale(java.util.Locale) SocketException(java.net.SocketException) AccessDeniedException(org.alfresco.repo.security.permissions.AccessDeniedException) ContentReader(org.alfresco.service.cmr.repository.ContentReader) OutputStream(java.io.OutputStream) ContentService(org.alfresco.service.cmr.repository.ContentService) Date(java.util.Date) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) ServiceRegistry(org.alfresco.service.ServiceRegistry)

Example 32 with ContentReader

use of org.alfresco.service.cmr.repository.ContentReader in project acs-community-packaging by Alfresco.

the class AdvancedSearchDialog method selectSearch.

/**
 * Action handler called when a saved search is selected by the user
 */
public void selectSearch(ActionEvent event) {
    if (NO_SELECTION.equals(properties.getSavedSearch()) == false) {
        // read an XML serialized version of the SearchContext object
        NodeRef searchSearchRef = new NodeRef(Repository.getStoreRef(), properties.getSavedSearch());
        ServiceRegistry services = Repository.getServiceRegistry(FacesContext.getCurrentInstance());
        ContentService cs = services.getContentService();
        try {
            if (services.getNodeService().exists(searchSearchRef)) {
                ContentReader reader = cs.getReader(searchSearchRef, ContentModel.PROP_CONTENT);
                SearchContext search = new SearchContext().fromXML(reader.getContentString());
                // if we get here we read the serialized object successfully
                // now setup the UI to match the new SearchContext object
                initialiseFromContext(search);
            }
        } catch (Throwable err) {
            Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext.getCurrentInstance(), MSG_ERROR_RESTORE_SEARCH), err.getMessage()), err);
        }
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ServiceRegistry(org.alfresco.service.ServiceRegistry) ContentService(org.alfresco.service.cmr.repository.ContentService)

Example 33 with ContentReader

use of org.alfresco.service.cmr.repository.ContentReader in project acs-community-packaging by Alfresco.

the class UsersBeanProperties method getPersonDescription.

public String getPersonDescription() {
    ContentService cs = Repository.getServiceRegistry(FacesContext.getCurrentInstance()).getContentService();
    ContentReader reader = cs.getReader(this.person.getNodeRef(), ContentModel.PROP_PERSONDESC);
    if (reader != null && reader.exists()) {
        return Utils.stripUnsafeHTMLTags(reader.getContentString()).replace("\r\n", "<p>");
    } else {
        return null;
    }
}
Also used : ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentService(org.alfresco.service.cmr.repository.ContentService)

Example 34 with ContentReader

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

the class SolrContentStoreTest method delete.

@Test
public void delete() throws Exception {
    SolrContentStore store = new SolrContentStore(solrHome);
    ContentContext ctx = createContentContext("abc");
    String url = ctx.getContentUrl();
    ContentWriter writer = store.getWriter(ctx);
    writer.putContent("Content goes here.");
    // Check the reader
    ContentReader reader = store.getReader(url);
    Assert.assertNotNull(reader);
    Assert.assertTrue(reader.exists());
    // Delete
    store.delete(url);
    reader = store.getReader(url);
    Assert.assertNotNull(reader);
    Assert.assertFalse(reader.exists());
    // Delete when already gone; should just not fail
    store.delete(url);
}
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 35 with ContentReader

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

the class SolrContentStoreTest method contentByStream.

@Test
public void contentByStream() throws Exception {
    SolrContentStore store = new SolrContentStore(solrHome);
    ContentContext ctx = createContentContext("abc");
    ContentWriter writer = store.getWriter(ctx);
    byte[] bytes = new byte[] { 1, 7, 13 };
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
    writer.putContent(bis);
    // Now get the reader
    ContentReader reader = store.getReader(ctx.getContentUrl());
    ByteArrayOutputStream bos = new ByteArrayOutputStream(3);
    reader.getContent(bos);
    Assert.assertEquals(bytes[0], bos.toByteArray()[0]);
    Assert.assertEquals(bytes[1], bos.toByteArray()[1]);
    Assert.assertEquals(bytes[2], bos.toByteArray()[2]);
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ContentContext(org.alfresco.repo.content.ContentContext) Test(org.junit.Test)

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