Search in sources :

Example 1 with ContentContext

use of org.alfresco.repo.content.ContentContext in project SearchServices by Alfresco.

the class SolrContentStore method storeDocOnSolrContentStore.

/**
 * Stores a {@link SolrInputDocument} into Alfresco solr content store.
 * @param tenant
 * @param dbId
 * @param doc
 * @throws IOException
 */
public void storeDocOnSolrContentStore(String tenant, long dbId, SolrInputDocument doc) throws IOException {
    ContentContext contentContext = SolrContentUrlBuilder.start().add(SolrContentUrlBuilder.KEY_TENANT, tenant).add(SolrContentUrlBuilder.KEY_DB_ID, String.valueOf(dbId)).getContentContext();
    this.delete(contentContext.getContentUrl());
    ContentWriter writer = this.getWriter(contentContext);
    if (log.isDebugEnabled()) {
        log.debug("Writing doc to " + contentContext.getContentUrl());
    }
    try (OutputStream contentOutputStream = writer.getContentOutputStream();
        // Compresses the document
        GZIPOutputStream gzip = new GZIPOutputStream(contentOutputStream)) {
        JavaBinCodec codec = new JavaBinCodec(resolver);
        codec.marshal(doc, gzip);
    } catch (Exception e) {
        // A failure to write to the store is acceptable as long as it's logged
        log.warn("Failed to write to store using URL: " + contentContext.getContentUrl(), e);
    }
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ContentContext(org.alfresco.repo.content.ContentContext) IOException(java.io.IOException) ContentIOException(org.alfresco.service.cmr.repository.ContentIOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec)

Example 2 with ContentContext

use of org.alfresco.repo.content.ContentContext in project SearchServices by Alfresco.

the class SolrContentStoreTest method contentByString.

@Test
public void contentByString() {
    SolrContentStore store = new SolrContentStore(solrHome);
    ContentContext ctx = createContentContext("abc");
    ContentWriter writer = store.getWriter(ctx);
    File file = new File(store.getRootLocation() + "/" + writer.getContentUrl().replace("solr://", ""));
    Assert.assertFalse("File was created before anything was written", file.exists());
    String content = "Quick brown fox jumps over the lazy dog.";
    writer.putContent(content);
    Assert.assertTrue("File was not created.", file.exists());
    try {
        writer.putContent("Should not work");
    } catch (IllegalStateException e) {
    // Expected
    }
    // Now get the reader
    ContentReader reader = store.getReader(ctx.getContentUrl());
    Assert.assertNotNull(reader);
    Assert.assertTrue(reader.exists());
    Assert.assertEquals(content, reader.getContentString());
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentReader(org.alfresco.service.cmr.repository.ContentReader) ContentContext(org.alfresco.repo.content.ContentContext) File(java.io.File) Test(org.junit.Test)

Example 3 with ContentContext

use of org.alfresco.repo.content.ContentContext in project SearchServices by Alfresco.

the class SolrContentStoreTest method getWriter.

@Test
public void getWriter() {
    SolrContentStore store = new SolrContentStore(solrHome);
    ContentContext ctx = createContentContext("abc");
    ContentWriter writer = store.getWriter(ctx);
    String url = writer.getContentUrl();
    Assert.assertNotNull(url);
    Assert.assertEquals("URL of the context does not match the writer URL. ", ctx.getContentUrl(), url);
}
Also used : ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentContext(org.alfresco.repo.content.ContentContext) Test(org.junit.Test)

Example 4 with ContentContext

use of org.alfresco.repo.content.ContentContext 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 5 with ContentContext

use of org.alfresco.repo.content.ContentContext 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

ContentContext (org.alfresco.repo.content.ContentContext)6 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)6 Test (org.junit.Test)5 ContentReader (org.alfresco.service.cmr.repository.ContentReader)4 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 ContentIOException (org.alfresco.service.cmr.repository.ContentIOException)1 JavaBinCodec (org.apache.solr.common.util.JavaBinCodec)1