Search in sources :

Example 71 with Binary

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

the class NavajoStreamCollector method processNavajoEvent.

public Optional<Navajo> processNavajoEvent(NavajoStreamEvent n) throws IOException {
    switch(n.type()) {
        case NAVAJO_STARTED:
            createHeader((NavajoHead) n.body());
            return Optional.empty();
        case MESSAGE_STARTED:
            Message prMessage = null;
            if (!messageStack.isEmpty()) {
                prMessage = messageStack.peek();
            }
            String mode = (String) n.attribute("mode");
            Message msg = NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_SIMPLE);
            msg.setMode(mode);
            if (prMessage == null) {
                assemble.addMessage(msg);
            } else {
                prMessage.addMessage(msg);
            }
            messageStack.push(msg);
            tagStack.push(n.path());
            return Optional.empty();
        case MESSAGE:
            Message msgParent = messageStack.pop();
            tagStack.pop();
            Msg mm = (Msg) n.body();
            List<Prop> msgProps = mm.properties();
            for (Prop e : msgProps) {
                msgParent.addProperty(createTmlProperty(e));
            }
            // pushBinaries
            for (Entry<String, Binary> e : pushBinaries.entrySet()) {
                msgParent.addProperty(createBinaryProperty(e.getKey(), e.getValue()));
            }
            pushBinaries.clear();
            binaryProperties.clear();
            return Optional.empty();
        case ARRAY_STARTED:
            tagStack.push(n.path());
            String path = currentPath();
            AtomicInteger cnt = arrayCounts.get(path);
            if (cnt == null) {
                cnt = new AtomicInteger();
                arrayCounts.put(path, cnt);
            }
            // cnt.incrementAndGet();
            Message parentMessage = null;
            if (!messageStack.isEmpty()) {
                parentMessage = messageStack.peek();
            }
            Message arr = NavajoFactory.getInstance().createMessage(assemble, n.path(), Message.MSG_TYPE_ARRAY);
            if (parentMessage == null) {
                assemble.addMessage(arr);
            } else {
                parentMessage.addMessage(arr);
            }
            messageStack.push(arr);
            return Optional.empty();
        case ARRAY_DONE:
            String apath = currentPath();
            arrayCounts.remove(apath);
            this.messageStack.pop();
            return Optional.empty();
        case ARRAY_ELEMENT_STARTED:
            String arrayElementName = tagStack.peek();
            String arrayPath = currentPath();
            AtomicInteger currentCount = arrayCounts.get(arrayPath);
            if (currentCount != null) {
                String ind = "@" + currentCount.getAndIncrement();
                tagStack.push(ind);
            }
            arrayPath = currentPath();
            Message newElt = NavajoFactory.getInstance().createMessage(assemble, arrayElementName, Message.MSG_TYPE_ARRAY_ELEMENT);
            Message arrParent = messageStack.peek();
            arrParent.addElement(newElt);
            messageStack.push(newElt);
            return Optional.empty();
        case ARRAY_ELEMENT:
            tagStack.pop();
            Message elementParent = messageStack.pop();
            Msg msgElement = (Msg) n.body();
            List<Prop> elementProps = msgElement.properties();
            for (Prop e : elementProps) {
                elementParent.addProperty(createTmlProperty(e));
            }
            for (Entry<String, Binary> e : pushBinaries.entrySet()) {
                elementParent.addProperty(createBinaryProperty(e.getKey(), e.getValue()));
            }
            pushBinaries.clear();
            return Optional.empty();
        case MESSAGE_DEFINITION_STARTED:
            return Optional.empty();
        case MESSAGE_DEFINITION:
            return Optional.empty();
        case NAVAJO_DONE:
            @SuppressWarnings("unchecked") List<Method> methodList = (List<Method>) n.body();
            methodList.stream().map(m -> NavajoFactory.getInstance().createMethod(assemble, m.name, "")).forEach(e -> assemble.addMethod(e));
            return Optional.of(assemble);
        case BINARY_STARTED:
            String name = n.path();
            n.attribute("direction");
            this.currentBinary = new Binary();
            this.currentBinary.startPushRead();
            this.pushBinaries.put(name, currentBinary);
            int length = (Integer) n.attribute("length", () -> -1);
            String description = (String) n.attribute("description", () -> "");
            String direction = (String) n.attribute("direction", () -> "");
            String subtype = (String) n.attribute("subtype", () -> "");
            this.currentProperty = NavajoFactory.getInstance().createProperty(assemble, name, Property.BINARY_PROPERTY, "", length, description, direction);
            this.currentProperty.setSubType(subtype);
            return Optional.empty();
        case BINARY_CONTENT:
            if (this.currentBinary == null) {
            // whoops;
            }
            this.currentBinary.pushContent((String) n.body());
            return Optional.empty();
        case BINARY_DONE:
            this.currentBinary.finishPushContent();
            return Optional.empty();
        default:
            return Optional.empty();
    }
}
Also used : Msg(com.dexels.navajo.document.stream.api.Msg) Logger(org.slf4j.Logger) Method(com.dexels.navajo.document.stream.api.Method) LoggerFactory(org.slf4j.LoggerFactory) Message(com.dexels.navajo.document.Message) IOException(java.io.IOException) HashMap(java.util.HashMap) Stack(java.util.Stack) Select(com.dexels.navajo.document.stream.api.Select) NavajoFactory(com.dexels.navajo.document.NavajoFactory) List(java.util.List) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) Selection(com.dexels.navajo.document.Selection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Msg(com.dexels.navajo.document.stream.api.Msg) Entry(java.util.Map.Entry) Optional(java.util.Optional) Header(com.dexels.navajo.document.Header) Property(com.dexels.navajo.document.Property) NavajoHead(com.dexels.navajo.document.stream.api.NavajoHead) Prop(com.dexels.navajo.document.stream.api.Prop) Binary(com.dexels.navajo.document.types.Binary) Navajo(com.dexels.navajo.document.Navajo) Message(com.dexels.navajo.document.Message) Prop(com.dexels.navajo.document.stream.api.Prop) Method(com.dexels.navajo.document.stream.api.Method) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) Binary(com.dexels.navajo.document.types.Binary)

Example 72 with Binary

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

the class SaxHandler method text.

@Override
public void text(Reader r) throws Exception {
    if (currentProperty == null) {
        return;
    // throw new IllegalArgumentException("Huh?");
    }
    if (Property.BINARY_PROPERTY.equals(currentProperty.getType())) {
        Binary b = new Binary(r);
        String sub = currentProperty.getSubType();
        currentProperty.setValue(b, false);
        // Preserve the subtype. This will cause the handle to refer to the server
        // handle, not the client side
        currentProperty.setSubType(sub);
        // Set mime type if specified as subtype.
        if (currentProperty.getSubType("mime") != null) {
            b.setMimeType(currentProperty.getSubType("mime"));
        }
    // currentProperty.setInitialized();
    } else {
        throw new IllegalArgumentException("uuuh?");
    }
}
Also used : Binary(com.dexels.navajo.document.types.Binary)

Example 73 with Binary

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

the class TmlNavajoTypeSerializer method serialize.

@Override
public void serialize(NavajoType type, JsonGenerator jg, SerializerProvider provider) throws IOException {
    Writer w = null;
    Object o = jg.getOutputTarget();
    if (o instanceof Writer) {
        w = (Writer) o;
    } else if (o instanceof OutputStream) {
        w = new OutputStreamWriter((OutputStream) o);
    } else {
        logger.warn("Unknown outputtarget from JsonGenerator: {}", o);
        return;
    }
    if (type instanceof Binary) {
        // Write a quote as rawvalue to change the mode of JsonGenerator to VALUE
        jg.writeRawValue("\"");
        // Flush JsonGenerator since we will now be talking to the underlying stream itself
        jg.flush();
        // Write the binary to the stream, and end with another quote
        ((Binary) type).writeBase64(w, false);
        jg.writeRaw("\"");
    } else {
        jg.writeString(type.toString());
    }
}
Also used : OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Binary(com.dexels.navajo.document.types.Binary) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 74 with Binary

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

the class JSONTMLImpl method parseProperty.

private void parseProperty(String name, Object value, Message p, JsonParser jp) throws Exception {
    if (name == null) {
        // Give property the name of the message
        name = p.getName();
    }
    Property prop = NavajoFactory.getInstance().createProperty(p.getRootDoc(), name, Property.STRING_PROPERTY, "", 0, "", "out");
    prop.setAnyValue(value);
    p.addProperty(prop);
    if (entityTemplate != null) {
        Property ep = getEntityTemplateProperty(prop);
        if (ep != null) {
            if (ep.getType().equals(Property.SELECTION_PROPERTY) && value != null) {
                Selection s = NavajoFactory.getInstance().createSelection(p.getRootDoc(), value.toString(), value.toString(), true);
                prop.setCardinality(ep.getCardinality());
                prop.setType(ep.getType());
                prop.addSelection(s);
            } else if (ep.getType().equals(Property.BINARY_PROPERTY) && value != null) {
                prop.setValue(new Binary(new StringReader(value.toString())));
                prop.setType(ep.getType());
                prop.setMethod(ep.getMethod());
            } else {
                prop.setType(ep.getType());
                prop.setMethod(ep.getMethod());
            }
        }
    }
}
Also used : Selection(com.dexels.navajo.document.Selection) StringReader(java.io.StringReader) Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property)

Example 75 with Binary

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

the class NQLContext method writeBinary.

private void writeBinary(OutputCallback callback) throws IOException {
    Binary b = (Binary) content;
    setMimeType(b.getMimeType(), callback);
    if (b.getLength() > 0) {
        setContentLength(b.getLength(), callback);
    }
    b.write(callback.getOutputStream());
}
Also used : Binary(com.dexels.navajo.document.types.Binary)

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