public static SynapseJsonPath getSynapseJsonPath(final String jsonPathExpression) throws JaxenException { return new SynapseJsonPath(jsonPathExpression); }
/** * Evaluate JSON path and retrieve the result as JsonElement. If multiple matches found, combine matching results * in a comma separated list and parse as a JSON array. * * @param messageContext messageContext which contains the JSON payload. * @return JsonArray or a JsonPrimitive depending on the JsonPath response. */ public static JsonElement getJSONElement(MessageContext messageContext, SynapseJsonPath jsonPath) { JsonParser parser = new JsonParser(); Object objectList = jsonPath.evaluate(messageContext); if (objectList instanceof List) { List list = (List) objectList; if (!list.isEmpty()) { if (list.size() > 1) { String result = "[" + StringUtils.join(list, ',') + "]"; JsonElement element = parser.parse(result); return element.getAsJsonArray(); } JsonElement result; String resultString = list.get(0).toString().trim(); result = tryParseJsonString(parser, resultString); return result; } } return null; }
public String stringValueOf(final String jsonString) { if (jsonString == null) { return ""; } if (isWholeBody) { return jsonString; } Object read; read = formatJsonPathResponse(jsonPath.read(jsonString)); return (null == read ? "null" : read.toString()); }
public SynapseJsonPath(String jsonPathExpression) throws JaxenException { super(jsonPathExpression, SynapsePath.JSON_PATH, log); // Set default configuration for Jayway JsonPath setJsonPathConfiguration(); this.contentAware = true; this.expression = jsonPathExpression; // Though SynapseJsonPath support "$.", the JSONPath implementation does not support it if (expression.endsWith(".")) { expression = expression.substring(0, expression.length() - 1); } jsonPath = JsonPath.compile(expression); // Check if the JSON path expression evaluates to the whole payload. If so no point in evaluating the path. if ("$".equals(jsonPath.getPath().trim()) || "$.".equals(jsonPath.getPath().trim())) { isWholeBody = true; } this.setPathType(SynapsePath.JSON_PATH); }
if (null == amc.getEnvelope().getBody().getFirstElement()) { stream = getMessageInputStreamPT(amc); if (stream == null) { stream = JsonUtil.getJsonPayload(amc); return stringValueOf(stream); }else{ log.warn("Json Payload is empty."); handleException("Could not find JSON Stream in PassThrough Pipe during JSON path evaluation.", e); return stringValueOf(stream);
private List listValueOf(MessageContext synCtx) { org.apache.axis2.context.MessageContext amc = ((Axis2MessageContext) synCtx).getAxis2MessageContext(); InputStream stream; if (!JsonUtil.hasAJsonPayload(amc) || "true".equals(enableStreamingJsonPath)) { try { if (null == amc.getEnvelope().getBody().getFirstElement()) { // Get message from PT Pipe. stream = getMessageInputStreamPT(amc); if (stream == null) { stream = JsonUtil.getJsonPayload(amc); } else { JsonUtil.getNewJsonPayload(amc, stream, true, true); } } else { // Message Already built. stream = JsonUtil.toJsonStream(amc.getEnvelope().getBody().getFirstElement()); } return listValueOf(stream); } catch (IOException e) { handleException("Could not find JSON Stream in PassThrough Pipe during JSON path evaluation.", e); } } else { stream = JsonUtil.getJsonPayload(amc); return listValueOf(stream); } return null; }
read = formatJsonPathResponse(jsonPath.read(jsonStream)); if (log.isDebugEnabled()) { log.debug("#stringValueOf. Evaluated JSON path <" + jsonPath.getPath() + "> : <" + (read == null ? null : read.toString()) + ">"); handleException("Error evaluating JSON Path <" + jsonPath.getPath() + ">", e); } catch (Exception e) { // catch invalid json paths that do not match with the existing JSON payload. if (log.isDebugEnabled()) {
String expression = synapseJsonPath.getJsonPath().getPath(); newJsonString = getNewJSONString(sourceNode, expression, jsonString, expression, false); } else if (action.equalsIgnoreCase(ACTION_ADD_SIBLING)) { String parentPath = synapseJsonPath.getParentPath(); if (parentPath != null) { newJsonString = getNewJSONString(sourceNode, expression, jsonString, parentPath, true);
if (xpath != null) { sourceJsonPath = (SynapseJsonPath) this.xpath; jsonPath = sourceJsonPath.getJsonPath().getPath();
/** * Read the JSON Stream and returns a list of objects using the jsonPath. */ @Override public Object evaluate(Object object) { List result = null; if (object != null) { if (object instanceof MessageContext) { MessageContext synCtx = (MessageContext) object; result = listValueOf(synCtx); } else if (object instanceof String) { result = listValueOf(IOUtils.toInputStream(object.toString())); } } return result; }
if (xpath != null) { SynapseJsonPath sourceJsonPath = (SynapseJsonPath) this.xpath; jsonPath = sourceJsonPath.getJsonPath().getPath();
private SynapseJsonPath createJSONPath(String expression) throws JaxenException { SynapseJsonPath xpath = new SynapseJsonPath(expression); return xpath; }
try { Object object = jsonPath.read(jsonStream); object = formatJsonPathResponse(jsonPath.read(jsonStream)); if (object != null) { if (object instanceof List && !jsonPath.isDefinite()) {
if (correlateExpression != null) { result = correlateExpression instanceof SynapseXPath ? correlateExpression.evaluate(synCtx) : ((SynapseJsonPath) correlateExpression).evaluate(synCtx); if (result instanceof List) { if (((List) result).isEmpty()) {
/** * Retrieving dynamic key * * @return SynapsePath */ public SynapsePath getExpression() { if (expression == null && keyValue != null && hasExprTypeKey()) { try { String expressionString = keyValue.substring(1, keyValue.length() - 1); if (expressionString.startsWith("json-eval(")) { // Remove "json-eval" and extract the json expression SynapseJsonPath expressionTypeKey = new SynapseJsonPath(expressionString.substring(10, expressionString.length() - 1)); expression = expressionTypeKey; } else { SynapseXPath expressionTypeKey = new SynapseXPath(expressionString); for (OMNamespace aNamespaceList : namespaceList) { expressionTypeKey.addNamespace(aNamespaceList); } expression = expressionTypeKey; } } catch (JaxenException e) { expression = null; handleException("Can not evaluate escaped expression.."); } } return expression; }
path = new SynapseJsonPath(pathAttrib.getAttributeValue().substring(10, pathAttrib.getAttributeValue().length() - 1)); } else { try {