Search in sources :

Example 11 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class TMLSerializer method parseMessage.

private final Message parseMessage(InputStream is, Message parent, String msgType) throws IOException {
    String name = "-";
    if (!msgType.equals(Message.MSG_TYPE_ARRAY_ELEMENT)) {
        // array message elements do not have a message name.
        byte[] array = new byte[bytesToShort(is)];
        is.read(array, 0, array.length);
        name = new String(array);
    }
    Message m = NavajoFactory.getInstance().createMessage(myNavajo, name, msgType);
    if (parent != null) {
        parent.addMessage(m);
    } else {
        myNavajo.addMessage(m);
    }
    boolean endOfMessage = false;
    int propertyIndex = 0;
    while (!endOfMessage) {
        // 
        byte tagType = (byte) is.read();
        if (tagType == MESSAGE_CODE) {
            // a submessage.
            parseMessage(is, m, Message.MSG_TYPE_SIMPLE);
        } else if (tagType == ARRAY_MESSAGE_CODE) {
            arrayEltIndex = 0;
            // an array message.
            Message am = parseMessage(is, m, Message.MSG_TYPE_ARRAY);
            fixPropertyNames(am);
        } else if (tagType == ARRAY_MESSAGE_ELT_CODE) {
            // an array message element.
            parseMessage(is, m, Message.MSG_TYPE_ARRAY_ELEMENT);
            arrayEltIndex++;
        } else if (tagType == MESSAGE_CLOSE_CODE) {
            propertyIndex = 0;
            endOfMessage = true;
        } else if (tagType == STRING_PROPERTY_CODE || tagType == LONG_STRING_PROPERTY_CODE) {
            Property p = parseProperty(Property.STRING_PROPERTY, m.getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT), propertyIndex, arrayEltIndex, is, (tagType == LONG_STRING_PROPERTY_CODE));
            m.addProperty(p);
            propertyIndex++;
        } else if (tagType == DATE_PROPERTY_CODE) {
            Property p = parseProperty(Property.DATE_PROPERTY, m.getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT), propertyIndex, arrayEltIndex, is, false);
            m.addProperty(p);
            propertyIndex++;
        } else if (tagType == BOOLEAN_PROPERTY_CODE) {
            Property p = parseProperty(Property.BOOLEAN_PROPERTY, m.getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT), propertyIndex, arrayEltIndex, is, false);
            m.addProperty(p);
            propertyIndex++;
        } else if (tagType == INTEGER_PROPERTY_CODE) {
            Property p = parseProperty(Property.INTEGER_PROPERTY, m.getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT), propertyIndex, arrayEltIndex, is, false);
            m.addProperty(p);
            propertyIndex++;
        } else if (tagType == FLOAT_PROPERTY_CODE) {
            Property p = parseProperty(Property.FLOAT_PROPERTY, m.getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT), propertyIndex, arrayEltIndex, is, false);
            m.addProperty(p);
            propertyIndex++;
        } else if (tagType == BINARY_PROPERTY_CODE) {
            Property p = parseProperty(Property.BINARY_PROPERTY, m.getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT), propertyIndex, arrayEltIndex, is, true);
            m.addProperty(p);
            propertyIndex++;
        }
    }
    return m;
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

Example 12 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class TMLSerializer method parseProperty.

private final Property parseProperty(String type, boolean partOfArrayElement, int propertyIndex, int arrayEltIndex, InputStream is, boolean longLength) throws IOException {
    String name = propertyIndex + "";
    byte[] array = null;
    if (arrayEltIndex == 0 || !partOfArrayElement) {
        array = new byte[bytesToShort(is)];
        is.read(array, 0, array.length);
        name = new String(array);
    }
    int size = (longLength ? bytesToInt(is) : bytesToShort(is));
    array = new byte[size];
    is.read(array, 0, array.length);
    if (type.equals(Property.BINARY_PROPERTY)) {
        Property p = NavajoFactory.getInstance().createProperty(myNavajo, name, type, null, 0, "", "");
        p.setAnyValue(new Binary(array));
        return p;
    } else {
        String value = new String(array);
        return NavajoFactory.getInstance().createProperty(myNavajo, name, type, value, 0, "", "");
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property)

Example 13 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class BaseRequestImpl method writeOutput.

@Override
public void writeOutput(Navajo inDoc, Navajo outDoc, long scheduledAt, long startedAt, String threadStatus) throws IOException {
    long finishedScriptAt = System.currentTimeMillis();
    // postTime =
    long postTime = scheduledAt - connectedAt;
    long queueTime = startedAt - scheduledAt;
    long serverTime = finishedScriptAt - startedAt;
    if (dataPath != null) {
        Property data = outDoc.getProperty(dataPath);
        Object dataObject = data.getTypedValue();
        if (dataObject instanceof Binary) {
            Binary b = (Binary) dataObject;
            if (contentType != null) {
                response.setContentType(contentType);
            } else {
                response.setContentType(b.guessContentType());
            }
            if (this.fileName != null) {
                response.setHeader("Content-Disposition", "attachment; filename=" + this.fileName);
            }
            OutputStream out = getOutputStream(acceptEncoding, response.getOutputStream());
            CountingOutputStream cos = new CountingOutputStream(out);
            b.write(cos);
            cos.close();
            logResponseSize(cos.getCount());
            return;
        } else {
            response.setContentType("text/plain; charset=UTF-8");
            OutputStream out = getOutputStream(acceptEncoding, response.getOutputStream());
            OutputStreamWriter osw = new OutputStreamWriter(out);
            osw.write("" + dataObject);
            osw.close();
        }
    }
    OutputStream out = getOutputStream(acceptEncoding, response.getOutputStream());
    response.setContentType("text/xml; charset=UTF-8");
    if (outDoc == null) {
        logger.warn("Null outDoc. This is going to hurt");
        response.sendError(500, "No response received, possible scheduling problem.");
        return;
    } else if (outDoc.getHeader() == null) {
        logger.warn("Null outDoc header. This is going to hurt");
        response.sendError(500, "No response header received, possible scheduling problem.");
        return;
    }
    outDoc.getHeader().setHeaderAttribute("postTime", "" + postTime);
    outDoc.getHeader().setHeaderAttribute("queueTime", "" + queueTime);
    outDoc.getHeader().setHeaderAttribute("serverTime", "" + serverTime);
    outDoc.getHeader().setHeaderAttribute("threadName", "" + Thread.currentThread().getName());
    CountingOutputStream cos = new CountingOutputStream(out);
    outDoc.write(cos);
    cos.close();
    logResponseSize(cos.getCount());
    if (inDoc != null && inDoc.getHeader() != null && outDoc.getHeader() != null) {
        statLogger.info("Finished {} ({}) in {}ms - {}", outDoc.getHeader().getHeaderAttribute("accessId"), inDoc.getHeader().getRPCName(), (System.currentTimeMillis() - connectedAt), threadStatus);
    }
}
Also used : CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) CountingOutputStream(org.apache.commons.io.output.CountingOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property)

