You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2023/03/17 11:40:28 UTC
[camel] 10/19: CAMEL-18995: camel-graphql - Upgrade to HttpComponents 5.x
This is an automated email from the ASF dual-hosted git repository.
nfilotto pushed a commit to branch CAMEL-18995/upgrade-httpcomponents-5
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 686858c5aeee1263d1fa76c60d101ae91dcaf385
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Fri Mar 17 12:28:35 2023 +0100
CAMEL-18995: camel-graphql - Upgrade to HttpComponents 5.x
---
components/camel-graphql/pom.xml | 6 +-
.../camel/component/graphql/GraphqlEndpoint.java | 31 +++++-----
.../camel/component/graphql/GraphqlProducer.java | 16 ++---
.../component/graphql/server/GraphqlServer.java | 72 +++++++++++-----------
4 files changed, 63 insertions(+), 62 deletions(-)
diff --git a/components/camel-graphql/pom.xml b/components/camel-graphql/pom.xml
index a19af884ff1..5bee1fd5803 100644
--- a/components/camel-graphql/pom.xml
+++ b/components/camel-graphql/pom.xml
@@ -43,9 +43,9 @@
</dependency>
<dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpclient</artifactId>
- <version>${httpclient4-version}</version>
+ <groupId>org.apache.httpcomponents.client5</groupId>
+ <artifactId>httpclient5</artifactId>
+ <version>${httpclient-version}</version>
</dependency>
<!-- for testing -->
diff --git a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
index cedf3b409e9..80869870fc8 100644
--- a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
+++ b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlEndpoint.java
@@ -34,17 +34,16 @@ import org.apache.camel.support.DefaultEndpoint;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.json.JsonObject;
-import org.apache.http.HttpHeaders;
-import org.apache.http.HttpHost;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.CredentialsProvider;
-import org.apache.http.client.utils.HttpClientUtils;
-import org.apache.http.impl.client.BasicCredentialsProvider;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.impl.client.HttpClients;
-import org.apache.http.message.BasicHeader;
+import org.apache.hc.client5.http.auth.AuthScope;
+import org.apache.hc.client5.http.auth.CredentialsStore;
+import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
+import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
+import org.apache.hc.client5.http.impl.classic.HttpClients;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.message.BasicHeader;
/**
* Send GraphQL queries and mutations to external systems.
@@ -87,7 +86,9 @@ public class GraphqlEndpoint extends DefaultEndpoint {
@Override
protected void doStop() throws Exception {
- HttpClientUtils.closeQuietly(this.httpClient);
+ if (httpClient != null) {
+ httpClient.close();
+ }
}
@Override
@@ -124,8 +125,10 @@ public class GraphqlEndpoint extends DefaultEndpoint {
Arrays.asList(new BasicHeader(HttpHeaders.AUTHORIZATION, authType + " " + accessToken)));
}
if (username != null && password != null) {
- CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
- credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
+ CredentialsStore credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(
+ new AuthScope(null, -1),
+ new UsernamePasswordCredentials(username, password.toCharArray()));
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
return httpClientBuilder.build();
diff --git a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlProducer.java b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlProducer.java
index 9478bab248c..7bbfab7ec45 100644
--- a/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlProducer.java
+++ b/components/camel-graphql/src/main/java/org/apache/camel/component/graphql/GraphqlProducer.java
@@ -23,13 +23,13 @@ import org.apache.camel.Exchange;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.support.DefaultAsyncProducer;
import org.apache.camel.util.json.JsonObject;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpHeaders;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.ContentType;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.util.EntityUtils;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpHeaders;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
public class GraphqlProducer extends DefaultAsyncProducer {
@@ -49,7 +49,7 @@ public class GraphqlProducer extends DefaultAsyncProducer {
URI httpUri = getEndpoint().getHttpUri();
String requestBody = buildRequestBody(getQuery(exchange), getEndpoint().getOperationName(),
getVariables(exchange));
- HttpEntity requestEntity = new StringEntity(requestBody, ContentType.create("application/json", "UTF-8"));
+ HttpEntity requestEntity = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
HttpPost httpPost = new HttpPost(httpUri);
httpPost.setHeader(HttpHeaders.ACCEPT, "application/json");
diff --git a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlServer.java b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlServer.java
index 1e56aaa50c9..4621f5f77f9 100644
--- a/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlServer.java
+++ b/components/camel-graphql/src/test/java/org/apache/camel/component/graphql/server/GraphqlServer.java
@@ -18,23 +18,22 @@ package org.apache.camel.component.graphql.server;
import java.io.IOException;
import java.util.Map;
-import java.util.concurrent.TimeUnit;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpEntityEnclosingRequest;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.impl.bootstrap.HttpServer;
-import org.apache.http.impl.bootstrap.ServerBootstrap;
-import org.apache.http.protocol.HttpContext;
-import org.apache.http.protocol.HttpRequestHandler;
-import org.apache.http.util.EntityUtils;
+import org.apache.hc.core5.http.ClassicHttpRequest;
+import org.apache.hc.core5.http.ClassicHttpResponse;
+import org.apache.hc.core5.http.HttpEntity;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
+import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
+import org.apache.hc.core5.http.io.HttpRequestHandler;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
+import org.apache.hc.core5.http.io.entity.StringEntity;
+import org.apache.hc.core5.http.protocol.HttpContext;
public class GraphqlServer {
@@ -44,7 +43,7 @@ public class GraphqlServer {
public GraphqlServer() {
this.graphql = GraphqlFactory.newGraphQL();
this.server = ServerBootstrap.bootstrap()
- .registerHandler("/graphql", new GraphqlHandler())
+ .register("/graphql", new GraphqlHandler())
.create();
}
@@ -53,7 +52,7 @@ public class GraphqlServer {
}
public void shutdown() {
- server.shutdown(0, TimeUnit.SECONDS);
+ server.close();
}
public int getPort() {
@@ -64,33 +63,32 @@ public class GraphqlServer {
private final ObjectMapper objectMapper = new ObjectMapper();
- public void handle(HttpRequest request, HttpResponse response, HttpContext context)
- throws IOException {
- if (request instanceof HttpEntityEnclosingRequest) {
- HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
- String json = EntityUtils.toString(entity);
-
- Map<String, Object> map = jsonToMap(json);
- String query = (String) map.get("query");
- String operationName = (String) map.get("operationName");
- Map<String, Object> variables = (Map<String, Object>) map.get("variables");
-
- ExecutionInput executionInput = ExecutionInput.newExecutionInput()
- .query(query)
- .operationName(operationName)
- .variables(variables)
- .build();
- ExecutionResult executionResult = graphql.execute(executionInput);
- Map<String, Object> resultMap = executionResult.toSpecification();
- String result = objectMapper.writeValueAsString(resultMap);
-
- response.setHeader("Content-Type", "application/json; charset=UTF-8");
- response.setEntity(new StringEntity(result));
- }
+ @SuppressWarnings("unchecked")
+ public void handle(ClassicHttpRequest request, ClassicHttpResponse response, HttpContext context)
+ throws HttpException, IOException {
+ HttpEntity entity = request.getEntity();
+ String json = EntityUtils.toString(entity);
+
+ Map<String, Object> map = jsonToMap(json);
+ String query = (String) map.get("query");
+ String operationName = (String) map.get("operationName");
+ Map<String, Object> variables = (Map<String, Object>) map.get("variables");
+
+ ExecutionInput executionInput = ExecutionInput.newExecutionInput()
+ .query(query)
+ .operationName(operationName)
+ .variables(variables)
+ .build();
+ ExecutionResult executionResult = graphql.execute(executionInput);
+ Map<String, Object> resultMap = executionResult.toSpecification();
+ String result = objectMapper.writeValueAsString(resultMap);
+
+ response.setHeader("Content-Type", "application/json; charset=UTF-8");
+ response.setEntity(new StringEntity(result));
}
private Map<String, Object> jsonToMap(String json) throws IOException {
- return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
+ return objectMapper.readValue(json, new TypeReference<>() {
});
}