Skip to content

Commit

Permalink
Fully implement request proxying with JSON objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Straut committed Aug 8, 2018
1 parent f2e127e commit 685ee7b
Show file tree
Hide file tree
Showing 4 changed files with 1,571 additions and 1,583 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
Expand Down Expand Up @@ -333,8 +334,10 @@ private HttpRequestBase getRequestBase(HttpMethod method, String endpoint) {
return new HttpGet(endpoint);
} else if (method.name().equals(HttpMethod.POST.name())) {
return new HttpPost(endpoint);
} else if (method.name().equals(HttpMethod.PUT.name())) {
return new HttpPut(endpoint);
} else {
throw new IllegalArgumentException(Messages.ONLY_GET_POST_METHODS_SUPPORTED);
throw new IllegalArgumentException(Messages.ONLY_GET_POST_PUT_METHODS_SUPPORTED);
}
}

Expand Down Expand Up @@ -372,7 +375,7 @@ private boolean validate(JsonObject object) {
}

if (method != null && !SUPPORTED_HTTP_METHODS.contains(method)) {
this.errorLog.addValidationError(Messages.ONLY_GET_POST_METHODS_SUPPORTED);
this.errorLog.addValidationError(Messages.ONLY_GET_POST_PUT_METHODS_SUPPORTED);
valid = false;
}
}
Expand Down Expand Up @@ -470,7 +473,6 @@ private String parseRequestBody(JsonElement requestBody, String type) throws Uns
}

} else if (requestBody.isJsonObject()) {

if(type.toLowerCase().trim().equals(HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED.toString().toLowerCase().trim())) {
for (Map.Entry<String, JsonElement> requestParameter : requestBody.getAsJsonObject().entrySet()) {

Expand All @@ -482,7 +484,11 @@ private String parseRequestBody(JsonElement requestBody, String type) throws Uns
}
}
} else if(type.toLowerCase().trim().equals(HttpHeaderValues.APPLICATION_JSON.toString().toLowerCase().trim())) {
builder.append(requestBody.getAsJsonObject());
JsonObject object = new JsonObject();
for (Map.Entry<String, JsonElement> requestParameter : requestBody.getAsJsonObject().entrySet()) {
object.add(requestParameter.getKey(), requestParameter.getValue());
}
builder.append(object);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

public class Messages {

public static final String UNSUPPORTED_HTTP_METHOD = "HTTP Method is not supported. Supported HTTP Methods are: %s";
public static final String UNSUPPORTED_HTTP_METHOD = "HTTP Method is not supported. Supported HTTP Methods are: %s, %s";
public static final String ENDPOINT_URL_MUST_BE_PROVIDED = "Endpoint URL must be provided";
public static final String ENDPOINT_URL_MUST_BE_VALID = "Endpoint URL must be a valid URL";
public static final String HTTP_METHOD_MUST_BE_SPECIFIED = "An HTTP Method must be specified";
Expand All @@ -15,5 +15,5 @@ public class Messages {
public static final String STATUS_CANNOT_BE_NULL_ERROR = "ProxyResponse status cannot be null";
public static final String HEADER_CANNOT_BE_NULL_ERROR = "ProxyResponse header name or header value cannot be null";
public static final String HEADER_CANNOT_BE_EMPTY_ERROR = "ProxyResponse header name or header value cannot be empty string";
public static final String ONLY_GET_POST_METHODS_SUPPORTED = "Only GET and POST methods are supported";
public static final String ONLY_GET_POST_PUT_METHODS_SUPPORTED = "Only GET, POST and PUT methods are supported";
}
Loading

0 comments on commit 685ee7b

Please sign in to comment.