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