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);
}
};
}
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;
}
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();
}
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);
}
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();
}
}
Aggregations