use of com.dexels.navajo.article.APIException in project navajo by Dexels.
the class BaseRuntimeImpl method execute.
@Override
public void execute(ArticleContext context) throws APIException, NoJSONOutputException {
verifyScopes();
List<XMLElement> children = article.getChildren();
try {
List<JsonNode> elements = new ArrayList<JsonNode>();
for (XMLElement e : children) {
String name = e.getName();
if (name.startsWith("_")) {
// at runtime
continue;
}
ArticleCommand ac = context.getCommand(name);
if (ac == null) {
throw new APIException("Unknown command: " + name, null, APIErrorCode.InternalError);
}
Map<String, String> parameters = new HashMap<String, String>();
for (Iterator<String> iterator = e.enumerateAttributeNames(); iterator.hasNext(); ) {
String attributeName = iterator.next();
parameters.put(attributeName, e.getStringAttribute(attributeName));
}
JsonNode node = ac.execute(this, context, parameters, e);
if (node != null) {
elements.add(node);
}
}
if (elements.size() == 0) {
writeNode(rootNode);
} else if (elements.size() == 1) {
// HUH?
writeNode(elements.iterator().next());
} else {
ArrayNode an = getObjectMapper().createArrayNode();
for (JsonNode jsonNode : elements) {
an.add(jsonNode);
}
writeNode(an);
}
// mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
// mapper.writeValue(getOutputWriter(), rootNode);
// commit();
} catch (IOException e1) {
logger.error("Error: ", e1);
}
}
use of com.dexels.navajo.article.APIException in project navajo by Dexels.
the class ArticleTmlRunnable method runInternal.
private void runInternal() throws APIException {
try {
runtime.getAccess().setQueueId(this.getRequestQueue().getId());
runtime.getAccess().queueTime = (int) (System.currentTimeMillis() - scheduledAt);
runtime.execute(context);
runtime.getAccess().setExitCode(Access.EXIT_OK);
} catch (NoJSONOutputException e) {
httpResponse.setContentType(e.getMimeType());
try {
IOUtils.copy(e.getStream(), httpResponse.getOutputStream());
} catch (IOException e1) {
throw new APIException(e1.getMessage(), e1, APIErrorCode.InternalError);
}
return;
} catch (APIException apiException) {
if (apiException.getErrorCode() == APIErrorCode.InternalError) {
logExceptionToAccess(runtime.getAccess(), apiException, createNavajoFromRequest(httpRequest));
} else if (apiException.getErrorCode() == APIErrorCode.ConditionError) {
runtime.getAccess().setExitCode(Access.EXIT_VALIDATION_ERR);
}
throw apiException;
} catch (Throwable e) {
logExceptionToAccess(runtime.getAccess(), e, createNavajoFromRequest(httpRequest));
throw new APIException(e.getMessage(), e, APIErrorCode.InternalError);
}
}
use of com.dexels.navajo.article.APIException in project navajo by Dexels.
the class ArticleBaseServlet method service.
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.addHeader("Access-Control-Allow-Origin", "*");
response.setContentType("application/json; charset=utf-8");
doServiceImpl(request, response);
} catch (Throwable t1) {
APIException exception = (t1 instanceof APIException) ? (APIException) t1 : new APIException(t1.getMessage(), t1, APIErrorCode.InternalError);
// If we get an internal error, we want to know about it in our logging system.
if (exception.getErrorCode().getHttpStatusCode() >= 500) {
logger.error("Error {}", t1);
}
try {
writeJSONErrorResponse(exception, response);
} catch (Throwable t2) {
logger.error("Failed to write JSON error response", t2);
try {
APIErrorCode internal = APIErrorCode.InternalError;
response.sendError(internal.getHttpStatusCode(), internal.getDescription());
} catch (Throwable t3) {
// We've failed to return the error to the user. We cannot do anything anymore.
logger.error("Failed to deliver error {}", t3);
}
}
}
}
use of com.dexels.navajo.article.APIException in project navajo by Dexels.
the class BaseContextImpl method interpretMeta.
public void interpretMeta(XMLElement article, ObjectMapper mapper, ObjectNode articleNode, boolean extended) throws APIException {
String outputType = article.getStringAttribute("output");
if (outputType != null) {
articleNode.put("output", outputType);
}
String scopes = article.getStringAttribute("scopes");
if (scopes != null) {
String[] scopeArray = scopes.split(",");
ArrayNode scopeArgs = mapper.createArrayNode();
for (String scope : scopeArray) {
scopeArgs.add(scope);
}
articleNode.set("scopes", scopeArgs);
}
String description = article.getStringAttribute("description");
if (extended && description != null && description.length() != 0) {
articleNode.put("description", description);
}
XMLElement argTag = article.getChildByTagName("_arguments");
ArrayNode inputArgs = mapper.createArrayNode();
articleNode.set("input", inputArgs);
if (argTag != null) {
List<XMLElement> args = argTag.getChildren();
for (XMLElement xmlElement : args) {
// name="aantalregels" description="Maximum aantal regels"
// type="integer" optional="true" default="5"
ObjectNode input = mapper.createObjectNode();
input.put("name", xmlElement.getStringAttribute("name"));
input.put("description", xmlElement.getStringAttribute("description"));
input.put("type", xmlElement.getStringAttribute("type"));
final boolean optional = xmlElement.getBooleanAttribute("optional", "true", "false", false);
input.put("optional", optional);
final String defaultValue = xmlElement.getStringAttribute("default");
if (defaultValue != null) {
input.put("default", defaultValue);
}
final String sourcearticle = xmlElement.getStringAttribute("sourcearticle");
if (sourcearticle != null) {
input.put("sourcearticle", sourcearticle);
}
final String sourcekey = xmlElement.getStringAttribute("sourcekey");
if (sourcekey != null) {
input.put("sourcekey", sourcekey);
}
inputArgs.add(input);
}
}
ArrayNode outputArgs = mapper.createArrayNode();
articleNode.set("output", outputArgs);
List<XMLElement> children = article.getChildren();
for (XMLElement e : children) {
String name = e.getName();
if (name.startsWith("_")) {
continue;
}
ArticleCommand ac = getCommand(name);
if (ac == null) {
throw new APIException("Unknown command: " + name, null, APIErrorCode.InternalError);
}
Map<String, String> parameters = new HashMap<String, String>();
for (Iterator<String> iterator = e.enumerateAttributeNames(); iterator.hasNext(); ) {
String attributeName = iterator.next();
parameters.put(attributeName, e.getStringAttribute(attributeName));
}
ac.writeMetadata(e, outputArgs, mapper);
}
}
use of com.dexels.navajo.article.APIException in project navajo by Dexels.
the class ElementCommand method execute.
@Override
public JsonNode execute(ArticleRuntime runtime, ArticleContext context, Map<String, String> parameters, XMLElement element) throws APIException, NoJSONOutputException {
String service = parameters.get("service");
Navajo current = null;
if (service == null) {
current = runtime.getNavajo();
} else {
current = runtime.getNavajo(service);
}
if (current == null) {
throw new APIException("No current navajo found for service " + service, null, APIErrorCode.InternalError);
}
String name = parameters.get("name");
if (name == null) {
throw new APIException("No 'name' parameter found in element for service " + service + " we've got parameters " + parameters, null, APIErrorCode.InternalError);
}
String propertyName = parameters.get("propertyName");
if (propertyName == null) {
propertyName = name;
}
Property p = current.getProperty(propertyName);
if (p == null) {
throw new APIException("No property: " + propertyName + " found in current navajo for service " + service, null, APIErrorCode.InternalError);
}
if (parameters.get("direct") != null) {
Object value = p.getTypedValue();
if (value instanceof Binary) {
Binary b = (Binary) value;
String mime = b.getMimeType();
if (mime == null) {
mime = b.guessContentType();
}
throw new NoJSONOutputException(mime, b.getDataAsStream());
} else {
String string = "" + value;
ByteArrayInputStream bais = new ByteArrayInputStream(string.getBytes());
throw new NoJSONOutputException("text/plain", bais);
}
}
if (name.indexOf('/') != -1) {
String msgpath = name.substring(0, name.lastIndexOf('/'));
String propname = name.substring(name.lastIndexOf('/') + 1, name.length());
ObjectNode msgNode = runtime.getGroupNode(msgpath);
APIValue.setValueOnNodeForType(msgNode, propname, parameters.get("type"), p, runtime);
return null;
} else {
ObjectNode on = runtime.getRootNode();
APIValue.setValueOnNodeForType(on, name, parameters.get("type"), p, runtime);
return on;
}
}
Aggregations