Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update dependencies, use httpclient5 #14

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base64/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
compileOnly("org.jetbrains:annotations:24.1.0")
}
Expand Down
4 changes: 2 additions & 2 deletions net/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ repositories {
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation("org.apache.httpcomponents:httpclient:4.5.14")
implementation("org.apache.httpcomponents.client5:httpclient5:5.4")
implementation("com.google.code.gson:gson:2.11.0")
compileOnly("org.jetbrains:annotations:24.1.0")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.pega.launchpad.net;

import com.google.gson.Gson;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.hc.client5.http.classic.methods.*;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.entity.EntityBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.util.Timeout;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -27,15 +27,14 @@ public class HttpRequestWithMappedResponseHeaders {
* @throws IOException error with http request
*/
public static Response send(Map<String, String> inputMap) throws IOException {
Response CFresponse = new Response();

// Default URL is a test URL, intended to be used with GET
String url = inputMap.get("url");

if (url == null) throw new IllegalArgumentException("url must be specified");

// Default method is GET
HttpRequestBase request;
HttpUriRequestBase request;
switch (inputMap.getOrDefault("method", "").strip().toLowerCase()) {
case "post":
request = new HttpPost(url);
Expand All @@ -56,7 +55,8 @@ public static Response send(Map<String, String> inputMap) throws IOException {

// Set request timeout
String timeout = inputMap.getOrDefault("connectionTimeout", "30000");
request.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setConnectTimeout(Integer.parseInt(timeout)).build());
request.
setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setConnectionRequestTimeout(Timeout.ofMilliseconds(Integer.parseInt(timeout))).build());

// Set request headers
String reqHeaders = inputMap.getOrDefault("headers", "");
Expand All @@ -69,53 +69,52 @@ public static Response send(Map<String, String> inputMap) throws IOException {
}

// Set request body - only necessary for POST/PUT/PATCH, if provided
String reqBody = inputMap.getOrDefault("body", "");
if (!reqBody.isEmpty()) {
if (request instanceof HttpPost) {
((HttpPost) request).setEntity(new StringEntity(reqBody));
} else if (request instanceof HttpPut) {
((HttpPut) request).setEntity(new StringEntity(reqBody));
} else if (request instanceof HttpPatch) {
((HttpPatch) request).setEntity(new StringEntity(reqBody));
if (!(request instanceof HttpGet)) {
String reqBody = inputMap.getOrDefault("body", "");
if (!reqBody.isEmpty()) {
HttpEntity entity = EntityBuilder.create().setText(reqBody).build();
request.setEntity(entity);
}
}

// Execute the request
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try (CloseableHttpResponse response1 = httpClient.execute(request)) {
HttpEntity entity1 = response1.getEntity();

// Get the response status

CFresponse.responseStatus = new HashMap<>();
CFresponse.responseStatus.put("statusCode", Integer.toString(response1.getStatusLine().getStatusCode()));
CFresponse.responseStatus.put("reason", response1.getStatusLine().getReasonPhrase());
CFresponse.responseStatus.put("protocolName", response1.getStatusLine().getProtocolVersion().getProtocol());
CFresponse.responseStatus.put("protocolMajorVersion", Integer.toString(response1.getStatusLine().getProtocolVersion().getMajor()));
CFresponse.responseStatus.put("protocolMinorVersion", Integer.toString(response1.getStatusLine().getProtocolVersion().getMajor()));

// Get response headers out of response, then construct a map out of them
Header[] allHeaders = response1.getAllHeaders();
CFresponse.responseHeaders = new HashMap<>();
for (Header header : allHeaders) {
CFresponse.responseHeaders.put(header.getName(), header.getValue());
}

byte[] responseBody = entity1.getContent().readAllBytes();
if (responseBody == null || responseBody.length == 0) responseBody = "{}".getBytes();

String responseBodyString = new String(responseBody);
if (responseBodyString.strip().startsWith("[")) {
CFresponse.responseBody = new Gson().fromJson(responseBodyString, List.class);
} else {
CFresponse.responseBody = new Gson().fromJson(responseBodyString, Map.class);
return httpClient.execute(request, response -> {
Response localResponse = new Response();

try (HttpEntity entity1 = response.getEntity()) {
// Get the response status

localResponse.responseStatus = new HashMap<>();
localResponse.responseStatus.put("statusCode", Integer.toString(response.getCode()));
localResponse.responseStatus.put("reason", response.getReasonPhrase());
localResponse.responseStatus.put("protocolName", response.getVersion().getProtocol());
localResponse.responseStatus.put("protocolMajorVersion", Integer.toString(response.getVersion().getMajor()));
localResponse.responseStatus.put("protocolMinorVersion", Integer.toString(response.getVersion().getMinor()));

// Get response headers out of response, then construct a map out of them
Header[] allHeaders = response.getHeaders();
localResponse.responseHeaders = new HashMap<>();
for (Header header : allHeaders) {
localResponse.responseHeaders.put(header.getName(), header.getValue());
}

byte[] responseBody = entity1.getContent().readAllBytes();
if (responseBody == null || responseBody.length == 0) {
localResponse.responseBody = new Object();
} else {
String responseBodyString = new String(responseBody).strip();
if (responseBodyString.startsWith("[")) {
localResponse.responseBody = new Gson().fromJson(responseBodyString, List.class);
} else if (responseBodyString.startsWith("{")){
localResponse.responseBody = new Gson().fromJson(responseBodyString, Map.class);
} else {
localResponse.responseBody = new Object();
}
}
}


EntityUtils.consume(entity1);
}
return localResponse;
});
}

return CFresponse;
}
}
4 changes: 2 additions & 2 deletions parser/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ repositories {
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation("org.apache.commons:commons-csv:1.11.0")
implementation("org.apache.commons:commons-csv:1.12.0")
implementation("com.google.code.gson:gson:2.11.0")
compileOnly("org.jetbrains:annotations:24.1.0")
}
Expand Down
4 changes: 2 additions & 2 deletions pdf/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ repositories {
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
implementation("com.google.code.gson:gson:2.11.0")
implementation("org.apache.pdfbox:pdfbox:3.0.2")
implementation("org.apache.pdfbox:pdfbox:3.0.3")
implementation("com.amazonaws:aws-lambda-java-core:1.2.3")
compileOnly("org.jetbrains:annotations:24.1.0")
}
Expand Down
2 changes: 1 addition & 1 deletion text/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
}

dependencies {
testImplementation(platform("org.junit:junit-bom:5.10.2"))
testImplementation(platform("org.junit:junit-bom:5.11.3"))
testImplementation("org.junit.jupiter:junit-jupiter")
compileOnly("org.jetbrains:annotations:24.1.0")
}
Expand Down
Loading