Search in sources :

Example 96 with Binary

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

the class MD5Sum method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    String output = "unknown";
    if (getOperand(0) == null) {
        return Integer.valueOf(0);
    }
    if (getOperand(0) instanceof Binary) {
        Binary binaryFile = (Binary) getOperand(0);
        return binaryFile.getHexDigest();
    }
    MessageDigest md5 = null;
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    if (getOperand(0) instanceof Message) {
        Message m = (Message) getOperand(0);
        StringWriter sw = new StringWriter();
        m.write(sw);
        md5.update(sw.toString().getBytes());
    } else {
        md5.update((getOperand(0) + "").getBytes());
    }
    byte[] array = md5.digest();
    if (getOperands().size() > 1 && getOperand(1) instanceof Boolean && (boolean) getOperand(1)) {
        // return hex representation
        return new BinaryDigest(array).hex();
    }
    BigInteger bigInt = new BigInteger(1, array);
    output = bigInt.toString(16);
    return output;
}
Also used : Message(com.dexels.navajo.document.Message) StringWriter(java.io.StringWriter) BinaryDigest(com.dexels.navajo.document.types.BinaryDigest) BigInteger(java.math.BigInteger) Binary(com.dexels.navajo.document.types.Binary) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 97 with Binary

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

the class StoreBinary method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() != 2) {
        throw new TMLExpressionException(this, "Invalid number of operands");
    }
    if (!(getOperand(0) instanceof String)) {
        throw new TMLExpressionException(this, "Invalid operand: " + getOperand(0));
    }
    if (!(getOperand(1) instanceof Binary)) {
        throw new TMLExpressionException(this, "Invalid operand: " + getOperand(1));
    }
    String id = (String) getOperand(0);
    Binary n = (Binary) getOperand(1);
    SharedStoreInterface mss = SharedStoreFactory.getInstance();
    try {
        OutputStream os = mss.getOutputStream(getAccess().getTenant(), GetBinary.PARENT_LOCATION, id, false);
        n.write(os);
        os.close();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new TMLExpressionException(this, e.getMessage());
    }
    return id;
}
Also used : OutputStream(java.io.OutputStream) SharedStoreInterface(com.dexels.navajo.sharedstore.SharedStoreInterface) Binary(com.dexels.navajo.document.types.Binary) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 98 with Binary

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

the class ScaleImageCentered method evaluate.

/*
	 * (non-Javadoc)
	 * @see com.dexels.navajo.parser.FunctionInterface#evaluate()
	 */
@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() < 3) {
        throw new TMLExpressionException(this, "Three operands expected. ");
    }
    Binary b = (Binary) getOperand(0);
    Integer width = (Integer) getOperand(1);
    Integer height = (Integer) getOperand(2);
    String imageType = "png";
    if (getOperands().size() == 4 && (String) getOperand(3) != null) {
        imageType = (String) getOperand(3);
    }
    try {
        Binary res = ImageScaler.scaleCentered(b, width.intValue(), height.intValue(), imageType);
        return res;
    } catch (IOException e) {
        return null;
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 99 with Binary

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

the class ScaleImageFree method evaluate.

/* (non-Javadoc)
	 * @see com.dexels.navajo.parser.FunctionInterface#evaluate()
	 */
@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() < 3) {
        throw new TMLExpressionException(this, "Three operands expected. ");
    }
    Binary b = (Binary) getOperand(0);
    Integer width = (Integer) getOperand(1);
    Integer height = (Integer) getOperand(2);
    String imageType = "png";
    if (getOperands().size() == 4 && (String) getOperand(3) != null) {
        imageType = (String) getOperand(3);
    }
    try {
        Binary res = ImageScaler.scaleFree(b, width.intValue(), height.intValue(), imageType);
        return res;
    } catch (IOException e) {
        logger.error("Error: ", e);
        return null;
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 100 with Binary

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

the class ScaleImageMin method evaluate.

/* (non-Javadoc)
	 * @see com.dexels.navajo.parser.FunctionInterface#evaluate()
	 */
@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() < 3) {
        throw new TMLExpressionException(this, "Three operands expected. ");
    }
    Binary b = (Binary) getOperand(0);
    Integer width = (Integer) getOperand(1);
    Integer height = (Integer) getOperand(2);
    String imageType = "png";
    if (getOperands().size() == 4 && (String) getOperand(3) != null) {
        imageType = (String) getOperand(3);
    }
    try {
        Binary res = ImageScaler.scaleToMin(b, width.intValue(), height.intValue(), imageType);
        return res;
    } catch (IOException e) {
        logger.error("Scaling problem: ", e);
        return null;
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary) IOException(java.io.IOException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

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