/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.component.servicenow.auth;

import java.io.IOException;
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;
    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);
    }

    @Override
    public void filter(ClientRequestContext requestContext) throws IOException {
        if (token != null) {
            requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, token.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"));
    }
}

@@ -878,16 +878,83 @@
 eption;%0A
+import java.nio.charset.StandardCharsets;%0Aimport java.util.Base64;%0A
 import j
@@ -1178,49 +1178,8 @@
 der;
-%0Aimport javax.xml.bind.DatatypeConverter;
 %0A%0Aim
@@ -1302,16 +1302,22 @@
 %0Apublic 
+final 
 class Au
@@ -1379,65 +1379,8 @@
 r %7B%0A
-    private final ServiceNowConfiguration configuration;%0A
     
@@ -1404,17 +1404,21 @@
 thToken 
-t
+authT
 oken;%0A  
@@ -1516,33 +1516,24 @@
 uration conf
-iguration
 ) throws IOE
@@ -1560,53 +1560,13 @@
 his.
-configuration = configuration;%0A        this.t
+authT
 oken
@@ -1568,33 +1568,24 @@
 Token = conf
-iguration
 .hasOAuthAut
@@ -1619,26 +1619,12 @@
 ken(
-this.configuration
+conf
 ) : 
@@ -1655,30 +1655,70 @@
 tring = 
-buildBasicAuth
+conf.hasBasicAuthentication() ? getBasicAuthentication
 String(c
@@ -1720,26 +1720,24 @@
 ing(conf
-iguration)
+) : null
 ;%0A    %7D%0A
@@ -1844,17 +1844,21 @@
     if (
-t
+authT
 oken != 
@@ -1936,17 +1936,21 @@
 ZATION, 
-t
+authT
 oken.get
@@ -2136,13 +2136,11 @@
 ing 
-build
+get
 Basi
@@ -2148,21 +2148,25 @@
 Auth
+entication
 String(
-final 
 Serv
@@ -2193,130 +2193,11 @@
 conf
-iguration) throws IOException %7B%0A        if (!configuration.hasBasicAuthentication()) %7B%0A            return null;%0A        %7D%0A
+) %7B
 %0A   
@@ -2226,33 +2226,24 @@
 sword = conf
-iguration
 .getUserName
@@ -2257,25 +2257,16 @@
 %22 + conf
-iguration
 .getPass
@@ -2303,43 +2303,42 @@
 %22 + 
-DatatypeConverter.printBase64Binary
+Base64.getEncoder().encodeToString
 (use
@@ -2363,15 +2363,30 @@
 tes(
-%22UTF-8%22
+StandardCharsets.UTF_8
 ));%0A

Simplify ServiceNow authentication request