com.databasesandlife.util.spring.SoapClientBasicAuthHeaderWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Utility classes developed at Adrian Smith Software (A.S.S.)
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