Search in sources :

Example 6 with NavajoStreamEvent

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

the class StreamDocument method messageWithPath.

public static FlowableOperator<NavajoStreamEvent, NavajoStreamEvent> messageWithPath(final String messagePath, final Function<Msg, Msg> operation, boolean filterOthers) {
    return new BaseFlowableOperator<NavajoStreamEvent, NavajoStreamEvent>(1) {

        @Override
        public Subscriber<? super NavajoStreamEvent> apply(Subscriber<? super NavajoStreamEvent> child) throws Exception {
            return new Subscriber<NavajoStreamEvent>() {

                private final Stack<String> pathStack = new Stack<>();

                @Override
                public void onComplete() {
                    operatorComplete(child);
                }

                @Override
                public void onError(Throwable e) {
                    operatorError(e, child);
                }

                @Override
                public void onNext(NavajoStreamEvent event) {
                    switch(event.type()) {
                        case MESSAGE_STARTED:
                            pathStack.push(event.path());
                            // operatorRequest(1);
                            break;
                        case ARRAY_ELEMENT_STARTED:
                            // operatorRequest(1);
                            break;
                        case MESSAGE:
                            if (matches(messagePath, pathStack)) {
                                Msg transformed;
                                try {
                                    transformed = operation.apply((Msg) event.body());
                                    operatorNext(event, e -> {
                                        return Events.message(transformed, event.path(), event.attributes());
                                    }, child);
                                } catch (Exception e1) {
                                    logger.error("Unexpected error: ", e1);
                                }
                                return;
                            }
                            pathStack.pop();
                            break;
                        case ARRAY_ELEMENT:
                            if (matches(messagePath, pathStack)) {
                                Msg transformed;
                                try {
                                    transformed = operation.apply((Msg) event.body());
                                    operatorNext(event, e -> {
                                        return Events.arrayElement(transformed, event.attributes());
                                    }, child);
                                } catch (Exception e1) {
                                    logger.error("Very unexpected exception: ", e1);
                                    e1.printStackTrace();
                                }
                                return;
                            }
                            break;
                        // TODO Support these?
                        case ARRAY_STARTED:
                            pathStack.push(event.path());
                            break;
                        case ARRAY_DONE:
                            pathStack.pop();
                            break;
                        default:
                            break;
                    }
                    if (!filterOthers) {
                        operatorNext(event, e -> e, child);
                    } else {
                        operatorRequest(1);
                    }
                }

                private boolean matches(String path, Stack<String> pathStack) {
                    String joined = String.join("/", pathStack);
                    return path.equals(joined);
                }

                @Override
                public void onSubscribe(Subscription s) {
                    operatorSubscribe(s, child);
                }
            };
        }
    };
}
Also used : Msg(com.dexels.navajo.document.stream.api.Msg) FlowableSubscriber(io.reactivex.FlowableSubscriber) Subscriber(org.reactivestreams.Subscriber) BaseFlowableOperator(com.dexels.navajo.document.stream.io.BaseFlowableOperator) Subscription(org.reactivestreams.Subscription) CharacterCodingException(java.nio.charset.CharacterCodingException) IOException(java.io.IOException) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) Stack(java.util.Stack)

Example 7 with NavajoStreamEvent

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

the class ITJettyClient method testNavajoClient.

@Test
public void testNavajoClient() throws Exception {
    JettyClient client = new JettyClient();
    Flowable<NavajoStreamEvent> in = Flowable.<NavajoStreamEvent>empty().compose(StreamDocument.inNavajo(service, Optional.of(username), Optional.of(password)));
    Flowable<byte[]> inStream = in.lift(StreamDocument.serialize()).doOnNext(e -> logger.debug("Sending: {}", new String(e, StandardCharsets.UTF_8)));
    Navajo n = client.callWithBodyToStream(uri, req -> req.header("X-Navajo-Reactive", "true").header("X-Navajo-Service", service).header("X-Navajo-Instance", "").header("X-Navajo-Username", username).header("X-Navajo-Password", password).method(HttpMethod.POST), inStream, "text/xml;charset=utf-8").lift(XML.parseFlowable(5)).concatMap(e -> e).lift(StreamDocument.parse()).concatMap(e -> e).compose(StreamDocument.inNavajo(service, Optional.of(username), Optional.of(password))).toObservable().compose(StreamDocument.domStreamCollector()).blockingFirst();
    StringWriter sw = new StringWriter();
    n.write(sw);
    logger.info("{}", sw);
    client.close();
}
Also used : NavajoReactiveJettyClient(com.dexels.navajo.client.stream.jetty.NavajoReactiveJettyClient) JettyClient(com.dexels.navajo.client.stream.jetty.JettyClient) StringWriter(java.io.StringWriter) Navajo(com.dexels.navajo.document.Navajo) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) Test(org.junit.Test)

