Search in sources :

Example 81 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestBinary method testMimeDetection2.

@Test
// Ignore until we find a proper fix for this problem...
@Ignore
public void testMimeDetection2() throws IOException {
    // File-based test - no sandbox mode
    NavajoFactory.getInstance().setSandboxMode(false);
    URL url = this.getClass().getResource("binary1.txt");
    Binary binaryx = new Binary(new File(url.getFile()));
    Assert.assertEquals("text/plain", binaryx.guessContentType());
}
Also used : Binary(com.dexels.navajo.document.types.Binary) File(java.io.File) URL(java.net.URL) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 82 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestBinary method testBinaryIteratorFromLazyURL.

@Test
public void testBinaryIteratorFromLazyURL() throws IOException {
    NavajoFactory.getInstance().setSandboxMode(false);
    URL u = new URL("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png");
    Binary b1 = new Binary(u, false);
    int i = 0;
    int size = 0;
    for (byte[] e : b1.getDataAsIterable(128)) {
        i++;
        size += e.length;
    }
    logger.info(">>> {}", size);
    Assert.assertEquals(5969, size);
    Assert.assertEquals(93, i);
}
Also used : Binary(com.dexels.navajo.document.types.Binary) URL(java.net.URL) Test(org.junit.Test)

Example 83 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestTMLJson method testJsonBinary.

@Test
public void testJsonBinary() throws Exception {
    Navajo n = NavajoFactory.getInstance().createNavajo(getClass().getResourceAsStream("message.xml"));
    JSONTML json = JSONTMLFactory.getInstance();
    Binary binary1 = new Binary(getClass().getResourceAsStream("binary1.txt"));
    Property binProp = NavajoFactory.getInstance().createProperty(n, "Bin", "", "", "");
    binProp.setAnyValue(binary1);
    n.getMessage("SimpleMessage").addProperty(binProp);
    Property testprop = NavajoFactory.getInstance().createProperty(n, "TestProp", "", "", "");
    testprop.setAnyValue("100a");
    n.getMessage("SimpleMessage").addProperty(testprop);
    Writer sw = new StringWriter();
    json.format(n, sw, true);
    String result = sw.toString();
    logger.info(result);
    // Length should be 113, right?
    Assert.assertEquals(113, result.length());
    // Turn back into a Navajo and compare
    Navajo n2 = json.parse(new StringReader(result), "SimpleMessage");
    Assert.assertEquals(n2.getMessage("SimpleMessage").getProperty("Bin").getTypedValue(), "YWJjZGVmZw==");
}
Also used : StringWriter(java.io.StringWriter) JSONTML(com.dexels.navajo.document.json.JSONTML) StringReader(java.io.StringReader) Navajo(com.dexels.navajo.document.Navajo) Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 84 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class Base64Encode method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    Object o = operand(0).value;
    String data = null;
    if (o instanceof Binary) {
        Binary b = (Binary) o;
        data = Base64.getEncoder().encodeToString(b.getData());
    } else if (o instanceof String) {
        data = Base64.getEncoder().encodeToString(((String) o).getBytes(StandardCharsets.UTF_8));
    } else if (o instanceof Property && ((Property) o).getType() == Property.BINARY_PROPERTY) {
        data = ((Property) o).getValue();
    } else {
        throw new TMLExpressionException("Can not Base64Encode null data");
    }
    return data;
}
Also used : Binary(com.dexels.navajo.document.types.Binary) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 85 with Binary

use of com.dexels.navajo.document.types.Binary in project navajo by Dexels.

the class TestBinary method setUp.

@Before
public void setUp() throws IOException {
    NavajoFactory.getInstance().setSandboxMode(true);
    fixture.setUp();
    binary1 = new Binary(getClass().getResourceAsStream("binary1.txt"));
    binary2 = new Binary(getClass().getResourceAsStream("binary2.txt"));
    binary3 = new Binary(getClass().getResourceAsStream("binary3.txt"));
    binary4 = new Binary(getClass().getResourceAsStream("binary4.txt"));
    logger.info("Created first");
    StringWriter sw = new StringWriter();
    binary1.writeBase64(sw);
    StringReader sr = new StringReader(sw.toString());
    binary5 = new Binary(sr);
    logger.info("Last...");
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) Binary(com.dexels.navajo.document.types.Binary) Before(org.junit.Before)

Aggregations

Binary (com.dexels.navajo.document.types.Binary)139 Test (org.junit.Test)38 IOException (java.io.IOException)32 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 File (java.io.File)25 Ignore (org.junit.Ignore)17 Property (com.dexels.navajo.document.Property)16 URL (java.net.URL)16 UserException (com.dexels.navajo.script.api.UserException)14 OutputStream (java.io.OutputStream)13 FileOutputStream (java.io.FileOutputStream)12 Navajo (com.dexels.navajo.document.Navajo)11 MappableException (com.dexels.navajo.script.api.MappableException)11 FileInputStream (java.io.FileInputStream)9 InputStream (java.io.InputStream)9 Message (com.dexels.navajo.document.Message)8 StringWriter (java.io.StringWriter)8 OutputStreamWriter (java.io.OutputStreamWriter)7 NavajoException (com.dexels.navajo.document.NavajoException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6