Example 14 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class Switch method createTestNavajo.

@Override
protected Navajo createTestNavajo() throws Exception {
    Navajo doc = NavajoFactory.getInstance().createNavajo();
    Message array = NavajoFactory.getInstance().createMessage(doc, "Aap");
    array.setType(Message.MSG_TYPE_ARRAY);
    Message array1 = NavajoFactory.getInstance().createMessage(doc, "Aap");
    array.addElement(array1);
    doc.addMessage(array);
    Property p = NavajoFactory.getInstance().createProperty(doc, "Noot", Property.INTEGER_PROPERTY, "10", 10, "", "in");
    p.setValue(10);
    array1.addProperty(p);
    Message single = NavajoFactory.getInstance().createMessage(doc, "Single");
    doc.addMessage(single);
    Property p2 = NavajoFactory.getInstance().createProperty(doc, "Selectie", "1", "", "in");
    p2.addSelection(NavajoFactory.getInstance().createSelection(doc, "key", "value", true));
    single.addProperty(p2);
    Property p3 = NavajoFactory.getInstance().createProperty(doc, "Vuur", Property.INTEGER_PROPERTY, "10", 10, "", "out");
    p3.setValue(10);
    single.addProperty(p3);
    return doc;
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Example 15 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class SetValueCommand method execute.

@Override
public JsonNode execute(ArticleRuntime runtime, ArticleContext context, Map<String, String> parameters, XMLElement xmlElement) throws APIException {
    String service = parameters.get("service");
    if (service == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". setvalue requires a service, which is not supplied.", null, APIErrorCode.InternalError);
    }
    String element = parameters.get("element");
    if (element == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". setvalue requires a element, which is not supplied.", null, APIErrorCode.InternalError);
    }
    Navajo n = runtime.getNavajo(service);
    if (n == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". Requested service: " + service + " is not loaded.", null, APIErrorCode.InternalError);
    }
    Property p = n.getProperty(element);
    if (p == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". Requested element: " + element + " in service " + service + " is not found.", null, APIErrorCode.InternalError);
    }
    String value = parameters.get("value");
    if (value == null) {
        throw new APIException("Article problem in " + runtime.getArticleName() + ". setvalue requires a value, which is not supplied.", null, APIErrorCode.InternalError);
    }
    if (value.startsWith("@")) {
        String resolved = runtime.resolveArgument(value);
        if (resolved != null) {
            p.setValue(resolved);
        }
    } else if (value.startsWith("$")) {
        p.setAnyValue(runtime.resolveScope(value));
    } else {
        p.setValue(value);
    }
    return null;
}
Also used : APIException(com.dexels.navajo.article.APIException) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Aggregations

Property (com.dexels.navajo.document.Property)253 Message (com.dexels.navajo.document.Message)148 Test (org.junit.Test)88 Navajo (com.dexels.navajo.document.Navajo)84 Selection (com.dexels.navajo.document.Selection)36 NavajoException (com.dexels.navajo.document.NavajoException)30 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)17 ArrayList (java.util.ArrayList)17 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)16 Binary (com.dexels.navajo.document.types.Binary)16 UserException (com.dexels.navajo.script.api.UserException)16 Access (com.dexels.navajo.script.api.Access)15 StringWriter (java.io.StringWriter)13 List (java.util.List)11 Operand (com.dexels.navajo.document.Operand)10 MappableException (com.dexels.navajo.script.api.MappableException)9 IOException (java.io.IOException)9 Writer (java.io.Writer)9 StringTokenizer (java.util.StringTokenizer)9 JSONTML (com.dexels.navajo.document.json.JSONTML)8