Skip to content

Commit

Permalink
fix: use ExecutionContext instead of System.getEnv
Browse files Browse the repository at this point in the history
related to apim-1614
  • Loading branch information
leleueri committed May 17, 2023
1 parent df1dbd3 commit 9286cd3
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 54 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ You can configure the policy with the following options:

=== Nested objects

To limit the processing time in case of nested object, a default max depth of nested object has been defined to 1000. This default value can be overriden using the environment variable `gravitee_policy_json_xml_maxdepth`.
To limit the processing time in case of nested object, a default max depth of nested object has been defined to 1000. This default value can be overriden using the environment variable `gravitee_policy_jsonxml_maxdepth`.
44 changes: 34 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@
<parent>
<groupId>io.gravitee</groupId>
<artifactId>gravitee-parent</artifactId>
<version>17.2</version>
<version>21.0.0</version>
</parent>

<properties>
<gravitee-gateway-buffer.version>3.10.0</gravitee-gateway-buffer.version>
<gravitee-bom.version>1.4</gravitee-bom.version>
<gravitee-gateway-api.version>1.31.0</gravitee-gateway-api.version>
<gravitee-policy-api.version>1.10.0</gravitee-policy-api.version>
<gravitee-common.version>1.20.0</gravitee-common.version>
<gravitee-gateway-buffer.version>3.18.0</gravitee-gateway-buffer.version>
<gravitee-bom.version>4.0.0</gravitee-bom.version>
<gravitee-node.version>1.24.8</gravitee-node.version>
<gravitee-gateway-api.version>1.34.2</gravitee-gateway-api.version>
<gravitee-policy-api.version>1.11.0</gravitee-policy-api.version>
<gravitee-common.version>1.27.0</gravitee-common.version>
<gravitee-expression-language.version>1.9.7</gravitee-expression-language.version>

