use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class RESTAdapter method setDoSend.
public void setDoSend(String url, Navajo od) throws UserException, ConditionErrorException, SystemException {
this.url = url.trim();
if (dateFormat != null && !dateFormat.equals("")) {
json.setDateFormat(new SimpleDateFormat(dateFormat));
}
Writer w = new StringWriter();
Binary bContent = new Binary();
if (textContent == null) {
// Remove globals and parms message
if (od.getMessage("__globals__") != null)
od.removeMessage("__globals__");
if (od.getMessage(Message.MSG_PARAMETERS_BLOCK) != null)
od.removeMessage(Message.MSG_PARAMETERS_BLOCK);
if (od.getMessage(Message.MSG_TOKEN_BLOCK) != null)
od.removeMessage(Message.MSG_TOKEN_BLOCK);
if (od.getMessage(Message.MSG_AAA_BLOCK) != null)
od.removeMessage(Message.MSG_AAA_BLOCK);
try {
json.format(od, w, true);
bContent.getOutputStream().write(w.toString().getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
logger.error("Exception on parsing input navajo as JSON! Not performing REST call!");
throw new UserException(e.getMessage(), e);
}
} else {
try {
bContent.getOutputStream().write(textContent.toString().getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
logger.error("IOException on writing textcontent! Not performing REST call!");
throw new UserException(e.getMessage(), e);
}
}
HTTPMap http = new HTTPMap();
setupHttpMap(http, bContent);
http.setDoSend(true);
Binary result = http.getResult();
responseCode = http.getResponseCode();
responseMessage = http.getResponseMessage();
try {
if (result == null) {
throw new UserException(-1, "Null result");
}
if (responseCode >= 300) {
logger.warn("Got a non-200 response code: {}!", responseCode);
if (breakOnException) {
throw new UserException(responseCode, responseMessage);
}
}
rawResult = new String(result.getData());
if (jsonResponse) {
if (http.getResponseContentType() != null && http.getResponseContentType().contains("application/json")) {
try {
inDoc = json.parse(result.getDataAsStream(), topMessage);
} catch (Throwable t) {
logger.warn("Unable to parse response as JSON!", t);
if (breakOnException) {
throw t;
}
}
} else if (http.getResponseContentType() == null) {
logger.info("No response content type - creating empty navajo as response");
inDoc = NavajoFactory.getInstance().createNavajo();
} else {
logger.warn("Unexpected output content type: {}", http.getResponseContentType());
if (breakOnException) {
throw new UserException(-1, "Unexpected content type: " + http.getResponseContentType());
}
}
} else {
logger.debug("Non-json response - creating empty Navajo");
inDoc = NavajoFactory.getInstance().createNavajo();
}
} catch (Throwable e) {
logger.error("Exception on getting response", e);
if (breakOnException) {
logger.warn("Raw response data: {}", rawResult);
throw new UserException(e.getMessage(), e);
} else {
logger.warn("Exception on getting response, but breakOnException was set. Continuing!");
}
}
if (inDoc == null) {
logger.warn("No indoc - creating empty one");
inDoc = NavajoFactory.getInstance().createNavajo();
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class SQLMaintenanceMap method load.
@Override
public void load(Access access) throws MappableException, UserException {
// if (noAccess)
// throw new MappableException("Cannot enter maintenance object, already in use");
setNoAccess(true);
logger.debug("In SQLMaintenanceMap");
this.access = access;
this.config = DispatcherFactory.getInstance().getNavajoConfig();
try {
// sqlMapConfigFile = XMLutils.createNavajoInstance(config. getConfigPath() + "/sqlmap.xml");
sqlMapConfigFile = config.readConfig("/sqlmap.xml");
} catch (Exception e) {
throw new MappableException(e.getMessage());
}
// logger.debug(sqlMapConfigFile.toString());
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class PropertyMap method store.
@Override
public void store() throws MappableException, UserException {
try {
Message msg = access.getCurrentOutMessage();
if (msg == null) {
throw new UserException(-1, "PropertyMap adapter can only be called from within a constructed output message");
}
Property prop = null;
prop = msg.getProperty(name);
if (prop != null && !removeExisting) {
throw new UserException(-1, "Cannot add already existing property " + name + " when removeExisting is false.");
}
// this will create if property not found and otherwise update value and other attributes, except for subtype and the options
// Do not have to pass inDoc as we're not creating a param
prop = MappingUtils.setProperty(false, msg, name, currentValue, type, null, direction, description, length == null ? -1 : length, outMessage, null, false);
if (subtype != null) {
prop.setSubType(subtype);
}
// For the selection property, parse the given optionList and the multiple setting
if (Property.SELECTION_PROPERTY.equals(type)) {
if (optionList != null) {
prop.clearSelections();
for (Option o : optionList) {
Selection sel = NavajoFactory.getInstance().createSelection(outMessage, o.getName(), o.getValue(), o.getSelected());
prop.addSelectionWithoutReplace(sel);
}
}
prop.setCardinality(multiple ? "+" : "1");
}
} catch (Exception e) {
throw new UserException(-1, e.getMessage(), e);
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class QueryMap method store.
@Override
public void store() throws MappableException, UserException {
// Construct Navajo message.
try {
Message recordSet = NavajoFactory.getInstance().createMessage(outputDoc, "RecordSet", Message.MSG_TYPE_ARRAY);
try {
outputDoc.addMessage(recordSet);
} catch (NavajoException ex) {
throw new UserException(-1, ex.getMessage(), ex);
}
ResultSetMap[] resultSet = getResultSet();
for (int i = 0; i < resultSet.length; i++) {
Message record = NavajoFactory.getInstance().createMessage(outputDoc, "RecordSet", Message.MSG_TYPE_ARRAY_ELEMENT);
recordSet.addElement(record);
RecordMap[] columns = resultSet[i].getRecords();
for (int j = 0; j < columns.length; j++) {
try {
Object value = columns[j].getRecordValue();
String type = (value != null ? MappingUtils.determineNavajoType(value) : "unknown");
Property prop = NavajoFactory.getInstance().createProperty(outputDoc, columns[j].recordName, type, null, 0, "", Property.DIR_IN);
prop.setAnyValue(value);
record.addProperty(prop);
} catch (Exception ex1) {
throw new UserException(-1, ex1.getMessage(), ex1);
}
}
}
} finally {
super.store();
}
}
use of com.dexels.navajo.script.api.UserException in project navajo by Dexels.
the class NavajoMap method setAppend.
/**
* Set this to a valid message path if the result of the webservices needs to be appended. If messageOffset = "/" the entire result will be appended to the
* current output message pointer.
*
* @param b
* @throws UserException
*
* TODO: FINISH THIS. IMPLEMENT CLONE METHOD IN MESSAGE IMPLEMENTATION(!!)
*
* (!)if messageOffset is '', the received inDoc document will become the new output document for the Navajo service. if messageOffset is '/',
* the messages of the received inDoc will be appended to the output document.
*/
public final void setAppend(String messageOffset) throws UserException {
waitForResult();
if (messageOffset.equals("")) {
access.setOutputDoc(inDoc);
return;
}
try {
Navajo currentDoc = access.getOutputDoc();
Message currentMsg = access.getCurrentOutMessage();
List<Message> list = null;
// If append message equals '/'.
if (messageOffset.equals(Navajo.MESSAGE_SEPARATOR)) {
list = inDoc.getAllMessages();
} else if (inDoc.getMessage(messageOffset) == null) {
return;
} else if (!inDoc.getMessage(messageOffset).getType().equals(Message.MSG_TYPE_ARRAY)) {
list = new ArrayList<>();
list.add(inDoc.getMessage(messageOffset));
} else {
// For array messages...
list = new ArrayList<>();
list.add(inDoc.getMessage(messageOffset));
}
// If no messages were found, there is nothing to append
if (list.isEmpty()) {
return;
}
/**
* appendTo logic. If appendTo ends with '/' append the entire append message to the defined appendTo message. If appendTo does not end with '/',
* merge the append message with the defined appendTo message.
*/
boolean appendToComplete = (appendTo != null && !appendTo.equals(Navajo.MESSAGE_SEPARATOR) && appendTo.endsWith(Navajo.MESSAGE_SEPARATOR));
if (appendToComplete) {
// Strip last "/".
appendTo = appendTo.substring(0, appendTo.length() - 1);
}
// Check whether incoming array message needs to be expanded: if not
// appendToComplete and if appendTo is defined and
// appendTo is array message.
boolean appendToIsArray = false;
if (appendTo != null && currentMsg != null && currentMsg.getMessage(appendTo) != null && currentMsg.getMessage(appendTo).getType().equals(Message.MSG_TYPE_ARRAY)) {
appendToIsArray = true;
}
if (appendTo != null && currentDoc.getMessage(appendTo) != null && currentDoc.getMessage(appendTo).getType().equals(Message.MSG_TYPE_ARRAY)) {
appendToIsArray = true;
}
if (!appendToComplete && appendTo != null && list != null && list.get(0) != null && list.get(0).getType().equals(Message.MSG_TYPE_ARRAY) && appendToIsArray) {
// Expand list if it contains an array message.
list = list.get(0).getAllMessages();
}
if (list == null) {
logger.warn("Can not append: appendTo target can not be found");
return;
}
for (int i = 0; i < list.size(); i++) {
Message inMsg = list.get(i);
// Clone message and append it to currentMsg if it exists, else
// directly under currentDoc.
// currentDoc.importMessage(inMsg)
Message clone = inDoc.copyMessage(inMsg, currentDoc);
if (currentMsg != null) {
if (appendTo != null) {
if (appendTo.equals(Navajo.MESSAGE_SEPARATOR)) {
if (clone.getName().equals(currentMsg.getName())) {
currentMsg.merge(clone);
} else {
currentDoc.addMessage(clone, true);
}
} else if (currentMsg.getMessage(appendTo) != null) {
if (currentMsg.getMessage(appendTo).getType().equals(Message.MSG_TYPE_ARRAY)) {
// For
// array
// messages
// do
// not
// overwrite.
currentMsg.getMessage(appendTo).addMessage(clone);
} else {
if (appendToComplete) {
currentMsg.getMessage(appendTo).addMessage(clone);
} else {
currentMsg.getMessage(appendTo).merge(clone);
}
}
} else {
throw new UserException(-1, "Unknown appendTo message: " + appendTo);
}
} else {
// Merge message with existing message.
if (clone.getName().equals(currentMsg.getName())) {
currentMsg.merge(clone);
} else {
currentMsg.addMessage(clone, true);
}
}
} else {
if (appendTo != null) {
if (appendTo.equals(Navajo.MESSAGE_SEPARATOR)) {
currentDoc.addMessage(clone, true);
} else if (currentDoc.getMessage(appendTo) != null) {
if (currentDoc.getMessage(appendTo).getType().equals(Message.MSG_TYPE_ARRAY)) {
// For
// array
// messages
// do
// not
// overwrite.
currentDoc.getMessage(appendTo).addMessage(clone);
} else {
if (appendToComplete) {
currentDoc.getMessage(appendTo).addMessage(clone);
} else {
currentDoc.getMessage(appendTo).merge(clone);
}
}
} else {
throw new UserException(-1, "Unknown appendTo message: " + appendTo);
}
} else {
// existing message.
if (currentDoc.getMessage(clone.getName()) != null) {
currentDoc.getMessage(clone.getName()).merge(clone);
} else {
currentDoc.addMessage(clone, true);
}
}
}
}
} catch (NavajoException ne) {
throw new UserException(-1, ne.getMessage());
}
}
Aggregations