protected DocumentMetadata getMetaFromVersionKey(String versionDocPath) { String metaData = metaStore.get(versionDocPath); if (metaData != null) { return JacksonUtil.objectFromJson(metaData, DocumentMetadata.class); } return null; }
public EmailAlerter(String templateName) { String templateJson = Kernel.getSys() .retrieveSystemConfig( ContextFactory.getKernelUser(), "CONFIG", TEMPLATE_URI + templateName); this.emailTemplate = JacksonUtil.objectFromJson(templateJson, EmailTemplate.class); }
public static EmailTemplate getEmailTemplate(CallingContext context, String templateName) { String templateJson = Kernel.getSys().retrieveSystemConfig(context, "CONFIG", EMAIL_TEMPLATE_DIR + templateName); if (StringUtils.isBlank(templateJson)) { throw RaptureExceptionFactory.create("Email template " + templateName + " does not exist"); } return JacksonUtil.objectFromJson(templateJson, EmailTemplate.class); }
@Override public ReflexValue readContent(ReflexStreamValue file, IReflexIOHandler ioHandler) { // The file is one big JSON document, and in fact is an array, so // convert it to an array ReflexValue content = ioHandler.getContent(file); List<?> ret = JacksonUtil.objectFromJson(content.toString(), List.class); return new ReflexValue(ret); }
public static SMTPConfig getSMTPConfig() { String configString = Kernel.getSys() .retrieveSystemConfig(ContextFactory.getKernelUser(), "CONFIG", SMTP_CONFIG_URL); if (StringUtils.isBlank(configString)) { throw RaptureExceptionFactory.create("No SMTP configured"); } return JacksonUtil.objectFromJson(configString, SMTPConfig.class); }
@Test public void testGetRepositories() { List<DocumentRepoConfig> documentRepositories = Kernel.getDoc().getDocRepoConfigs(callingContext); int preexisting = documentRepositories.size(); if (!Kernel.getDoc().docRepoExists(callingContext, repoUri)) { Kernel.getDoc().createDocRepo(callingContext, repoUri, config); } documentRepositories = Kernel.getDoc().getDocRepoConfigs(callingContext); assertEquals( JacksonUtil.jsonFromObject(documentRepositories), preexisting + 1, documentRepositories.size()); }
@Override public PluginTransportItem encode(CallingContext ctx, String uri) { try { Object o = getReflectionObject(ctx, uri); MessageDigest md = MessageDigest.getInstance("MD5"); PluginTransportItem item = new PluginTransportItem(); item.setContent(JacksonUtil.bytesJsonFromObject(o)); md.update(item.getContent()); item.setHash(Hex.encodeHexString(md.digest())); item.setUri(uri); return item; } catch (NoSuchAlgorithmException e) { throw RaptureExceptionFactory.create( HttpURLConnection.HTTP_INTERNAL_ERROR, "Error encoding item", e); } }
@Override public void addMetadata(CallingContext context, Map<String, String> values, Boolean overwrite) { if ((values == null) || values.isEmpty()) return; Map<String, String> metadata = context.getMetadata(); if (metadata == null) metadata = new HashMap<String, String>(); for (String key : values.keySet()) { if (!overwrite && metadata.containsKey(key)) { throw RaptureExceptionFactory.create( HttpURLConnection.HTTP_BAD_REQUEST, key + " exists and overwrite was disallowed"); } metadata.put(key, values.get(key)); } context.setMetadata(metadata); getEphemeralRepo() .addToStage( RaptureConstants.OFFICIAL_STAGE, "session/" + context.getContext(), JacksonUtil.jsonFromObject(context), false); }
/** * Insert values into the array, preserving the original order of the request * * @param docsWithMeta - array to set values in with the correct order * @param positions - the original ordering * @param contents - the document contents * @param metaContents - the meta document contents */ protected void constructDocumentWithMetaList( DocumentWithMeta[] docsWithMeta, List<RaptureURI> uris, List<Integer> positions, List<String> contents, List<String> metaContents) { if (contents.size() != metaContents.size()) { log.error("Batch getDocAndMetas failed due to different size of content vs metaContent"); return; } for (int i = 0; i < contents.size(); i++) { DocumentWithMeta dwm = new DocumentWithMeta(); String meta = metaContents.get(i); if (meta != null) { dwm.setMetaData(JacksonUtil.objectFromJson(meta, DocumentMetadata.class)); } dwm.setContent(contents.get(i)); dwm.setDisplayName(uris.get(positions.get(i)).getDocPath()); docsWithMeta[positions.get(i)] = dwm; } }
@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); }); }
HooksConfig readFromFile() { String json = ConfigLoader.getConf().DefaultApiHooks; return JacksonUtil.objectFromJson(json, HooksConfig.class); }
@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(); } }
public static ReflexValue deserialize(String json) throws ClassNotFoundException { Object x = JacksonUtil.objectFromJson(json, Object.class); return KernelExecutor.reconstructFromObject(x); }
protected void addLatestToMetaStore(String latestKey, DocumentMetadata newMetaData) { metaStore.put(latestKey, JacksonUtil.jsonFromObject(newMetaData)); }
private static SMTPConfig getEmailConfig() { String configString = Kernel.getSys().retrieveSystemConfig(ContextFactory.getKernelUser(), "CONFIG", CONFIG_URI); return JacksonUtil.objectFromJson(configString, SMTPConfig.class); }