<slf4j.version>1.7.26</slf4j.version>
<json-schema-generator-maven-plugin.version>1.1.0</json-schema-generator-maven-plugin.version>
<json-schema-generator-maven-plugin.outputDirectory>${project.build.directory}/schemas</json-schema-generator-maven-plugin.outputDirectory>
<maven-assembly-plugin.version>2.6</maven-assembly-plugin.version>
Expand All @@ -55,6 +59,13 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node</artifactId>
<version>${gravitee-node.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -81,6 +92,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.gravitee.node</groupId>
<artifactId>gravitee-node-api</artifactId>
<version>${gravitee-node.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -90,11 +108,17 @@

<!-- Test scope -->
<dependency>
<groupId>io.gravitee.gateway</groupId>
<artifactId>gravitee-gateway-buffer</artifactId>
<groupId>io.gravitee.apim.gateway</groupId>
<artifactId>gravitee-apim-gateway-buffer</artifactId>
<version>${gravitee-gateway-buffer.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.gravitee.el</groupId>
<artifactId>gravitee-expression-language</artifactId>
<version>${gravitee-expression-language.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down Expand Up @@ -184,10 +208,10 @@
<plugin>
<groupId>com.hubspot.maven.plugins</groupId>
<artifactId>prettier-maven-plugin</artifactId>
<version>0.12</version>
<version>0.17</version>
<configuration>
<nodeVersion>12.13.0</nodeVersion>
<prettierJavaVersion>1.0.1</prettierJavaVersion>
<prettierJavaVersion>1.6.1</prettierJavaVersion>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@
package io.gravitee.policy.json2xml;

import io.gravitee.common.http.MediaType;
import io.gravitee.gateway.api.ExecutionContext;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.api.buffer.Buffer;
import io.gravitee.gateway.api.http.stream.TransformableRequestStreamBuilder;
import io.gravitee.gateway.api.http.stream.TransformableResponseStreamBuilder;
import io.gravitee.gateway.api.stream.ReadWriteStream;
import io.gravitee.gateway.api.stream.exception.TransformationException;
import io.gravitee.node.api.configuration.Configuration;
import io.gravitee.policy.api.PolicyChain;
import io.gravitee.policy.api.annotations.OnRequestContent;
import io.gravitee.policy.api.annotations.OnResponseContent;
import io.gravitee.policy.json2xml.configuration.JsonToXmlTransformationPolicyConfiguration;
import io.gravitee.policy.json2xml.configuration.PolicyScope;
import io.gravitee.policy.json2xml.transformer.JSONObject;
import io.gravitee.policy.json2xml.transformer.JSONTokener;
import io.gravitee.policy.json2xml.transformer.XML;
import io.gravitee.policy.json2xml.utils.CharsetHelper;
import java.nio.charset.Charset;
Expand All @@ -40,6 +43,7 @@
*/
public class JsonToXmlTransformationPolicy {

public static final String POLICY_JSON_XML_MAXDEPTH = "policy.json-xml.maxdepth";
private static final String UTF8_CHARSET_NAME = "UTF-8";
static final String CONTENT_TYPE = MediaType.APPLICATION_XML + ";charset=" + UTF8_CHARSET_NAME;

Expand All @@ -48,41 +52,61 @@ public class JsonToXmlTransformationPolicy {
*/
private final JsonToXmlTransformationPolicyConfiguration configuration;

private Integer maxDepth;

public JsonToXmlTransformationPolicy(final JsonToXmlTransformationPolicyConfiguration configuration) {
this.configuration = configuration;
}

@OnResponseContent
public ReadWriteStream onResponseContent(Response response, PolicyChain chain) {
public ReadWriteStream onResponseContent(Response response, PolicyChain chain, ExecutionContext ctx) {
if (configuration.getScope() == null || configuration.getScope() == PolicyScope.RESPONSE) {
Charset charset = CharsetHelper.extractCharset(response.headers());

return TransformableResponseStreamBuilder.on(response).chain(chain).contentType(CONTENT_TYPE).transform(map(charset)).build();
return TransformableResponseStreamBuilder
.on(response)
.chain(chain)
.contentType(CONTENT_TYPE)
.transform(map(charset, getMaxDepth(ctx)))
.build();
}
return null;
}

@OnRequestContent
public ReadWriteStream onRequestContent(Request request, PolicyChain chain) {
public ReadWriteStream onRequestContent(Request request, PolicyChain chain, ExecutionContext ctx) {
if (configuration.getScope() == PolicyScope.REQUEST) {
Charset charset = CharsetHelper.extractCharset(request.headers());

return TransformableRequestStreamBuilder.on(request).chain(chain).contentType(CONTENT_TYPE).transform(map(charset)).build();
return TransformableRequestStreamBuilder
.on(request)
.chain(chain)
.contentType(CONTENT_TYPE)
.transform(map(charset, getMaxDepth(ctx)))
.build();
}
return null;
}

private Function<Buffer, Buffer> map(Charset charset) {
private Function<Buffer, Buffer> map(Charset charset, int maxDepth) {
return input -> {
try {
String encodedPayload = new String(input.toString(charset).getBytes(UTF8_CHARSET_NAME));
JSONObject jsonPayload = new JSONObject(encodedPayload);
JSONObject jsonPayload = new JSONObject(encodedPayload, maxDepth);
JSONObject jsonPayloadWithRoot = new JSONObject();
jsonPayloadWithRoot.append(this.configuration.getRootElement(), jsonPayload);
return Buffer.buffer(XML.toString(jsonPayloadWithRoot));
return Buffer.buffer(XML.toString(jsonPayloadWithRoot), UTF8_CHARSET_NAME);
} catch (Exception ex) {
throw new TransformationException("Unable to transform JSON into XML: " + ex.getMessage(), ex);
}
};
}

protected int getMaxDepth(ExecutionContext ctx) {
if (this.maxDepth == null) {
this.maxDepth =
ctx.getComponent(Configuration.class).getProperty(POLICY_JSON_XML_MAXDEPTH, Integer.class, JSONTokener.DEFAULT_MAX_DEPTH);
}
return this.maxDepth;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public static String rowToString(JSONArray ja) {
* @throws JSONException
*/
public static JSONArray toJSONArray(String string) throws JSONException {
return toJSONArray(new JSONTokener(string));
return toJSONArray(new JSONTokener(string, JSONTokener.DEFAULT_MAX_DEPTH));
}

/**
Expand All @@ -221,7 +221,7 @@ public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
* @throws JSONException
*/
public static JSONArray toJSONArray(JSONArray names, String string) throws JSONException {
return toJSONArray(names, new JSONTokener(string));
return toJSONArray(names, new JSONTokener(string, JSONTokener.DEFAULT_MAX_DEPTH));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static JSONObject toJSONObject(String string) throws JSONException {
String name;
JSONObject jo = new JSONObject();
Object value;
JSONTokener x = new JSONTokener(string);
JSONTokener x = new JSONTokener(string, JSONTokener.DEFAULT_MAX_DEPTH);
jo.put("name", x.nextTo('='));
x.next('=');
jo.put("value", x.nextTo(';'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class CookieList {
*/
public static JSONObject toJSONObject(String string) throws JSONException {
JSONObject jo = new JSONObject();
JSONTokener x = new JSONTokener(string);
JSONTokener x = new JSONTokener(string, JSONTokener.DEFAULT_MAX_DEPTH);
while (x.more()) {
String name = Cookie.unescape(x.nextTo('='));
x.next('=');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class HTTPTokener extends JSONTokener {
* @param string A source string.
*/
public HTTPTokener(String string) {
super(string);
super(string, DEFAULT_MAX_DEPTH);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public JSONArray(JSONTokener x) throws JSONException {
* If there is a syntax error.
*/
public JSONArray(String source) throws JSONException {
this(new JSONTokener(source));
this(new JSONTokener(source, JSONTokener.DEFAULT_MAX_DEPTH));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ public JSONObject(Object object, String names[]) {
* If there is a syntax error in the source string or a
* duplicated key.
*/
public JSONObject(String source) throws JSONException {
this(new JSONTokener(source));
public JSONObject(String source, int maxDepth) throws JSONException {
this(new JSONTokener(source, maxDepth));
}

/**
Expand Down Expand Up @@ -1198,7 +1198,7 @@ public JSONObject put(String key, Collection<?> value) throws JSONException {
* If the key is null or if the number is invalid.
*/
public JSONObject put(String key, double value) throws JSONException {
this.put(key, new Double(value));
this.put(key, Double.valueOf(value));
return this;
}

Expand All @@ -1214,7 +1214,7 @@ public JSONObject put(String key, double value) throws JSONException {
* If the key is null.
*/
public JSONObject put(String key, int value) throws JSONException {
this.put(key, new Integer(value));
this.put(key, Integer.valueOf(value));
return this;
}

Expand All @@ -1230,7 +1230,7 @@ public JSONObject put(String key, int value) throws JSONException {
* If the key is null.
*/
public JSONObject put(String key, long value) throws JSONException {
this.put(key, new Long(value));
this.put(key, Long.valueOf(value));
return this;
}

Expand Down Expand Up @@ -1487,7 +1487,7 @@ public static Object stringToValue(String string) {
return d;
}
} else {
Long myLong = new Long(string);
Long myLong = Long.parseLong(string);
if (string.equals(myLong.toString())) {
if (myLong.longValue() == myLong.intValue()) {
return Integer.valueOf(myLong.intValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ of this software and associated documentation files (the "Software"), to deal
public class JSONTokener {

public static final int DEFAULT_MAX_DEPTH = 1000;
public static final String GRAVITEE_POLICY_JSON_XML_MAXDEPTH = "gravitee_policy_json_xml_maxdepth";
private long character;
private boolean eof;
private long index;
Expand All @@ -77,32 +76,32 @@ public class JSONTokener {
*
* @param reader A reader.
*/
public JSONTokener(Reader reader) {
public JSONTokener(Reader reader, int maxDepth) {
this.reader = reader.markSupported() ? reader : new BufferedReader(reader);
this.eof = false;
this.usePrevious = false;
this.previous = 0;
this.index = 0;
this.character = 1;
this.line = 1;
this.maxDepth = ofNullable(getenv(GRAVITEE_POLICY_JSON_XML_MAXDEPTH)).map(Integer::parseInt).orElse(DEFAULT_MAX_DEPTH);
this.maxDepth = maxDepth;
}

/**
* Construct a JSONTokener from an InputStream.
* @param inputStream The source.
*/
public JSONTokener(InputStream inputStream) throws JSONException {
this(new InputStreamReader(inputStream));
public JSONTokener(InputStream inputStream, int maxDepth) throws JSONException {
this(new InputStreamReader(inputStream), maxDepth);
}

/**
* Construct a JSONTokener from a string.
*
* @param s A source string.
*/
public JSONTokener(String s) {
this(new StringReader(s));
public JSONTokener(String s, int maxDepth) {
this(new StringReader(s), maxDepth);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class XMLTokener extends JSONTokener {
* @param s A source string.
*/
public XMLTokener(String s) {
super(s);
super(s, DEFAULT_MAX_DEPTH);
}

/**
Expand Down
Loading

0 comments on commit 9286cd3

Please sign in to comment.