Quantcast

camel git commit: Simplify ServiceNow authentication request

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

camel git commit: Simplify ServiceNow authentication request

lburgazzoli-2
Repository: camel
Updated Branches:
  refs/heads/master 56b55cf67 -> 7aeb3b769


Simplify ServiceNow authentication request


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7aeb3b76
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7aeb3b76
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7aeb3b76

Branch: refs/heads/master
Commit: 7aeb3b769a9df352c8adde12c79369131b4f84fb
Parents: 56b55cf
Author: lburgazzoli <[hidden email]>
Authored: Wed Mar 15 18:07:11 2017 +0100
Committer: lburgazzoli <[hidden email]>
Committed: Wed Mar 15 18:07:17 2017 +0100

----------------------------------------------------------------------
 .../auth/AuthenticationRequestFilter.java       | 29 ++++++++------------
 1 file changed, 12 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7aeb3b76/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/auth/AuthenticationRequestFilter.java
----------------------------------------------------------------------
diff --git a/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/auth/AuthenticationRequestFilter.java b/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/auth/AuthenticationRequestFilter.java
index 9e934ad..4689455 100644
--- a/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/auth/AuthenticationRequestFilter.java
+++ b/components/camel-servicenow/src/main/java/org/apache/camel/component/servicenow/auth/AuthenticationRequestFilter.java
@@ -17,45 +17,40 @@
 package org.apache.camel.component.servicenow.auth;
 
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
 import javax.annotation.Priority;
 import javax.ws.rs.Priorities;
 import javax.ws.rs.client.ClientRequestContext;
 import javax.ws.rs.client.ClientRequestFilter;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.ext.Provider;
-import javax.xml.bind.DatatypeConverter;
 
 import org.apache.camel.component.servicenow.ServiceNowConfiguration;
 
 
 @Provider
 @Priority(Priorities.AUTHENTICATION)
-public class AuthenticationRequestFilter implements ClientRequestFilter {
-    private final ServiceNowConfiguration configuration;
-    private final OAuthToken token;
+public final class AuthenticationRequestFilter implements ClientRequestFilter {
+    private final OAuthToken authToken;
     private final String authString;
 
-    public AuthenticationRequestFilter(ServiceNowConfiguration configuration) throws IOException {
-        this.configuration = configuration;
-        this.token = configuration.hasOAuthAuthentication() ? new OAuthToken(this.configuration) : null;
-        this.authString = buildBasicAuthString(configuration);
+    public AuthenticationRequestFilter(ServiceNowConfiguration conf) throws IOException {
+        this.authToken = conf.hasOAuthAuthentication() ? new OAuthToken(conf) : null;
+        this.authString = conf.hasBasicAuthentication() ? getBasicAuthenticationString(conf) : null;
     }
 
     @Override
     public void filter(ClientRequestContext requestContext) throws IOException {
-        if (token != null) {
-            requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, token.getAuthString());
+        if (authToken != null) {
+            requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, authToken.getAuthString());
         } else if (authString != null) {
             requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, authString);
         }
     }
 
-    private static String buildBasicAuthString(final ServiceNowConfiguration configuration) throws IOException {
-        if (!configuration.hasBasicAuthentication()) {
-            return null;
-        }
-
-        String userAndPassword = configuration.getUserName() + ":" + configuration.getPassword();
-        return "Basic " + DatatypeConverter.printBase64Binary(userAndPassword.getBytes("UTF-8"));
+    private static String getBasicAuthenticationString(ServiceNowConfiguration conf) {
+        String userAndPassword = conf.getUserName() + ":" + conf.getPassword();
+        return "Basic " + Base64.getEncoder().encodeToString(userAndPassword.getBytes(StandardCharsets.UTF_8));
     }
 }

Loading...