Search in sources :

Example 1 with NavajoStreamEvent

use of com.dexels.navajo.document.stream.events.NavajoStreamEvent in project navajo by Dexels.

the class NavajoStreamToMutableMessageStream method apply.

public Subscriber<? super NavajoStreamEvent> apply(Subscriber<? super Flowable<Message>> downStream) throws Exception {
    return new Subscriber<NavajoStreamEvent>() {

        @Override
        public void onError(Throwable t) {
            error = t;
            done = true;
            drain(downStream);
        }

        @Override
        public void onComplete() {
            // feeder.endOfInput();
            queue.offer(navajoEventComplete());
            done = true;
            drain(downStream);
        }

        @Override
        public void onNext(NavajoStreamEvent event) {
            Flowable<Message> msg = processNavajoEvent(event);
            queue.offer(msg);
            drain(downStream);
        }

        @Override
        public void onSubscribe(Subscription s) {
            subscription = s;
            downStream.onSubscribe(new Subscription() {

                @Override
                public void request(long n) {
                    BackpressureHelper.add(requested, n);
                    drain(downStream);
                }

                @Override
                public void cancel() {
                    cancelled = true;
                    s.cancel();
                }
            });
            s.request(1);
        }
    };
}
Also used : Message(com.dexels.navajo.document.Message) Subscriber(org.reactivestreams.Subscriber) Subscription(org.reactivestreams.Subscription) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent)

Example 2 with NavajoStreamEvent

use of com.dexels.navajo.document.stream.events.NavajoStreamEvent in project navajo by Dexels.

the class NavajoDomStreamer method processNavajo.

public static List<NavajoStreamEvent> processNavajo(Navajo navajo) {
    List<NavajoStreamEvent> result = new ArrayList<>();
    Navajo output = NavajoFactory.getInstance().createNavajo();
    List<Message> all = navajo.getAllMessages();
    Header h = navajo.getHeader();
    if (h != null) {
        result.add(header(h));
    } else {
        logger.warn("Unexpected case: Deal with tml without header?");
    }
    for (Message message : all) {
        emitMessage(message, result, output);
    }
    NavajoStreamEvent done = done(navajo.getAllMethods().stream().map(e -> new Method(e.getName())).collect(Collectors.toList()));
    result.add(done);
    return result;
}
Also used : Message(com.dexels.navajo.document.Message) Header(com.dexels.navajo.document.Header) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) Method(com.dexels.navajo.document.stream.api.Method) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent)

Example 3 with NavajoStreamEvent

use of com.dexels.navajo.document.stream.events.NavajoStreamEvent in project navajo by Dexels.

the class ITJettyClient method testJettyCLient.

@Test
public void testJettyCLient() throws Exception {
    JettyClient jc = new JettyClient();
    Flowable<byte[]> in = Flowable.<NavajoStreamEvent>empty().compose(StreamDocument.inNavajo(service, Optional.of(username), Optional.of(password))).lift(StreamDocument.serialize());
    byte[] result = jc.callWithBodyToStream(uri, req -> req.header("X-Navajo-Reactive", "true").header("X-Navajo-Service", service).header("X-Navajo-Username", username).header("X-Navajo-Password", password).header("Accept-Encoding", null).method(HttpMethod.POST), in, "text/xml;charset=utf-8").reduce(new ByteArrayOutputStream(), (stream, b) -> {
        stream.write(b);
        return stream;
    }).map(stream -> stream.toByteArray()).blockingGet();
    logger.info("result: {}", new String(result));
    Assert.assertTrue(result.length > 5000);
    jc.close();
}
Also used : NavajoReactiveJettyClient(com.dexels.navajo.client.stream.jetty.NavajoReactiveJettyClient) JettyClient(com.dexels.navajo.client.stream.jetty.JettyClient) Logger(org.slf4j.Logger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringWriter(java.io.StringWriter) LoggerFactory(org.slf4j.LoggerFactory) NavajoReactiveJettyClient(com.dexels.navajo.client.stream.jetty.NavajoReactiveJettyClient) Test(org.junit.Test) StandardCharsets(java.nio.charset.StandardCharsets) HttpMethod(org.eclipse.jetty.http.HttpMethod) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Optional(java.util.Optional) XML(com.dexels.navajo.document.stream.xml.XML) JettyClient(com.dexels.navajo.client.stream.jetty.JettyClient) StreamDocument(com.dexels.navajo.document.stream.StreamDocument) TestConfig(com.dexels.navajo.runtime.config.TestConfig) Assert(org.junit.Assert) Navajo(com.dexels.navajo.document.Navajo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) Test(org.junit.Test)

Example 4 with NavajoStreamEvent

use of com.dexels.navajo.document.stream.events.NavajoStreamEvent in project navajo by Dexels.

the class NavajoDomStreamer method streamMessage.

public static Flowable<NavajoStreamEvent> streamMessage(Message message) {
    List<NavajoStreamEvent> result = new ArrayList<>();
    Navajo output = NavajoFactory.getInstance().createNavajo();
    emitMessage(message, result, output);
    return Flowable.fromIterable(result);
}
Also used : ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent)

Example 5 with NavajoStreamEvent

use of com.dexels.navajo.document.stream.events.NavajoStreamEvent 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)

Aggregations

NavajoStreamEvent (com.dexels.navajo.document.stream.events.NavajoStreamEvent)9 Navajo (com.dexels.navajo.document.Navajo)5 JettyClient (com.dexels.navajo.client.stream.jetty.JettyClient)4 Optional (java.util.Optional)4 Message (com.dexels.navajo.document.Message)3 StreamDocument (com.dexels.navajo.document.stream.StreamDocument)3 XML (com.dexels.navajo.document.stream.xml.XML)3 Flowable (io.reactivex.Flowable)3 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)2 NavajoReactiveJettyClient (com.dexels.navajo.client.stream.jetty.NavajoReactiveJettyClient)2 Header (com.dexels.navajo.document.Header)2 DataItem (com.dexels.navajo.document.stream.DataItem)2 Method (com.dexels.navajo.document.stream.api.Method)2 Msg (com.dexels.navajo.document.stream.api.Msg)2 StreamScriptContext (com.dexels.navajo.document.stream.api.StreamScriptContext)2 ReactiveParameters (com.dexels.navajo.reactive.api.ReactiveParameters)2 ReactiveResolvedParameters (com.dexels.navajo.reactive.api.ReactiveResolvedParameters)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2