org.jlot.client.config.rest.MySimpleClientHttpRequestFactory Maven / Gradle / Ivy
package org.jlot.client.config.rest;
import java.io.IOException;
import java.net.HttpURLConnection;
import javax.inject.Inject;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import org.jlot.client.login.Login;
import org.jlot.client.login.LoginContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Component;
@Component
public class MySimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory
{
private final Logger logger = LoggerFactory.getLogger(MySimpleClientHttpRequestFactory.class);
@Inject
private LoginContext loginContext;
@Inject
private HostnameVerifier hostnameVerifier;
@Override
protected void prepareConnection ( HttpURLConnection connection, String httpMethod ) throws IOException
{
addHostnameVerifier(connection);
super.prepareConnection(connection, httpMethod);
addLogin(connection);
}
private void addHostnameVerifier ( HttpURLConnection connection )
{
if (connection instanceof HttpsURLConnection)
{
( (HttpsURLConnection) connection ).setHostnameVerifier(hostnameVerifier);
}
}
private void addLogin ( HttpURLConnection connection )
{
Login login = loginContext.getLogin();
if (login != null)
{
logger.debug("Setting Authorization header for http request");
connection.setRequestProperty("Authorization", login.getBasicAuthorisationValue());
}
}
}