use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.
the class Expression method replacePropertyValues.
public static final String replacePropertyValues(String clause, Navajo inMessage) {
// Find all property references in clause.
StringBuilder result = new StringBuilder();
int begin = clause.indexOf('[');
if (// Clause does not contain properties.
begin == -1)
return clause;
result.append(clause.substring(0, begin));
while (begin >= 0) {
int end = clause.indexOf(']');
String propertyRef = clause.substring(begin + 1, end);
Property prop = inMessage.getProperty(propertyRef);
String value = "null";
if (prop != null) {
String type = prop.getType();
if (type.equals(Property.SELECTION_PROPERTY)) {
if (!prop.getCardinality().equals("+")) {
// Uni-selection property.
try {
List<Selection> list = prop.getAllSelectedSelections();
if (!list.isEmpty()) {
Selection sel = list.get(0);
value = sel.getValue();
}
} catch (com.dexels.navajo.document.NavajoException te) {
throw new TMLExpressionException(te.getMessage());
}
} else {
// Multi-selection property.
try {
List<Selection> list = prop.getAllSelectedSelections();
List<String> selectedValues = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Selection sel = list.get(i);
String o = sel.getValue();
selectedValues.add(o);
}
value = String.join(";", selectedValues);
} catch (NavajoException te) {
throw new TMLExpressionException(te.getMessage(), te);
}
}
} else if (type.equals(Property.STRING_PROPERTY)) {
value = "\"" + prop.getValue() + "\"";
} else {
value = prop.getValue();
}
}
result.append("{" + value + "}");
clause = clause.substring(end + 1, clause.length());
begin = clause.indexOf('[');
if (begin >= 0)
result.append(clause.substring(0, begin));
else
result.append(clause.substring(0, clause.length()));
}
return result.toString();
}
use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.
the class BaseMessageImpl method merge.
@Override
public void merge(Message incoming, boolean preferThis, boolean applySubType) {
if (isArrayMessage() != incoming.isArrayMessage()) {
throw new NavajoException("Incompatible message types in merge: " + getFullMessageName() + " (" + getType() + "), " + incoming.getFullMessageName() + " (" + incoming.getType() + ")");
}
if (this.isArrayMessage() && incoming.isArrayMessage() && incoming.getDefinitionMessage() != null) {
// Perform merge for all my children with the definition message
for (Message child : this.getElements()) {
child.merge(incoming.getDefinitionMessage(), preferThis);
}
}
if (incoming.getScope() != null && (this.getScope() == null || this.getScope().equals(""))) {
this.setScope(incoming.getScope());
}
if (incoming.getSubType() != null && (this.getSubType() == null || this.getSubType().equals(""))) {
this.setSubType(incoming.getSubType());
}
// Check if message with incoming name exists.
if (!getName().equals(incoming.getName())) {
incoming.setName(getName());
}
List<Property> properties = incoming.getAllProperties();
for (int i = 0; i < properties.size(); i++) {
Property p = (Property) properties.get(i).clone();
Property otherProperty = null;
if (preferThis) {
otherProperty = getProperty(p.getName());
}
if (!preferThis || otherProperty == null) {
addProperty(p);
}
// If we don't have a method set, use the incoming method
if (otherProperty != null && otherProperty.getMethod().equals("")) {
otherProperty.setMethod(p.getMethod());
}
}
List<Message> incomingSubMessages = incoming.getAllMessages();
for (int i = 0; i < incomingSubMessages.size(); i++) {
Message incomingSubMessage = incomingSubMessages.get(i);
// check if we have a sub message with the same name
Message existingSubMessage = getMessage(incomingSubMessage.getName());
if (existingSubMessage != null) {
// incoming message, if not, we cannot use it
if (existingSubMessage.isArrayMessage() != incomingSubMessage.isArrayMessage()) {
throw new NavajoException("Incompatible message types in merge: " + existingSubMessage.getFullMessageName() + " (" + existingSubMessage.getType() + "), " + incomingSubMessage.getFullMessageName() + " (" + incomingSubMessage.getType() + ")");
}
}
if (existingSubMessage == null) {
// if we don't have the sub message ourselves and if the incoming sub message is marked
// as nullable, then we should NOT add it (because we explicitly allow the message
// to not exist)
String nullableString = incomingSubMessage.getSubType("nullable");
boolean nullable = nullableString != null && Boolean.parseBoolean(nullableString);
if (nullable && applySubType) {
continue;
} else {
addMessage(incomingSubMessage.copy());
}
} else {
existingSubMessage.merge(incomingSubMessage, preferThis);
}
}
}
use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.
the class CheckUniqueness method evaluate.
@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
if (getOperands().size() < 2) {
for (int i = 0; i < getOperands().size(); i++) {
Object o = getOperands().get(i);
logger.debug("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
}
throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
}
if (!(operand(0).value instanceof String && operand(1).value instanceof String)) {
throw new TMLExpressionException(this, "Wrong argument types: " + operand(0).value.getClass() + " and " + operand(1).value.getClass());
}
String messageName = getStringOperand(0);
String propertyName = getStringOperand(1);
String filter = null;
if (getOperands().size() > 2) {
filter = getStringOperand(2);
}
Message parent = getCurrentMessage();
Navajo doc = getNavajo();
boolean isUnique = true;
HashSet<Object> values = new HashSet<Object>();
try {
List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
if (arrayMsg == null) {
throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
}
for (int i = 0; i < arrayMsg.size(); i++) {
Message m = arrayMsg.get(i);
Property p = m.getProperty(propertyName);
boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
if (evaluate) {
if (p != null) {
Object o = null;
if (p.getType().equals(Property.SELECTION_PROPERTY)) {
o = p.getSelected().getValue();
} else {
o = p.getTypedValue();
}
if (values.contains(o)) {
return Boolean.FALSE;
} else {
values.add(o);
}
}
}
}
} catch (NavajoException ne) {
throw new TMLExpressionException(this, ne.getMessage());
} catch (SystemException se) {
throw new TMLExpressionException(this, se.getMessage());
}
return (isUnique ? Boolean.TRUE : Boolean.FALSE);
}
use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.
the class NavajoRequestToString method evaluate.
@Override
public Object evaluate() throws TMLExpressionException {
Navajo in = getNavajo().copy();
in.removeHeader();
in.removeInternalMessages();
StringWriter ws = new StringWriter();
try {
in.write(ws);
} catch (NavajoException e) {
throw new TMLExpressionException(this, e.getMessage(), e);
}
return ws.toString();
}
use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.
the class NqlServlet method doGet.
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String server = req.getParameter("server");
String query = req.getParameter("query");
String ping = req.getParameter("ping");
String tenant = req.getParameter("tenant");
if (tenant == null) {
// Fall back to header
tenant = req.getHeader("X-Navajo-Instance");
}
if (ping != null) {
if (!checkPing(username, resp)) {
throw new ServletException("ping failed.");
}
return;
}
NavajoRemoteContext nrc = new NavajoRemoteContext();
nrc.setupClient(server, username, password, req.getServerName(), req.getServerPort(), req.getContextPath(), "/PostmanLegacy");
// new NQLContext();
NqlContextApi nc = getNqlContext();
nc.setNavajoContext(getClientContext());
try {
nc.executeCommand(query, tenant, username, password, new OutputCallback() {
@Override
public void setOutputType(String mime) {
resp.setContentType(mime);
}
@Override
public void setContentLength(long l) {
resp.setContentLength((int) l);
resp.setHeader("Accept-Ranges", "none");
resp.setHeader("Connection", "close");
}
@Override
public OutputStream getOutputStream() {
try {
return resp.getOutputStream();
} catch (IOException e) {
logger.error("Error: ", e);
return null;
}
}
});
} catch (ClientException e) {
logger.error("Error: ", e);
} catch (NavajoException e) {
logger.error("Error: ", e);
}
resp.getOutputStream().flush();
resp.getOutputStream().close();
// String
}
Aggregations