Example 8 with NavajoStreamEvent

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

the class CallRemoteSource method execute.

@Override
public Flowable<DataItem> execute(StreamScriptContext context, Optional<ImmutableMessage> current, ImmutableMessage param) {
    ReactiveResolvedParameters resolved = params.resolve(context, current, ImmutableFactory.empty(), metadata);
    String server = resolved.paramString("server");
    String username = resolved.paramString("username");
    String password = resolved.paramString("password");
    final String service = resolved.paramString("service");
    Flowable<NavajoStreamEvent> flow = client.callWithBodyToStream(server, e -> e.header("X-Navajo-Username", username).header("X-Navajo-Password", password).header("X-Navajo-Service", service).method(HttpMethod.POST), Flowable.<NavajoStreamEvent>empty().compose(StreamDocument.inNavajo(service, Optional.of(username), Optional.of(password))).lift(StreamDocument.serialize()), "text/xml").lift(XML.parseFlowable(10)).concatMap(e -> e).lift(StreamDocument.parse()).concatMap(e -> e).filter(e -> e.type() != NavajoStreamEvent.NavajoEventTypes.NAVAJO_STARTED && e.type() != NavajoStreamEvent.NavajoEventTypes.NAVAJO_DONE);
    Flowable<DataItem> fw = Flowable.just(DataItem.ofEventStream(flow));
    return fw;
}
Also used : ReactiveResolvedParameters(com.dexels.navajo.reactive.api.ReactiveResolvedParameters) Type(com.dexels.navajo.document.stream.DataItem.Type) ReactiveParameters(com.dexels.navajo.reactive.api.ReactiveParameters) DataItem(com.dexels.navajo.document.stream.DataItem) StreamScriptContext(com.dexels.navajo.document.stream.api.StreamScriptContext) ImmutableFactory(com.dexels.immutable.factory.ImmutableFactory) SourceMetadata(com.dexels.navajo.reactive.api.SourceMetadata) HttpMethod(org.eclipse.jetty.http.HttpMethod) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) ReactiveResolvedParameters(com.dexels.navajo.reactive.api.ReactiveResolvedParameters) Flowable(io.reactivex.Flowable) 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) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) ReactiveSource(com.dexels.navajo.reactive.api.ReactiveSource) DataItem(com.dexels.navajo.document.stream.DataItem) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent)

Example 9 with NavajoStreamEvent

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

the class CallRemoteTransformer method execute.

@Override
public FlowableTransformer<DataItem, DataItem> execute(StreamScriptContext context, Optional<ImmutableMessage> current, ImmutableMessage param) {
    ReactiveResolvedParameters resolved = parameters.resolve(context, current, param, metadata);
    String server = resolved.paramString("server");
    String username = resolved.paramString("username");
    String password = resolved.paramString("password");
    final String service = resolved.paramString("service");
    return flow -> {
        Flowable<NavajoStreamEvent> result = client.callWithBodyToStream(server, e -> e.header("X-Navajo-Username", username).header("X-Navajo-Password", password).header("X-Navajo-Service", service), flow.map(di -> di.eventStream()).concatMap(e -> e).lift(StreamDocument.serialize()), "text/xml").lift(XML.parseFlowable(10)).concatMap(e -> e).lift(StreamDocument.parse()).concatMap(e -> e).filter(e -> e.type() != NavajoStreamEvent.NavajoEventTypes.NAVAJO_STARTED && e.type() != NavajoStreamEvent.NavajoEventTypes.NAVAJO_DONE);
        return Flowable.just(DataItem.ofEventStream(result));
    };
}
Also used : ReactiveResolvedParameters(com.dexels.navajo.reactive.api.ReactiveResolvedParameters) ReactiveTransformer(com.dexels.navajo.reactive.api.ReactiveTransformer) ReactiveParameters(com.dexels.navajo.reactive.api.ReactiveParameters) DataItem(com.dexels.navajo.document.stream.DataItem) TransformerMetadata(com.dexels.navajo.reactive.api.TransformerMetadata) StreamScriptContext(com.dexels.navajo.document.stream.api.StreamScriptContext) FlowableTransformer(io.reactivex.FlowableTransformer) NavajoStreamEvent(com.dexels.navajo.document.stream.events.NavajoStreamEvent) ReactiveResolvedParameters(com.dexels.navajo.reactive.api.ReactiveResolvedParameters) Flowable(io.reactivex.Flowable) 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) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Flowable(io.reactivex.Flowable)

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