
com.sap.cloud.sdk.cloudplatform.connectivity.HttpClientWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connectivity Show documentation
Show all versions of connectivity Show documentation
Cloud platform abstraction for general-purpose connectivity.
The newest version!
/*
* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved.
*/
package com.sap.cloud.sdk.cloudplatform.connectivity;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.ServiceLoader;
import javax.annotation.Nonnull;
import org.apache.http.HeaderElement;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import com.sap.cloud.s4hana.quality.QualityListenerException;
import com.sap.cloud.sdk.cloudplatform.logging.CloudLoggerFactory;
import lombok.RequiredArgsConstructor;
/**
* Decorates the HttpClient of a given destination. This will allow the HttpClient user to send the relative url path
* and it will append the url configured in the destination.
*/
class HttpClientWrapper implements HttpClient
{
private static final Logger logger = CloudLoggerFactory.getSanitizedLogger(HttpClientWrapper.class);
@RequiredArgsConstructor
private static class ApacheHttpHeader implements org.apache.http.Header
{
@Nonnull
private final Header header;
@Override
public String getName()
{
return header.getName();
}
@Override
public String getValue()
{
return header.getValue();
}
@Override
public HeaderElement[] getElements()
throws ParseException
{
return new HeaderElement[0];
}
}
private static final List listeners;
static {
listeners = ImmutableList.copyOf(ServiceLoader.load(HttpClientListener.class));
}
private final HttpClient httpClient;
private final Destination destination;
HttpClientWrapper( final HttpClient httpClient, final Destination destination )
{
this.httpClient = httpClient;
this.destination = destination;
}
private HttpUriRequest wrapRequest( final HttpUriRequest request )
{
final URI requestUri = new UriPathMerger().merge(destination.getUri(), request.getURI());
final RequestBuilder requestBuilder = RequestBuilder.copy(request);
requestBuilder.setUri(requestUri);
for( final Header header : destination.getHeaders(requestUri) ) {
requestBuilder.addHeader(new ApacheHttpHeader(header));
}
return requestBuilder.build();
}
@Override
public HttpResponse execute( final HttpUriRequest request )
throws IOException
{
final HttpUriRequest httpUriRequest = wrapRequest(request);
notifyListeners(httpUriRequest.getURI());
return httpClient.execute(httpUriRequest);
}
@Override
public HttpResponse execute( final HttpUriRequest request, final HttpContext context )
throws IOException
{
final HttpUriRequest httpUriRequest = wrapRequest(request);
notifyListeners(httpUriRequest.getURI());
return httpClient.execute(httpUriRequest, context);
}
@Override
public HttpResponse execute( final HttpHost target, final HttpRequest request )
throws IOException
{
notifyListeners(target.toURI(), request.getRequestLine().getUri());
return httpClient.execute(target, request);
}
@Override
public T execute( final HttpUriRequest request, final ResponseHandler extends T> responseHandler )
throws IOException
{
final HttpUriRequest httpUriRequest = wrapRequest(request);
notifyListeners(httpUriRequest.getURI());
return httpClient.execute(httpUriRequest, responseHandler);
}
@Override
public HttpResponse execute( final HttpHost target, final HttpRequest request, final HttpContext context )
throws IOException
{
notifyListeners(target.toURI(), request.getRequestLine().getUri());
return httpClient.execute(target, request, context);
}
@Override
public T execute(
final HttpUriRequest request,
final ResponseHandler extends T> responseHandler,
final HttpContext context )
throws IOException
{
notifyListeners(request.getURI());
return httpClient.execute(wrapRequest(request), responseHandler, context);
}
@Override
public <
T>
T
execute( final HttpHost target, final HttpRequest request, final ResponseHandler extends T> responseHandler )
throws IOException
{
notifyListeners(target.toURI(), request.getRequestLine().getUri());
return httpClient.execute(target, request, responseHandler);
}
@Override
public T execute(
final HttpHost target,
final HttpRequest request,
final ResponseHandler extends T> responseHandler,
final HttpContext context )
throws IOException
{
notifyListeners(target.toURI(), request.getRequestLine().getUri());
return httpClient.execute(target, request, responseHandler, context);
}
@SuppressWarnings( "deprecation" )
@Override
public org.apache.http.conn.ClientConnectionManager getConnectionManager()
{
return httpClient.getConnectionManager();
}
@SuppressWarnings( "deprecation" )
@Override
public org.apache.http.params.HttpParams getParams()
{
return httpClient.getParams();
}
private void notifyListeners( final URI uri )
{
final String portString = (uri.getPort() == -1) ? "" : String.valueOf(uri.getPort());
String scheme = Strings.nullToEmpty(uri.getScheme());
if( !Strings.isNullOrEmpty(scheme) ) {
scheme += "://";
}
final String reducedUri =
scheme + Strings.nullToEmpty(uri.getHost()) + portString + Strings.nullToEmpty(uri.getPath());
for( final HttpClientListener listener : listeners ) {
try {
listener.onRequest(reducedUri);
}
catch( final QualityListenerException e ) {
logger.error("Failure while invoking query listener of type " + listener.getClass().getName() + ".", e);
}
}
}
private void notifyListeners( final String targetUriString, final String requestUriString )
{
try {
notifyListeners(URI.create(targetUriString + requestUriString));
}
catch( final IllegalArgumentException e ) {
logger.error("Failed to construct URI from '" + targetUriString + "' and '" + requestUriString + "'.", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy