Search in sources :

Example 6 with StreamSource

use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.

the class BlobTest method testDefaultBinaryMimeType.

/**
 * Tests the default mimeType "application/octet-stream" for binary data.
 * @throws IOException
 */
@Test
public void testDefaultBinaryMimeType() throws IOException {
    Blob blob = createBlob(new ByteArraySource("dummy".getBytes(UTF8)));
    Assert.assertEquals("application/octet-stream", blob.getMimeType());
    Assert.assertTrue(blob.getParameter().isEmpty());
    blob = createBlob(new StreamSource(new ByteArrayInputStream("dummy".getBytes(UTF8))));
    Assert.assertEquals("application/octet-stream", blob.getMimeType());
    Assert.assertTrue(blob.getParameter().isEmpty());
}
Also used : Blob(org.apache.stanbol.enhancer.servicesapi.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Example 7 with StreamSource

use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.

the class BlobTest method testStringWithCustomCharset.

/**
 * This tests that texts with custom charsets are converted to UTF-8.
 * @throws IOException
 */
@Test
public void testStringWithCustomCharset() throws IOException {
    String test = "Exámplê";
    Charset ISO8859_4 = Charset.forName("ISO-8859-4");
    // first via a StringSource
    ContentSource cs = new StringSource(test, ISO8859_4, "text/plain");
    Blob blob = createBlob(cs);
    Assert.assertEquals("text/plain", blob.getMimeType());
    Assert.assertTrue(blob.getParameter().containsKey("charset"));
    Assert.assertEquals(ISO8859_4.name(), blob.getParameter().get("charset"));
    // 2nd via a ByteArray
    byte[] data = test.getBytes(ISO8859_4);
    cs = new ByteArraySource(data, "text/plain; charset=" + ISO8859_4.name());
    blob = createBlob(cs);
    Assert.assertEquals("text/plain", blob.getMimeType());
    Assert.assertTrue(blob.getParameter().containsKey("charset"));
    Assert.assertEquals(ISO8859_4.name(), blob.getParameter().get("charset"));
    // 3rd as Stream
    cs = new StreamSource(new ByteArrayInputStream(data), "text/plain; charset=" + ISO8859_4.name());
    blob = createBlob(cs);
    Assert.assertEquals("text/plain", blob.getMimeType());
    Assert.assertTrue(blob.getParameter().containsKey("charset"));
    Assert.assertEquals(ISO8859_4.name(), blob.getParameter().get("charset"));
    cs = new StreamSource(new ByteArrayInputStream(data), "text/plain; " + ISO8859_4.name());
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) Charset(java.nio.charset.Charset) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Example 8 with StreamSource

use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.

the class ContentSourceTest method checkDataFromStreamSource.

@Test
public void checkDataFromStreamSource() throws IOException {
    ContentSource source = new StreamSource(new ByteArrayInputStream(DATA));
    Assert.assertTrue(Arrays.equals(DATA, source.getData()));
    // multiple calls must work
    source.getData();
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) Test(org.junit.Test)

Example 9 with StreamSource

use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.

the class ContentSourceTest method checkStreamFromStreamSource.

/*
     * Tests checking correct handling of data
     */
@Test
public void checkStreamFromStreamSource() throws IOException {
    ContentSource source = new StreamSource(new ByteArrayInputStream(DATA));
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    IOUtils.copy(source.getStream(), out);
    Assert.assertTrue(Arrays.equals(DATA, out.toByteArray()));
    try {
        source.getStream();
    // multiple calls are supported -> is OK
    } catch (RuntimeException e) {
        // multiple calls are not supported -> illegal state
        Assert.assertTrue(e instanceof IllegalStateException);
    }
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 10 with StreamSource

use of org.apache.stanbol.enhancer.servicesapi.impl.StreamSource in project stanbol by apache.

the class ContentSourceTest method checkFileName.

@Test
public void checkFileName() throws IOException {
    ContentSource source = new StreamSource(new ByteArrayInputStream(DATA), null, null, null);
    assertNull(source.getFileName());
    source = new StreamSource(new ByteArrayInputStream(DATA), null, FILE_NAME, null);
    assertEquals(FILE_NAME, source.getFileName());
    source = new ByteArraySource(DATA, null, FILE_NAME);
    assertEquals(FILE_NAME, source.getFileName());
    source = new ByteArraySource(DATA, null, FILE_NAME, null);
    assertEquals(FILE_NAME, source.getFileName());
}
Also used : ContentSource(org.apache.stanbol.enhancer.servicesapi.ContentSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(org.apache.stanbol.enhancer.servicesapi.impl.StreamSource) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Aggregations

StreamSource (org.apache.stanbol.enhancer.servicesapi.impl.StreamSource)11 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Test (org.junit.Test)7 ContentSource (org.apache.stanbol.enhancer.servicesapi.ContentSource)6 ByteArraySource (org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource)4 Blob (org.apache.stanbol.enhancer.servicesapi.Blob)3 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)3 InputStream (java.io.InputStream)2 MediaType (javax.ws.rs.core.MediaType)2 IRI (org.apache.clerezza.commons.rdf.IRI)2 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)2 FileItemStream (org.apache.commons.fileupload.FileItemStream)2 ContentItemFactory (org.apache.stanbol.enhancer.servicesapi.ContentItemFactory)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 Charset (java.nio.charset.Charset)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1