All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.databasesandlife.util.spring.SoapClientBasicAuthHeaderWriter Maven / Gradle / Ivy

There is a newer version: 21.0.1
Show newest version
package com.databasesandlife.util.spring;

import com.databasesandlife.util.gwtsafe.CleartextPassword;
import org.springframework.ws.transport.http.HttpUrlConnectionMessageSender;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Base64;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * Allows communication with a SOAP server which demands basic authentication.
 * 

* Usage: *

 * var client = getWebServiceTemplate();
 * client.setMessageSender(new SoapClientBasicAuthHeaderWriter(username, password));
 * 
*

*/ public class SoapClientBasicAuthHeaderWriter extends HttpUrlConnectionMessageSender { protected @Nonnull String username; protected @Nonnull CleartextPassword password; public SoapClientBasicAuthHeaderWriter(@Nonnull String username, @Nonnull CleartextPassword password) { this.username = username; this.password = password; } @Override protected void prepareConnection(@Nonnull HttpURLConnection connection) throws IOException { var userpassword = username + ":" + password.getCleartext(); var encodedAuthorization = new String(Base64.getEncoder().encode(userpassword.getBytes(UTF_8)), UTF_8); connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization); super.prepareConnection(connection); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy