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:33 UTC

[camel] 15/19: CAMEL-18995: camel-workday - 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 810e77367d128632675c09a995900e66aa1daf8d
Author: Nicolas Filotto <nf...@talend.com>
AuthorDate: Fri Mar 17 12:33:27 2023 +0100

    CAMEL-18995: camel-workday - Upgrade to HttpComponents 5.x
---
 components/camel-workday/pom.xml                   | 11 +---
 .../workday/WorkdayEndpointConfigurer.java         |  4 +-
 .../apache/camel/component/workday/workday.json    |  2 +-
 .../component/workday/WorkdayConfiguration.java    |  2 +-
 .../workday/auth/AuthClientForIntegration.java     | 62 +++++++++---------
 ...cationClient.java => AuthenticationClient.java} |  8 +--
 .../workday/producer/WorkdayDefaultProducer.java   | 74 +++++++++++++---------
 7 files changed, 88 insertions(+), 75 deletions(-)

diff --git a/components/camel-workday/pom.xml b/components/camel-workday/pom.xml
index c46089089bd..f346db51d4c 100644
--- a/components/camel-workday/pom.xml
+++ b/components/camel-workday/pom.xml
@@ -35,14 +35,9 @@
 
   <dependencies>
     <dependency>
-      <groupId>org.apache.httpcomponents</groupId>
-      <artifactId>httpclient</artifactId>
-      <version>${httpclient4-version}</version>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.httpcomponents</groupId>
-      <artifactId>httpmime</artifactId>
-      <version>${httpclient4-version}</version>
+      <groupId>org.apache.httpcomponents.client5</groupId>
+      <artifactId>httpclient5</artifactId>
+      <version>${httpclient-version}</version>
     </dependency>
     <dependency>
       <groupId>org.apache.camel</groupId>
diff --git a/components/camel-workday/src/generated/java/org/apache/camel/component/workday/WorkdayEndpointConfigurer.java b/components/camel-workday/src/generated/java/org/apache/camel/component/workday/WorkdayEndpointConfigurer.java
index 09421ccdaf2..082f98a3bb8 100644
--- a/components/camel-workday/src/generated/java/org/apache/camel/component/workday/WorkdayEndpointConfigurer.java
+++ b/components/camel-workday/src/generated/java/org/apache/camel/component/workday/WorkdayEndpointConfigurer.java
@@ -27,7 +27,7 @@ public class WorkdayEndpointConfigurer extends PropertyConfigurerSupport impleme
         case "clientSecret": target.getWorkdayConfiguration().setClientSecret(property(camelContext, java.lang.String.class, value)); return true;
         case "host": target.getWorkdayConfiguration().setHost(property(camelContext, java.lang.String.class, value)); return true;
         case "httpconnectionmanager":
-        case "httpConnectionManager": target.getWorkdayConfiguration().setHttpConnectionManager(property(camelContext, org.apache.http.impl.conn.PoolingHttpClientConnectionManager.class, value)); return true;
+        case "httpConnectionManager": target.getWorkdayConfiguration().setHttpConnectionManager(property(camelContext, org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.class, value)); return true;
         case "lazystartproducer":
         case "lazyStartProducer": target.setLazyStartProducer(property(camelContext, boolean.class, value)); return true;
         case "reportformat":
@@ -48,7 +48,7 @@ public class WorkdayEndpointConfigurer extends PropertyConfigurerSupport impleme
         case "clientSecret": return java.lang.String.class;
         case "host": return java.lang.String.class;
         case "httpconnectionmanager":
-        case "httpConnectionManager": return org.apache.http.impl.conn.PoolingHttpClientConnectionManager.class;
+        case "httpConnectionManager": return org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager.class;
         case "lazystartproducer":
         case "lazyStartProducer": return boolean.class;
         case "reportformat":
diff --git a/components/camel-workday/src/generated/resources/org/apache/camel/component/workday/workday.json b/components/camel-workday/src/generated/resources/org/apache/camel/component/workday/workday.json
index 3898e573953..2ba60b9d80a 100644
--- a/components/camel-workday/src/generated/resources/org/apache/camel/component/workday/workday.json
+++ b/components/camel-workday/src/generated/resources/org/apache/camel/component/workday/workday.json
@@ -32,7 +32,7 @@
     "entity": { "kind": "path", "displayName": "Entity", "group": "producer", "label": "", "required": true, "type": "object", "javaType": "org.apache.camel.component.workday.WorkdayConfiguration.Entity", "enum": [ "report", "commonAPI" ], "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.workday.WorkdayConfiguration", "configurationField": "workdayConfiguration", "description": "The entity to be requested o [...]
     "path": { "kind": "path", "displayName": "Path", "group": "producer", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.workday.WorkdayConfiguration", "configurationField": "workdayConfiguration", "description": "The API path to access an entity structure." },
     "lazyStartProducer": { "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer (advanced)", "label": "producer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may other [...]
-    "httpConnectionManager": { "kind": "parameter", "displayName": "Http Connection Manager", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.http.impl.conn.PoolingHttpClientConnectionManager", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.workday.WorkdayConfiguration", "configurationField": "workdayConfiguration", "description": "Pool connection manager for advanced confi [...]
+    "httpConnectionManager": { "kind": "parameter", "displayName": "Http Connection Manager", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager", "deprecated": false, "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.workday.WorkdayConfiguration", "configurationField": "workdayConfiguration", "description": "Pool connection manager for advan [...]
     "reportFormat": { "kind": "parameter", "displayName": "Report Format", "group": "format", "label": "format", "required": false, "type": "string", "javaType": "java.lang.String", "enum": [ "json" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "json", "configurationClass": "org.apache.camel.component.workday.WorkdayConfiguration", "configurationField": "workdayConfiguration", "description": "Workday Report as a service output format." },
     "host": { "kind": "parameter", "displayName": "Host", "group": "host", "label": "host", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "configurationClass": "org.apache.camel.component.workday.WorkdayConfiguration", "configurationField": "workdayConfiguration", "description": "Workday Host name." },
     "clientId": { "kind": "parameter", "displayName": "Client Id", "group": "security", "label": "security", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": true, "configurationClass": "org.apache.camel.component.workday.WorkdayConfiguration", "configurationField": "workdayConfiguration", "description": "Workday client Id generated by API client for integrations." },
diff --git a/components/camel-workday/src/main/java/org/apache/camel/component/workday/WorkdayConfiguration.java b/components/camel-workday/src/main/java/org/apache/camel/component/workday/WorkdayConfiguration.java
index 3ed01112d7c..d1041ba9436 100644
--- a/components/camel-workday/src/main/java/org/apache/camel/component/workday/WorkdayConfiguration.java
+++ b/components/camel-workday/src/main/java/org/apache/camel/component/workday/WorkdayConfiguration.java
@@ -25,7 +25,7 @@ import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriParams;
 import org.apache.camel.spi.UriPath;
 import org.apache.camel.util.ObjectHelper;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
 
 @UriParams
 public class WorkdayConfiguration {
diff --git a/components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AuthClientForIntegration.java b/components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AuthClientForIntegration.java
index 242514628f9..31bf771d7a8 100644
--- a/components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AuthClientForIntegration.java
+++ b/components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AuthClientForIntegration.java
@@ -18,22 +18,22 @@ package org.apache.camel.component.workday.auth;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Base64;
 import java.util.List;
 
 import org.apache.camel.component.workday.WorkdayConfiguration;
-import org.apache.http.HttpStatus;
-import org.apache.http.NameValuePair;
-import org.apache.http.client.entity.UrlEncodedFormEntity;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpRequestBase;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.message.BasicNameValuePair;
+import org.apache.hc.client5.http.classic.methods.HttpPost;
+import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
+import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
+import org.apache.hc.core5.http.HttpStatus;
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.http.message.BasicNameValuePair;
 
-public class AuthClientForIntegration implements AutheticationClient {
+public class AuthClientForIntegration implements AuthenticationClient {
 
     public static final String BASE_TOKEN_ENDPOINT = "https://%s/ccx/oauth2/%s/token";
 
@@ -49,15 +49,14 @@ public class AuthClientForIntegration implements AutheticationClient {
 
     private static final String CONTENT_TYPE = "application/x-www-form-urlencoded";
 
-    private WorkdayConfiguration workdayConfiguration;
+    private final WorkdayConfiguration workdayConfiguration;
 
     public AuthClientForIntegration(WorkdayConfiguration workdayConfiguration) {
         this.workdayConfiguration = workdayConfiguration;
     }
 
     @Override
-    public void configure(CloseableHttpClient httpClient, HttpRequestBase method) throws IOException {
-
+    public void configure(CloseableHttpClient httpClient, HttpUriRequest method) throws IOException {
         String bearerToken = getBearerToken(httpClient);
         method.addHeader(AUTHORIZATION_HEADER, "Bearer " + bearerToken);
 
@@ -69,33 +68,36 @@ public class AuthClientForIntegration implements AutheticationClient {
 
         HttpPost httpPost = createPostMethod(tokenUrl);
 
-        CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
-
-        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
-            throw new IllegalStateException(
-                    "Got the invalid http status value '" + httpResponse.getStatusLine()
-                                            + "' as the result of the Token Request '" + tokenUrl + "'");
-        }
+        return httpClient.execute(
+                httpPost,
+                httpResponse -> {
+                    if (httpResponse.getCode() != HttpStatus.SC_OK) {
+                        throw new IllegalStateException(
+                                "Got the invalid http status value '" + httpResponse
+                                                        + "' as the result of the Token Request '" + tokenUrl + "'");
+                    }
+
+                    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
+                        httpResponse.getEntity().writeTo(baos);
+                        return parseResponse(baos.toString());
+                    }
+                });
 
-        ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        httpResponse.getEntity().writeTo(baos);
-        return parseResponse(baos.toString());
     }
 
-    private HttpPost createPostMethod(String tokenUrl) throws UnsupportedEncodingException {
+    private HttpPost createPostMethod(String tokenUrl) {
 
-        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
+        List<NameValuePair> nvps = new ArrayList<>();
         nvps.add(new BasicNameValuePair(GRANT_TYPE, REFRESH_TOKEN));
         nvps.add(new BasicNameValuePair(REFRESH_TOKEN, workdayConfiguration.getTokenRefresh()));
 
         HttpPost postMethod = new HttpPost(tokenUrl);
         postMethod.addHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE);
         postMethod.addHeader(AUTHORIZATION_HEADER,
-                "Basic " + new String(
-                        Base64.getEncoder()
-                                .encode((workdayConfiguration.getClientId() + ":" + workdayConfiguration.getClientSecret())
-                                        .getBytes())));
-        postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
+                "Basic " + Arrays.toString(Base64.getEncoder()
+                        .encode((workdayConfiguration.getClientId() + ":" + workdayConfiguration.getClientSecret())
+                                .getBytes())));
+        postMethod.setEntity(new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
 
         return postMethod;
     }
diff --git a/components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AutheticationClient.java b/components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AuthenticationClient.java
similarity index 76%
rename from components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AutheticationClient.java
rename to components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AuthenticationClient.java
index cc99354e7b7..8bb959a1f35 100644
--- a/components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AutheticationClient.java
+++ b/components/camel-workday/src/main/java/org/apache/camel/component/workday/auth/AuthenticationClient.java
@@ -18,11 +18,11 @@ package org.apache.camel.component.workday.auth;
 
 import java.io.IOException;
 
-import org.apache.http.client.methods.HttpRequestBase;
-import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.hc.client5.http.classic.methods.HttpUriRequest;
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
 
-public interface AutheticationClient {
+public interface AuthenticationClient {
 
-    void configure(CloseableHttpClient httpClient, HttpRequestBase method) throws IOException;
+    void configure(CloseableHttpClient httpClient, HttpUriRequest request) throws IOException;
 
 }
diff --git a/components/camel-workday/src/main/java/org/apache/camel/component/workday/producer/WorkdayDefaultProducer.java b/components/camel-workday/src/main/java/org/apache/camel/component/workday/producer/WorkdayDefaultProducer.java
index c89b126985b..2162f06cdf6 100644
--- a/components/camel-workday/src/main/java/org/apache/camel/component/workday/producer/WorkdayDefaultProducer.java
+++ b/components/camel-workday/src/main/java/org/apache/camel/component/workday/producer/WorkdayDefaultProducer.java
@@ -17,18 +17,19 @@
 package org.apache.camel.component.workday.producer;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.NoTypeConversionAvailableException;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.workday.WorkdayConfiguration;
 import org.apache.camel.component.workday.WorkdayEndpoint;
 import org.apache.camel.component.workday.auth.AuthClientForIntegration;
-import org.apache.camel.component.workday.auth.AutheticationClient;
+import org.apache.camel.component.workday.auth.AuthenticationClient;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.support.DefaultProducer;
-import org.apache.http.HttpStatus;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.hc.client5.http.classic.methods.HttpGet;
+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.io.PoolingHttpClientConnectionManager;
+import org.apache.hc.core5.http.HttpStatus;
 
 /**
  * The Workday Default producer.
@@ -38,14 +39,14 @@ public abstract class WorkdayDefaultProducer extends DefaultProducer {
     @Metadata(description = "The workday URL", javaType = "String")
     public static final String WORKDAY_URL_HEADER = "CamelWorkdayURL";
 
-    private WorkdayEndpoint endpoint;
+    private final WorkdayEndpoint endpoint;
 
-    private AutheticationClient autheticationClient;
+    private final AuthenticationClient authenticationClient;
 
     public WorkdayDefaultProducer(WorkdayEndpoint endpoint) {
         super(endpoint);
         this.endpoint = endpoint;
-        this.autheticationClient = new AuthClientForIntegration(this.endpoint.getWorkdayConfiguration());
+        this.authenticationClient = new AuthClientForIntegration(this.endpoint.getWorkdayConfiguration());
     }
 
     @Override
@@ -60,26 +61,41 @@ public abstract class WorkdayDefaultProducer extends DefaultProducer {
         String workdayUri = prepareUri(endpoint.getWorkdayConfiguration());
 
         HttpGet httpGet = new HttpGet(workdayUri);
-        this.autheticationClient.configure(httpClient, httpGet);
-
-        CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
-
-        if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
-            throw new IllegalStateException(
-                    "Got the invalid http status value '" + httpResponse.getStatusLine() + "' as the result of the RAAS '"
-                                            + workdayUri + "'");
-        }
-
-        String report = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class,
-                httpResponse.getEntity().getContent());
-
-        if (report.isEmpty()) {
-            throw new IllegalStateException(
-                    "Got the unexpected value '" + report + "' as the result of the report '" + workdayUri + "'");
+        this.authenticationClient.configure(httpClient, httpGet);
+
+        try {
+            httpClient.execute(
+                    httpGet,
+                    httpResponse -> {
+                        if (httpResponse.getCode() != HttpStatus.SC_OK) {
+                            throw new IllegalStateException(
+                                    "Got the invalid http status value '" + httpResponse + "' as the result of the RAAS '"
+                                                            + workdayUri + "'");
+                        }
+
+                        try {
+                            String report = getEndpoint().getCamelContext().getTypeConverter().mandatoryConvertTo(String.class,
+                                    httpResponse.getEntity().getContent());
+
+                            if (report.isEmpty()) {
+                                throw new IllegalStateException(
+                                        "Got the unexpected value '" + report + "' as the result of the report '" + workdayUri
+                                                                + "'");
+                            }
+
+                            exchange.getIn().setBody(report);
+                            exchange.getIn().setHeader(WORKDAY_URL_HEADER, workdayUri);
+                            return null;
+                        } catch (NoTypeConversionAvailableException e) {
+                            throw new RuntimeCamelException(e);
+                        }
+                    });
+        } catch (RuntimeCamelException e) {
+            if (e.getCause() instanceof Exception ex) {
+                throw ex;
+            }
+            throw e;
         }
-
-        exchange.getIn().setBody(report);
-        exchange.getIn().setHeader(WORKDAY_URL_HEADER, workdayUri);
     }
 
     public abstract String prepareUri(WorkdayConfiguration configuration) throws Exception;