Skip to content

Commit

Permalink
feat(thrift): add http proxy for thrift clients
Browse files Browse the repository at this point in the history
Signed-off-by: Till Markus (INST-CSS/BSV-OS) <[email protected]>
  • Loading branch information
bs-matil committed Mar 25, 2019
1 parent a1a92a9 commit 3bb68c9
Showing 1 changed file with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
package org.eclipse.sw360.datahandler.thrift;

import org.apache.http.HttpHost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentService;
import org.eclipse.sw360.datahandler.thrift.components.ComponentService;
Expand All @@ -33,6 +37,8 @@
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransportException;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import static org.apache.log4j.Logger.getLogger;
Expand All @@ -50,6 +56,7 @@ public class ThriftClients {
public static final String PROPERTIES_FILE_PATH = "/sw360.properties";

public static final String BACKEND_URL;
public static final String BACKEND_PROXY_URL;

//! Service addresses
private static final String ATTACHMENT_SERVICE_URL = "/attachments/thrift";
Expand All @@ -76,8 +83,9 @@ public class ThriftClients {
Properties props = CommonUtils.loadProperties(ThriftClients.class, PROPERTIES_FILE_PATH);

BACKEND_URL = props.getProperty("backend.url", "http://127.0.0.1:8080");
//Proxy can be set e.g. with "http://localhost:3128". if set all request to the thrift backend are routed through the proxy
BACKEND_PROXY_URL = props.getProperty("backend.proxy.url", null);
}

public ThriftClients() {
}

Expand All @@ -88,9 +96,19 @@ private static TProtocol makeProtocol(String url, String service) {
THttpClient thriftClient = null;
final String destinationAddress = url + service;
try {
thriftClient = new THttpClient(destinationAddress);
if (BACKEND_PROXY_URL != null) {
URL proxyUrl = new URL(BACKEND_PROXY_URL);
HttpHost proxy = new HttpHost(proxyUrl.getHost(), proxyUrl.getPort(), proxyUrl.getProtocol());
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpClient = HttpClients.custom().setRoutePlanner(routePlanner).build();
thriftClient = new THttpClient(destinationAddress, httpClient);
} else {
thriftClient = new THttpClient(destinationAddress);
}
} catch (TTransportException e) {
log.error("cannot connect to backend on " + destinationAddress, e);
} catch (MalformedURLException e) {
log.error("cannot connect via http proxy (REASON:MalformedURLException) to thrift backend", e);
}
return new TCompactProtocol(thriftClient);
}
Expand Down

0 comments on commit 3bb68c9

Please sign in to comment.