Example usage for org.apache.commons.io Charsets UTF_8

List of usage examples for org.apache.commons.io Charsets UTF_8

Introduction

In this page you can find the example usage for org.apache.commons.io Charsets UTF_8.

Prototype

Charset UTF_8

To view the source code for org.apache.commons.io Charsets UTF_8.

Click Source Link

Document

Eight-bit Unicode Transformation Format.

Usage

From source file:org.silverpeas.migration.jcr.service.repository.DocumentRepositoryTest.java

/**
 * Test of deleteDocument method, of class DocumentRepository.
 *//*from   w w w.j a v a 2  s  . co  m*/
@Test
public void testDeleteDocument() throws Exception {
    new JcrDocumentRepositoryTest() {
        @Override
        public void run(final Session session) throws Exception {
            String language = "en";
            ByteArrayInputStream content = new ByteArrayInputStream("This is a test".getBytes(Charsets.UTF_8));
            SimpleAttachment attachment = defaultENContentBuilder().build();
            Date creationDate = attachment.getCreated();
            String foreignId = "node18";
            SimpleDocument document = defaultDocumentBuilder(foreignId, attachment).build();
            SimpleDocumentPK result = getDocumentRepository().createDocument(session, document);
            getDocumentRepository().storeContent(document, content);
            SimpleDocumentPK expResult = new SimpleDocumentPK(result.getId(), instanceId);
            assertThat(result, is(expResult));
            SimpleDocument doc = getDocumentRepository().findDocumentById(session, expResult, language);
            assertThat(doc, is(notNullValue()));
            checkEnglishSimpleDocument(doc);
            assertThat(doc.getOldSilverpeasId(), greaterThan(0L));
            assertThat(doc.getCreated(), is(creationDate));
            getDocumentRepository().deleteDocument(session, expResult);
            doc = getDocumentRepository().findDocumentById(session, expResult, language);
            assertThat(doc, is(nullValue()));
        }
    }.execute();
}

From source file:org.silverpeas.migration.jcr.service.repository.DocumentRepositoryTest.java

/**
 * Test of findDocumentById method, of class DocumentRepository.
 *///w  w  w.  ja v  a2s .c  om
@Test
public void testFindDocumentById() throws Exception {
    new JcrDocumentRepositoryTest() {
        @Override
        public void run(final Session session) throws Exception {
            ByteArrayInputStream content = new ByteArrayInputStream("This is a test".getBytes(Charsets.UTF_8));
            SimpleAttachment attachment = defaultENContentBuilder().build();
            Date creationDate = attachment.getCreated();
            String foreignId = "node18";
            SimpleDocument document = defaultDocumentBuilder(foreignId, attachment).build();
            SimpleDocumentPK result = getDocumentRepository().createDocument(session, document);
            getDocumentRepository().storeContent(document, content);
            SimpleDocumentPK expResult = new SimpleDocumentPK(result.getId(), instanceId);
            assertThat(result, is(expResult));
            SimpleDocument doc = getDocumentRepository().findDocumentById(session, expResult, "en");
            assertThat(doc, is(notNullValue()));
            assertThat(doc.getOldSilverpeasId(), is(not(0L)));
            assertThat(doc.getAttachment(), notNullValue());
            assertThat(doc.getCreated(), is(creationDate));
            checkEnglishSimpleDocument(doc);
            doc = getDocumentRepository().findDocumentById(session, expResult, "fr");
            assertThat(doc, is(notNullValue()));
            assertThat(doc.getOldSilverpeasId(), is(not(0L)));
            assertThat(doc.getAttachment(), nullValue());
        }
    }.execute();
}

From source file:org.silverpeas.migration.jcr.service.repository.DocumentRepositoryTest.java

/**
 * Test of findLast method, of class DocumentRepository.
 *//* w  w  w  .  j  a v  a 2  s  .c om*/
@Test
public void testFindLast() throws Exception {
    new JcrDocumentRepositoryTest() {
        @Override
        public void run(final Session session) throws Exception {
            String foreignId = "node18";
            ByteArrayInputStream content = new ByteArrayInputStream("This is a test".getBytes(Charsets.UTF_8));
            SimpleAttachment attachment = defaultENContentBuilder().build();
            SimpleDocument document = defaultDocumentBuilder(foreignId, attachment).setOrder(10).build();
            SimpleDocumentPK result = getDocumentRepository().createDocument(session, document);
            getDocumentRepository().storeContent(document, content);
            SimpleDocumentPK expResult = new SimpleDocumentPK(result.getId(), instanceId);
            assertThat(result, is(expResult));
            long oldSilverpeasId = document.getOldSilverpeasId();
            content = new ByteArrayInputStream("Ceci est un test".getBytes(Charsets.UTF_8));
            attachment = defaultFRContentBuilder().build();
            foreignId = "node18";
            document = defaultDocumentBuilder(foreignId, attachment).setOrder(5).build();
            result = getDocumentRepository().createDocument(session, document);
            getDocumentRepository().storeContent(document, content);
            expResult = new SimpleDocumentPK(result.getId(), instanceId);
            assertThat(result, is(expResult));
            session.save();
            SimpleDocument doc = getDocumentRepository().findLast(session, instanceId, foreignId);
            assertThat(doc, is(notNullValue()));
            assertThat(doc.getOldSilverpeasId(), is(oldSilverpeasId));
        }
    }.execute();
}

From source file:org.silverpeas.migration.jcr.service.repository.DocumentRepositoryTest.java

/**
 * Test of updateDocument method, of class DocumentRepository.
 *//*from  ww w  . j a  v  a2s .co  m*/
@Test
public void testUpdateDocument() throws Exception {
    new JcrDocumentRepositoryTest() {
        @Override
        public void run(final Session session) throws Exception {
            ByteArrayInputStream content = new ByteArrayInputStream("This is a test".getBytes(Charsets.UTF_8));
            SimpleAttachment attachment = defaultENContentBuilder().build();
            String foreignId = "node18";
            SimpleDocument document = defaultDocumentBuilder(foreignId, attachment).setOrder(10).build();
            SimpleDocumentPK result = getDocumentRepository().createDocument(session, document);
            getDocumentRepository().storeContent(document, content);
            SimpleDocumentPK expResult = new SimpleDocumentPK(result.getId(), instanceId);
            assertThat(result, is(expResult));
            attachment = defaultFRContentBuilder().build();
            document = defaultDocumentBuilder(foreignId, attachment).setPk(result).setOrder(15).build();
            getDocumentRepository().updateDocument(session, document);
            session.save();
            SimpleDocument doc = getDocumentRepository().findDocumentById(session, result, "fr");
            assertThat(doc, is(notNullValue()));
            assertThat(doc.getOrder(), is(15));
            assertThat(doc.getContentType(), is(MimeTypes.PLAIN_TEXT));
            assertThat(doc.getSize(), is(28L));
        }
    }.execute();
}

From source file:org.silverpeas.migration.jcr.service.repository.DocumentRepositoryTest.java

/**
 * Test of addContent method, of class DocumentRepository.
 *//*from www.  j  a v a2 s.  co  m*/
@Test
public void testAddContent() throws Exception {
    new JcrDocumentRepositoryTest() {
        @Override
        public void run(final Session session) throws Exception {
            String foreignId = "node18";
            SimpleDocument document = createAttachmentForTest(defaultDocumentBuilder(foreignId).setOrder(10),
                    defaultENContentBuilder(), "This is a test");
            SimpleDocumentPK expResult = new SimpleDocumentPK(document.getId(), instanceId);
            assertThat(document.getPk(), is(expResult));

            // Vrifying the attachments
            SimpleDocument enDocument = getDocumentById(document.getId(), "en");
            assertThat(enDocument, notNullValue());
            assertThat(enDocument.getAttachment(), notNullValue());
            checkEnglishSimpleDocument(enDocument);
            assertThat(new File(enDocument.getAttachmentPath()).exists(), is(true));
            SimpleDocument frDocument = getDocumentById(document.getId(), "fr");
            assertThat(frDocument, notNullValue());
            assertThat(frDocument.getAttachment(), nullValue());

            ByteArrayInputStream frContent = new ByteArrayInputStream(
                    "Ceci est un test".getBytes(Charsets.UTF_8));
            SimpleAttachment attachment = defaultFRContentBuilder().build();
            getDocumentRepository().addContent(session, document.getPk(), attachment);
            document.setLanguage(attachment.getLanguage());
            getDocumentRepository().storeContent(document, frContent);
            session.save();

            // Vrifying the attachments
            enDocument = getDocumentById(document.getId(), "en");
            assertThat(enDocument, notNullValue());
            assertThat(enDocument.getAttachment(), notNullValue());
            checkEnglishSimpleDocument(enDocument);
            assertThat(new File(enDocument.getAttachmentPath()).exists(), is(true));
            frDocument = getDocumentById(document.getId(), "fr");
            assertThat(frDocument, notNullValue());
            assertThat(frDocument.getAttachment(), notNullValue());
            checkFrenchSimpleDocument(frDocument);
            // This method does not add the content physically (just into JCR)
            assertThat(new File(frDocument.getAttachmentPath()).exists(), is(false));
        }
    }.execute();
}

From source file:org.silverpeas.migration.jcr.service.RepositoryManager.java

private final void initRepository(String repositoryHome, String conf) throws RepositoryException {
    Map<String, String> parameters = new HashMap<String, String>(2);
    parameters.put(RepositoryFactoryImpl.REPOSITORY_HOME, repositoryHome);
    parameters.put(RepositoryFactoryImpl.REPOSITORY_CONF, conf);
    repository = (JackrabbitRepository) JcrUtils.getRepository(parameters);
    Reader reader = new InputStreamReader(
            this.getClass().getClassLoader().getResourceAsStream("silverpeas-jcr.txt"), Charsets.UTF_8);
    try {//from   ww  w .  java  2s  .c o  m
        Session session = getSession();
        CndImporter.registerNodeTypes(reader, session);
        session.save();
        session.logout();
    } catch (InvalidNodeTypeDefinitionException ex) {
        logger.error("Error during JCR repository initalisation", ex);
    } catch (NodeTypeExistsException ex) {
        logger.error("Error during JCR repository initalisation", ex);
    } catch (UnsupportedRepositoryOperationException ex) {
        logger.error("Error during JCR repository initalisation", ex);
    } catch (ParseException ex) {
        logger.error("Error during JCR repository initalisation", ex);
    } catch (IOException ex) {
        logger.error("Error during JCR repository initalisation", ex);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}

From source file:org.silverpeas.migration.jcr.service.SimpleDocumentServiceTest.java

/**
 * Test of getBinaryContent method, of class DocumentRepository.
 */// www . j a v  a  2s.  c  o m
@Test
public void testGetBinaryContent() throws Exception {
    new JcrSimpleDocumentServiceTest() {
        @Override
        public void run() throws Exception {
            String foreignId = "node18";
            SimpleDocument document = createAttachmentForTest(defaultDocumentBuilder(foreignId).setOrder(10),
                    defaultENContentBuilder(), "This is a test");

            ByteArrayOutputStream bibaryContent = new ByteArrayOutputStream();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "en");
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is("This is a test"));
            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "en", 1, 2);
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is("hi"));
            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "en", 1, 50);
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is("his is a test"));
            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "en", -10, 50);
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is("This is a test"));
            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "fr");
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is(""));

            updateAttachmentForTest(document, "fr", "Ceci est un test");

            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "en");
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is("This is a test"));
            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "fr");
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is("Ceci est un test"));

            updateAttachmentForTest(document, "en", "A");
            updateAttachmentForTest(document, "fr", "B");

            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "en");
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is("A"));
            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "en", 1, 4);
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is(""));
            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "fr");
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is("B"));
            bibaryContent.reset();
            getSimpleDocumentService().getBinaryContent(bibaryContent, document.getPk(), "fr", 1, 2);
            assertThat(bibaryContent.toString(Charsets.UTF_8.name()), is(""));
        }
    }.execute();
}

From source file:org.silverpeas.migration.jcr.wysiwyg.purge.WysiwygDocumentPurger.java

/**
 * Gets the content of the given wysiwyg/language.
 * @param commonLogPart//  ww w.  j  a  va 2s. c  o  m
 * @param wysiwygLangAttachment
 * @return a String that represents the content in UTF8, a dummy string if physical file does
 * not exist.
 */
private String getContent(String commonLogPart, SimpleDocument wysiwygLangAttachment) {
    long start = System.currentTimeMillis();
    ByteArrayOutputStream content = new ByteArrayOutputStream();
    try {
        service.getBinaryContent(content, wysiwygLangAttachment.getPk(), wysiwygLangAttachment.getLanguage());
        return content.toString(Charsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
        console.printError(commonLogPart + " - doc=" + wysiwygLangAttachment.getNodeName() + ", lang="
                + wysiwygLangAttachment.getLanguage() + " - unsupported encoding", e);
        return UUID.randomUUID().toString();
    } catch (AttachmentException ae) {
        console.printError(commonLogPart + " - doc=" + wysiwygLangAttachment.getNodeName() + ", lang="
                + wysiwygLangAttachment.getLanguage() + " - inconsistent data, nothing is done", ae);
        return UUID.randomUUID().toString();
    } finally {
        console.printMessage(commonLogPart + " - getContent method - duration of "
                + DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start));
    }
}

From source file:org.silverpeas.test.jcr.JcrTest.java

/**
 * Common method to assert the content for a language of a document.
 * @param uuId the uuId to retrieve the document.
 * @param language the language in which the content must be verified.
 * @param expectedContent if null, the content for the language does not exist.
 * @return the document content loaded for the assertions.
 *//*www .  jav  a2 s .  c o  m*/
public SimpleDocument assertContent(String uuId, String language, String expectedContent) throws Exception {
    SimpleDocument document = getDocumentById(uuId, language);
    assertThat(document, notNullValue());
    final File physicalContent;
    if (document.getAttachment() != null) {
        physicalContent = new File(FileUtil.getAbsolutePath(document.getInstanceId()),
                document.getNodeName() + "/" + document.getMajorVersion() + "_" + document.getMinorVersion()
                        + "/" + language + "/" + document.getFilename());
    } else {
        physicalContent = new File(FileUtil.getAbsolutePath(document.getInstanceId()), document.getNodeName()
                + "/" + document.getMajorVersion() + "_" + document.getMinorVersion() + "/" + language);
    }
    if (expectedContent == null) {
        assertThat(document.getAttachment(), nullValue());
        assertThat(physicalContent.exists(), is(false));
    } else {
        assertThat(document.getAttachment(), notNullValue());
        assertThat(document.getLanguage(), is(language));
        assertThat(physicalContent.exists(), is(true));
        assertThat("It must exist one file only...", physicalContent.getParentFile().listFiles(),
                arrayWithSize(1));
        ByteArrayOutputStream content = new ByteArrayOutputStream();
        getSimpleDocumentService().getBinaryContent(content, document.getPk(), language);
        assertThat(content.toString(Charsets.UTF_8.name()), is(expectedContent));
    }
    return document;
}

From source file:org.silverpeas.test.jcr.JcrTest.java

protected SimpleAttachmentBuilder defaultENContentBuilder() {
    return new SimpleAttachmentBuilder().setFilename("test.pdf").setLanguage("en").setTitle("My test document")
            .setDescription("This is a test document").setSize("This is a test".getBytes(Charsets.UTF_8).length)
            .setContentType(MimeTypes.OCTET_STREAM).setCreatedBy("0").setCreated(randomDate())
            .setXmlFormId("18");

}