Skip to content

Commit

Permalink
#21529 added parameter to json viewtool to post parameters as a json …
Browse files Browse the repository at this point in the history
…payload (#25832)

Co-authored-by: erickgonzalez <erick.gonzalez@dotcms.com>
  • Loading branch information
dsolistorres and erickgonzalez committed Aug 23, 2023
1 parent 7519573 commit fbc0845
Show file tree
Hide file tree
Showing 2 changed files with 403 additions and 18 deletions.
@@ -1,11 +1,15 @@
package com.dotcms.rendering.velocity.viewtools;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import com.dotcms.http.CircuitBreakerUrlBuilder;
import com.dotcms.rest.api.v1.DotObjectMapperProvider;
import com.google.common.annotations.VisibleForTesting;
import org.apache.velocity.tools.view.ImportSupport;
import org.apache.velocity.tools.view.tools.ViewTool;

Expand All @@ -18,6 +22,19 @@

public class JSONTool extends ImportSupport implements ViewTool {

private final Supplier<CircuitBreakerUrlBuilder> circuitBreakerUrlSupplier;

public JSONTool() {
super();
this.circuitBreakerUrlSupplier = CircuitBreakerUrl::builder;
}

@VisibleForTesting
JSONTool(final Supplier<CircuitBreakerUrlBuilder> circuitBreakerUrlSupplier) {
super();
this.circuitBreakerUrlSupplier = circuitBreakerUrlSupplier;
}

public void init(Object obj) {

}
Expand Down Expand Up @@ -102,8 +119,7 @@ public Object get(final String url, final int timeout) {

public Object get(String url, int timeout, Map<String, String> headers) {
try {
String x = CircuitBreakerUrl
.builder()
String x = this.circuitBreakerUrlSupplier.get()
.setHeaders(headers)
.setUrl(url)
.setTimeout(timeout)
Expand All @@ -117,11 +133,18 @@ public Object get(String url, int timeout, Map<String, String> headers) {
return null;
}

/**
* Will put data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to put to
* @param timeout The timeout in milliseconds
* @param headers The headers to send in the HTTP request
* @param rawData The raw data to send in the request
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object put(final String url, final int timeout, final Map<String, String> headers,
final String rawData) {
try {
final String response = CircuitBreakerUrl
.builder()
final String response = this.circuitBreakerUrlSupplier.get()
.setMethod(Method.PUT)
.setHeaders(headers)
.setUrl(url)
Expand All @@ -137,20 +160,33 @@ public Object put(final String url, final int timeout, final Map<String, String>
return null;
}

/**
* Will put data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to put to
* @param headers The headers to send in the HTTP request
* @param rawData The raw data to send in the request
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object put(final String url, final Map<String, String> headers, final String rawData) {
return put(url, Config.getIntProperty("URL_CONNECTION_TIMEOUT", 2000), headers, rawData);
}


/**
* Will put data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to put to
* @param timeout The timeout in milliseconds
* @param headers The headers to send in the HTTP request
* @param params The parameters to send in the request
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object put(final String url, final int timeout, final Map<String, String> headers,
final Map<String, String> params) {
final Map<String, Object> params) {
try {
final String response = CircuitBreakerUrl
.builder()
final String response = this.circuitBreakerUrlSupplier.get()
.setMethod(Method.PUT)
.setHeaders(headers)
.setUrl(url)
.setParams(params)
.setParams(convertObjToStringParameters(params))
.setTimeout(timeout)
.build()
.doString();
Expand All @@ -162,16 +198,64 @@ public Object put(final String url, final int timeout, final Map<String, String>
return null;
}

/**
* Will put data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to put to
* @param headers The headers to send in the HTTP request
* @param params The parameters to send in the request
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object put(final String url, final Map<String, String> headers,
final Map<String, Object> params) {
return put(url, Config.getIntProperty("URL_CONNECTION_TIMEOUT", 2000), headers, params);
}

/**
* Will put data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to put to
* @param timeout The timeout in milliseconds
* @param headers The headers to send in the HTTP request
* @param params The parameters to send in the request
* @param useParamsAsJsonPayload If true, the params will be sent as a JSON payload,
* otherwise they will be sent as request parameters
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object put(final String url, final int timeout, final Map<String, String> headers,
final Map<String, Object> params, final boolean useParamsAsJsonPayload) {
if (useParamsAsJsonPayload) {
return put(url, timeout, headers, generate(params).toString());
} else {
return put(url, timeout, headers, params);
}
}

/**
* Will put data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to put to
* @param headers The headers to send in the HTTP request
* @param params The parameters to send in the request
* @param useParamsAsJsonPayload If true, the params will be sent as a JSON payload,
* otherwise they will be sent as request parameters
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object put(final String url, final Map<String, String> headers,
final Map<String, String> params) {
return post(url, Config.getIntProperty("URL_CONNECTION_TIMEOUT", 2000), headers, params);
final Map<String, Object> params, final boolean useParamsAsJsonPayload) {
return put(url, Config.getIntProperty("URL_CONNECTION_TIMEOUT", 2000),
headers, params, useParamsAsJsonPayload);
}

/**
* Will post data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to post to
* @param timeout The timeout in milliseconds
* @param headers The headers to send in the HTTP request
* @param rawData The raw data to send in the request
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object post(final String url, final int timeout, final Map<String, String> headers,
final String rawData) {
try {
final String response = CircuitBreakerUrl
.builder()
final String response = this.circuitBreakerUrlSupplier.get()
.setMethod(Method.POST)
.setHeaders(headers)
.setUrl(url)
Expand All @@ -187,19 +271,33 @@ public Object post(final String url, final int timeout, final Map<String, String
return null;
}

/**
* Will post data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to post to
* @param headers The headers to send in the HTTP request
* @param rawData The raw data to send in the request
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object post(final String url, final Map<String, String> headers, final String rawData) {
return post(url, Config.getIntProperty("URL_CONNECTION_TIMEOUT", 5000), headers, rawData);
}

/**
* Will post data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to post to
* @param timeout The timeout in milliseconds
* @param headers The headers to send in the HTTP request
* @param params The parameters to send in the request
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object post(final String url, final int timeout, final Map<String, String> headers,
final Map<String, String> params) {
final Map<String, Object> params) {
try {
final String response = CircuitBreakerUrl
.builder()
final String response = this.circuitBreakerUrlSupplier.get()
.setMethod(Method.POST)
.setHeaders(headers)
.setUrl(url)
.setParams(params)
.setParams(convertObjToStringParameters(params))
.setTimeout(timeout)
.build()
.doString();
Expand All @@ -211,11 +309,75 @@ public Object post(final String url, final int timeout, final Map<String, String
return null;
}

/**
* Will post data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to post to
* @param headers The headers to send in the HTTP request
* @param params The parameters to send in the request
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object post(final String url, final Map<String, String> headers,
final Map<String, String> params) {
final Map<String, Object> params) {
return post(url, Config.getIntProperty("URL_CONNECTION_TIMEOUT", 2000), headers, params);
}

/**
* Will post data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to post to
* @param timeout The timeout in milliseconds
* @param headers The headers to send in the HTTP request
* @param params The parameters to send in the request
* @param useParamsAsJsonPayload If true, the params will be sent as a JSON payload,
* otherwise they will be sent as request parameters
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object post(final String url, final int timeout, final Map<String, String> headers,
final Map<String, Object> params, final boolean useParamsAsJsonPayload) {
if (useParamsAsJsonPayload) {
return post(url, timeout, headers, generate(params).toString());
} else {
return post(url, timeout, headers, params);
}
}

/**
* Will post data to the remote URL returning the JSON Object or JSON Array response from the server
* @param url The URL to post to
* @param headers The headers to send in the HTTP request
* @param params The parameters to send in the request
* @param useParamsAsJsonPayload If true, the params will be sent as a JSON payload,
* otherwise they will be sent as request parameters
* @return The JSON Object or JSON Array returned from the remote URL
*/
public Object post(final String url, final Map<String, String> headers,
final Map<String, Object> params, final boolean useParamsAsJsonPayload) {
return post(url, Config.getIntProperty("URL_CONNECTION_TIMEOUT", 2000),
headers, params, useParamsAsJsonPayload);
}

/**
* Converts the given map of objects to a map of strings to be used as parameters in a request
* @param objParams The map of objects to convert
* @return The map of strings to be used as parameters in a request
*/
private Map<String, String> convertObjToStringParameters(Map<String, Object> objParams) {
Map<String, String> params = new HashMap<>();
for (Map.Entry<String, Object> entry : objParams.entrySet()) {
String value;
if (entry.getValue() == null) {
value = "";
} else if (entry.getValue() instanceof Map || entry.getValue() instanceof List) {
value = generate(entry.getValue()).toString();
} else if (entry.getValue() instanceof String) {
value = (String) entry.getValue();
} else {
value = entry.getValue().toString();
}
params.put(entry.getKey(), value);
}
return params;
}

/**
* Returns a JSONObject from a passed in Map
*
Expand Down

0 comments on commit fbc0845

Please sign in to comment.