|
19 | 19 |
|
20 | 20 | package org.apache.synapse.mediators.transform.pfutils; |
21 | 21 |
|
| 22 | +import com.google.gson.Gson; |
| 23 | +import com.google.gson.JsonElement; |
| 24 | +import com.google.gson.JsonPrimitive; |
| 25 | +import org.apache.axiom.om.OMElement; |
| 26 | +import org.apache.axiom.om.OMException; |
| 27 | +import org.apache.axis2.AxisFault; |
| 28 | +import org.apache.commons.io.IOUtils; |
22 | 29 | import org.apache.commons.logging.Log; |
23 | 30 | import org.apache.commons.logging.LogFactory; |
| 31 | +import org.apache.commons.text.StringEscapeUtils; |
24 | 32 | import org.apache.synapse.MessageContext; |
| 33 | +import org.apache.synapse.SynapseException; |
| 34 | +import org.apache.synapse.commons.json.JsonUtil; |
25 | 35 | import org.apache.synapse.mediators.transform.ArgumentDetails; |
| 36 | +import org.apache.synapse.util.xpath.SynapseExpression; |
| 37 | +import org.jaxen.JaxenException; |
26 | 38 |
|
27 | 39 | import java.util.HashMap; |
| 40 | +import java.util.Iterator; |
28 | 41 | import java.util.Map; |
29 | 42 | import java.util.regex.Matcher; |
30 | 43 | import java.util.regex.Pattern; |
| 44 | +import javax.xml.stream.XMLStreamException; |
31 | 45 |
|
32 | 46 | /** |
33 | 47 | * TemplateProcessor implementation for Regex based templates |
34 | 48 | */ |
35 | 49 | public class RegexTemplateProcessor extends TemplateProcessor { |
36 | 50 |
|
37 | 51 | private static final Log log = LogFactory.getLog(RegexTemplateProcessor.class); |
38 | | - private final Pattern pattern = Pattern.compile("\\$(\\d)+"); |
| 52 | + // Pattern matches "${...}" (quoted), ${...} (unquoted), and $n |
| 53 | + private final Pattern pattern = Pattern.compile("\"\\$\\{(.+?)\\}\"|\\$\\{(.+?)\\}|\\$(\\d+)"); |
| 54 | + |
| 55 | + private final Gson gson = new Gson(); |
39 | 56 |
|
40 | 57 | @Override |
41 | 58 | public String processTemplate(String template, String mediaType, MessageContext synCtx) { |
@@ -72,25 +89,157 @@ private void replace(String format, StringBuffer result, String mediaType, Messa |
72 | 89 | } |
73 | 90 | try { |
74 | 91 | while (matcher.find()) { |
75 | | - String matchSeq = matcher.group(); |
76 | | - replacement = getReplacementValue(argValues, matchSeq); |
77 | | - replacementEntry = replacement.entrySet().iterator().next(); |
78 | | - replacementValue = prepareReplacementValue(mediaType, synCtx, replacementEntry); |
79 | | - matcher.appendReplacement(result, replacementValue); |
| 92 | + if (matcher.group(1) != null) { |
| 93 | + // Handle "${...}" pattern (with quotes) |
| 94 | + String expression = matcher.group(1); |
| 95 | + Object expressionResult = evaluateExpression(expression, synCtx); |
| 96 | + if (expressionResult instanceof JsonPrimitive) { |
| 97 | + replacementValue = prepareJSONPrimitiveReplacementValue(expressionResult, mediaType); |
| 98 | + } else if (expressionResult instanceof JsonElement) { |
| 99 | + // Escape JSON object and Arrays since we need to consider it as |
| 100 | + replacementValue = escapeJson(Matcher.quoteReplacement(gson.toJson(expressionResult))); |
| 101 | + if (XML_TYPE.equals(mediaType)) { |
| 102 | + replacementValue = convertJsonToXML(replacementValue); |
| 103 | + } |
| 104 | + } else { |
| 105 | + replacementValue = expressionResult.toString(); |
| 106 | + if (XML_TYPE.equals(mediaType)) { |
| 107 | + replacementValue = StringEscapeUtils.escapeXml10(replacementValue); |
| 108 | + } else if (JSON_TYPE.equals(mediaType)) { |
| 109 | + if (isXML(replacementValue)) { |
| 110 | + // consider the replacement value as a literal XML |
| 111 | + replacementValue = Matcher.quoteReplacement(replacementValue); |
| 112 | + } else { |
| 113 | + replacementValue = escapeSpecialCharactersOfJson(replacementValue); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + matcher.appendReplacement(result, "\"" + replacementValue + "\""); |
| 118 | + } else if (matcher.group(2) != null) { |
| 119 | + // Handle ${...} pattern (without quotes) |
| 120 | + String expression = matcher.group(2); |
| 121 | + Object expressionResult = evaluateExpression(expression, synCtx); |
| 122 | + replacementValue = expressionResult.toString(); |
| 123 | + if (expressionResult instanceof JsonPrimitive) { |
| 124 | + replacementValue = prepareJSONPrimitiveReplacementValue(expressionResult, mediaType); |
| 125 | + } else if (expressionResult instanceof JsonElement) { |
| 126 | + if (XML_TYPE.equals(mediaType)) { |
| 127 | + replacementValue = convertJsonToXML(replacementValue); |
| 128 | + replacementValue = Matcher.quoteReplacement(replacementValue); |
| 129 | + } else { |
| 130 | + replacementValue = Matcher.quoteReplacement(gson.toJson(expressionResult)); |
| 131 | + } |
| 132 | + } else { |
| 133 | + if (JSON_TYPE.equals(mediaType) && isXML(replacementValue)) { |
| 134 | + replacementValue = convertXMLToJSON(replacementValue); |
| 135 | + } else { |
| 136 | + if (XML_TYPE.equals(mediaType) && !isXML(replacementValue)) { |
| 137 | + replacementValue = StringEscapeUtils.escapeXml10(replacementValue); |
| 138 | + } |
| 139 | + replacementValue = Matcher.quoteReplacement(replacementValue); |
| 140 | + } |
| 141 | + } |
| 142 | + matcher.appendReplacement(result, replacementValue); |
| 143 | + } else if (matcher.group(3) != null) { |
| 144 | + // Handle $n pattern |
| 145 | + String matchSeq = matcher.group(3); |
| 146 | + replacement = getReplacementValue(argValues, matchSeq); |
| 147 | + replacementEntry = replacement.entrySet().iterator().next(); |
| 148 | + replacementValue = prepareReplacementValue(mediaType, synCtx, replacementEntry); |
| 149 | + matcher.appendReplacement(result, replacementValue); |
| 150 | + } |
80 | 151 | } |
81 | 152 | } catch (ArrayIndexOutOfBoundsException e) { |
82 | 153 | log.error("#replace. Mis-match detected between number of formatters and arguments", e); |
| 154 | + } catch (JaxenException e) { |
| 155 | + throw new SynapseException("Error evaluating expression" , e); |
83 | 156 | } |
84 | 157 | matcher.appendTail(result); |
85 | 158 | } |
86 | 159 |
|
| 160 | + private String prepareJSONPrimitiveReplacementValue(Object expressionResult, String mediaType) { |
| 161 | + |
| 162 | + String replacementValue = ((JsonPrimitive) expressionResult).getAsString(); |
| 163 | + replacementValue = escapeSpecialChars(Matcher.quoteReplacement(replacementValue)); |
| 164 | + if (XML_TYPE.equals(mediaType)) { |
| 165 | + replacementValue = StringEscapeUtils.escapeXml10(replacementValue); |
| 166 | + } |
| 167 | + return replacementValue; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Evaluates the expression and returns the result as a string or an object. |
| 172 | + * If the expression contains "xpath(", we meed to evaluate it as a string. |
| 173 | + * |
| 174 | + * @param expression expression to evaluate |
| 175 | + * @param synCtx message context |
| 176 | + * @return evaluated result |
| 177 | + * @throws JaxenException if an error occurs while evaluating the expression |
| 178 | + */ |
| 179 | + private Object evaluateExpression(String expression, MessageContext synCtx) throws JaxenException { |
| 180 | + |
| 181 | + if (expression.contains("xpath(")) { |
| 182 | + return new SynapseExpression(expression).stringValueOf(synCtx); |
| 183 | + } |
| 184 | + return new SynapseExpression(expression).objectValueOf(synCtx); |
| 185 | + } |
| 186 | + |
| 187 | + private String escapeJson(String value) { |
| 188 | + // Manual escape for JSON: escaping double quotes and backslashes |
| 189 | + return value.replace("\"", "\\\"").replace("\\", "\\\\"); |
| 190 | + } |
| 191 | + |
| 192 | + private String convertJsonToXML(String replacementValue) { |
| 193 | + |
| 194 | + try { |
| 195 | +// replacementValue = Matcher.quoteReplacement(replacementValue); |
| 196 | + OMElement omXML = JsonUtil.toXml(IOUtils.toInputStream(replacementValue), false); |
| 197 | + if (JsonUtil.isAJsonPayloadElement(omXML)) { // remove <jsonObject/> from result. |
| 198 | + Iterator children = omXML.getChildElements(); |
| 199 | + String childrenStr = ""; |
| 200 | + while (children.hasNext()) { |
| 201 | + childrenStr += (children.next()).toString().trim(); |
| 202 | + } |
| 203 | + replacementValue = childrenStr; |
| 204 | + } else { |
| 205 | + replacementValue = omXML.toString(); |
| 206 | + } |
| 207 | + } catch (AxisFault e) { |
| 208 | + handleException( |
| 209 | + "Error converting JSON to XML, please check your expressions return valid JSON: "); |
| 210 | + } |
| 211 | + return escapeSpecialCharactersOfXml(replacementValue); |
| 212 | + } |
| 213 | + |
| 214 | + private String convertXMLToJSON(String replacementValue) { |
| 215 | + |
| 216 | + try { |
| 217 | + replacementValue = "<jsonObject>" + replacementValue + "</jsonObject>"; |
| 218 | + OMElement omXML = convertStringToOM(replacementValue); |
| 219 | + replacementValue = JsonUtil.toJsonString(omXML).toString(); |
| 220 | + replacementValue = escapeSpecialCharactersOfJson(replacementValue); |
| 221 | + } catch (XMLStreamException e) { |
| 222 | + handleException( |
| 223 | + "Error parsing XML for JSON conversion, please check your expressions return valid XML: "); |
| 224 | + } catch (AxisFault e) { |
| 225 | + handleException("Error converting XML to JSON"); |
| 226 | + } catch (OMException e) { |
| 227 | + // if the logic comes to this means, it was tried as a XML, which means it has |
| 228 | + // "<" as starting element and ">" as end element, so basically if the logic comes here, that means |
| 229 | + // value is a string value, that means No conversion required, as path evaluates to regular String. |
| 230 | + replacementValue = escapeSpecialChars(replacementValue); |
| 231 | + |
| 232 | + } |
| 233 | + return replacementValue; |
| 234 | + } |
| 235 | + |
87 | 236 | private HashMap<String, ArgumentDetails> getReplacementValue(HashMap<String, ArgumentDetails>[] argValues, |
88 | 237 | String matchSeq) { |
89 | 238 |
|
90 | 239 | HashMap<String, ArgumentDetails> replacement; |
91 | 240 | int argIndex; |
92 | 241 | try { |
93 | | - argIndex = Integer.parseInt(matchSeq.substring(1)); |
| 242 | + argIndex = Integer.parseInt(matchSeq); |
94 | 243 | } catch (NumberFormatException e) { |
95 | 244 | argIndex = Integer.parseInt(matchSeq.substring(2, matchSeq.length() - 1)); |
96 | 245 | } |
|
0 commit comments