@SuppressWarnings("unchecked") private void setupRoutes() { before( (req, res) -> { log.info("Path is: " + req.pathInfo()); CallingContext ctx = ctxs.get(req.session().id()); if (ctx == null) { // check x-api-key header String apiKey = req.headers("x-api-key"); if (StringUtils.isNotBlank(apiKey)) { ctx = Kernel.INSTANCE.loadContext(APP_KEY, apiKey); if (ctx == null) { String msg = String.format("Invalid apiKey [%s] for app [%s]", apiKey, APP_KEY); log.warn(msg); halt(401, msg); } String id = req.session(true).id(); ctxs.put(id, ctx); } else { log.warn("x-api-key header not found, rejecting..."); halt(401, "Please provide 'x-api-key' header to access this API"); } } }); post( "/doc/:authority", (req, res) -> { log.info(req.body()); Map<String, Object> data = JacksonUtil.getMapFromJson(req.body()); String authority = req.params(":authority"); String config = (String) data.get("config"); CallingContext ctx = getContext(req); if (Kernel.getDoc().docRepoExists(ctx, authority)) { halt(409, String.format("Repo [%s] already exists", authority)); } Kernel.getDoc().createDocRepo(ctx, authority, config); return new RaptureURI(authority, Scheme.DOCUMENT).toString(); }); get( "/doc/*", (req, res) -> { String meta = req.queryParams("meta"); if (StringUtils.isNotBlank(meta)) { return Kernel.getDoc().getDocAndMeta(getContext(req), getDocUriParam(req)); } return Kernel.getDoc().getDoc(getContext(req), getDocUriParam(req)); }); put( "/doc/*", (req, res) -> { return Kernel.getDoc().putDoc(getContext(req), getDocUriParam(req), req.body()); }); delete( "/doc/:authority", (req, res) -> { Kernel.getDoc().deleteDocRepo(getContext(req), req.params(":authority")); return true; }); delete( "/doc/*", (req, res) -> { return Kernel.getDoc().deleteDoc(getContext(req), getDocUriParam(req)); }); post( "/blob/:authority", (req, res) -> { log.info(req.body()); Map<String, Object> data = JacksonUtil.getMapFromJson(req.body()); String authority = req.params(":authority"); String config = (String) data.get("config"); String metaConfig = (String) data.get("metaConfig"); CallingContext ctx = getContext(req); if (Kernel.getBlob().blobRepoExists(ctx, authority)) { halt(409, String.format("Repo [%s] already exists", authority)); } Kernel.getBlob().createBlobRepo(ctx, authority, config, metaConfig); return new RaptureURI(authority, Scheme.BLOB).toString(); }); get( "/blob/*", (req, res) -> { return Kernel.getBlob().getBlob(getContext(req), getBlobUriParam(req)); }, json()); put( "/blob/*", (req, res) -> { String uri = getBlobUriParam(req); Kernel.getBlob().putBlob(getContext(req), uri, req.bodyAsBytes(), req.contentType()); return uri; }); delete( "/blob/:authority", (req, res) -> { Kernel.getBlob().deleteBlobRepo(getContext(req), req.params(":authority")); return true; }); delete( "/blob/*", (req, res) -> { Kernel.getBlob().deleteBlob(getContext(req), getBlobUriParam(req)); return true; }); post( "/series/:authority", (req, res) -> { log.info(req.body()); Map<String, Object> data = JacksonUtil.getMapFromJson(req.body()); String authority = req.params(":authority"); String config = (String) data.get("config"); CallingContext ctx = getContext(req); if (Kernel.getSeries().seriesRepoExists(ctx, authority)) { halt(409, String.format("Repo [%s] already exists", authority)); } Kernel.getSeries().createSeriesRepo(ctx, authority, config); return new RaptureURI(authority, Scheme.SERIES).toString(); }); get( "/series/*", (req, res) -> { return Kernel.getSeries().getPoints(getContext(req), getSeriesUriParam(req)); }, json()); put( "/series/*", (req, res) -> { String uri = getSeriesUriParam(req); Map<String, Object> data = JacksonUtil.getMapFromJson(req.body()); List<String> keys = (List<String>) data.get("keys"); List<Object> values = (List<Object>) data.get("values"); if (!values.isEmpty()) { Object obj = values.get(0); if (obj instanceof Long) { Kernel.getSeries() .addLongsToSeries(getContext(req), uri, keys, (List<Long>) (List<?>) values); } else if (obj instanceof String) { Kernel.getSeries() .addStringsToSeries(getContext(req), uri, keys, (List<String>) (List<?>) values); } else if (obj instanceof Double) { Kernel.getSeries() .addDoublesToSeries(getContext(req), uri, keys, (List<Double>) (List<?>) values); } else { halt(400, "Unknown type in values parameter"); } } return uri; }); delete( "/series/:authority", (req, res) -> { Kernel.getSeries().deleteSeriesRepo(getContext(req), req.params(":authority")); return true; }); delete( "/series/*", (req, res) -> { Kernel.getSeries().deleteSeries(getContext(req), getSeriesUriParam(req)); return true; }); post( "/workorder/*", (req, res) -> { Map<String, String> params = new HashMap<>(); String body = req.body(); if (StringUtils.isNotBlank(body)) { Map<String, Object> data = JacksonUtil.getMapFromJson(body); params = (Map<String, String>) data.get("params"); } return Kernel.getDecision() .createWorkOrder(getContext(req), getWorkorderUriParam(req), params); }); }
@Override public String invoke(CallingContext ctx) { DecisionApi decision = Kernel.getDecision(); String workOrderUri = new RaptureURI(getWorkerURI(), Scheme.WORKORDER).toShortString(); String config = StringUtils.stripToNull(decision.getContextValue(ctx, workOrderUri, "CONFIGURATION")); try { decision.setContextLiteral(ctx, getWorkerURI(), "STEPNAME", getStepName()); String docPath = new RaptureURI(workOrderUri).getDocPath(); int lio = docPath.lastIndexOf('/'); if (lio < 0) lio = 0; StringBuilder externalUrl = new StringBuilder(); String host = System.getenv("HOST"); String port = System.getenv("PORT"); externalUrl .append("http://") .append((host != null) ? host : LOCALHOST) .append(":") .append((port != null) ? port : DEFAULT_RIM_PORT) .append("/process/") .append(docPath.substring(0, lio)) .append(WORKORDER_DELIMETER) .append(docPath.substring(lio + 1)); decision.setContextLiteral( ctx, workOrderUri, EXTERNAL_RIM_WORKORDER_URL, externalUrl.toString()); Map<String, String> view = new HashMap<>(); DocApi docApi = Kernel.getDoc(); if (config == null) { decision.writeWorkflowAuditEntry( ctx, getWorkerURI(), "No configuration document specified", false); return this.getNextTransition(); } List<String> configs; try { // Could be a list or a single entry. configs = JacksonUtil.objectFromJson(config, ArrayList.class); } catch (Exception e) { configs = ImmutableList.of(config); } for (String conf : configs) { if (docApi.docExists(ctx, conf)) { String doc = docApi.getDoc(ctx, conf); Map<String, Object> map = JacksonUtil.getMapFromJson(doc); for (Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); String value = StringUtils.stripToNull(entry.getValue().toString()); ContextValueType type = ContextValueType.getContextValueType(value.charAt(0)); if (type == ContextValueType.NULL) { type = ContextValueType.LITERAL; } else value = value.substring(1); ExecutionContextUtil.setValueECF(ctx, workOrderUri, view, key, type, value); decision.writeWorkflowAuditEntry( ctx, getWorkerURI(), getStepName() + ": Read configuration data from " + conf, false); } } else { decision.writeWorkflowAuditEntry( ctx, getWorkerURI(), getStepName() + ": Cannot locate configuration document " + conf, true); } } return Steps.NEXT.toString(); } catch (Exception e) { decision.setContextLiteral( ctx, getWorkerURI(), getStepName(), "Exception in workflow : " + e.getLocalizedMessage()); decision.setContextLiteral(ctx, getWorkerURI(), getErrName(), ExceptionToString.summary(e)); log.error(ExceptionToString.format(ExceptionToString.getRootCause(e))); decision.writeWorkflowAuditEntry( ctx, getWorkerURI(), "Problem in " + getStepName() + ": unable to read the configuration document " + config, true); decision.writeWorkflowAuditEntry( ctx, getWorkerURI(), ": " + ExceptionToString.getRootCause(e).getLocalizedMessage(), true); return getErrorTransition(); } }