1 package org.onap.ccsdk.sli.adaptors.base.http;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.util.Base64;
8 import java.util.Properties;
9 import javax.net.ssl.HostnameVerifier;
10 import javax.net.ssl.SSLSession;
11 import javax.ws.rs.client.ClientBuilder;
12 import org.onap.logging.filter.base.MetricLogClientFilter;
13 import org.onap.logging.filter.base.PayloadLoggingClientFilter;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 public abstract class AbstractHttpAdapter {
18 protected static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
19 private static final String SDNC_CONFIG_DIR_DEFAULT = "/opt/sdnc/data/properties";
20 protected static final int DEFAULT_HTTP_CONNECT_TIMEOUT_MS = 60000; // 1 minute
21 protected static final int DEFAULT_HTTP_READ_TIMEOUT_MS = 1800000; // 30 minutes
22 protected ClientBuilder clientBuilder;
24 private static final Logger logger = LoggerFactory.getLogger(AbstractHttpAdapter.class);
26 public AbstractHttpAdapter() {
27 clientBuilder = ClientBuilder.newBuilder();
29 defaultHostNameVerifier();
32 private void defaultHostNameVerifier() {
33 clientBuilder.hostnameVerifier(new HostnameVerifier() {
35 public boolean verify(String hostname, SSLSession session) {
41 protected void enableMetricLogging() {
42 clientBuilder.register(new MetricLogClientFilter());
45 protected void enablePayloadLogging() {
46 clientBuilder.register(new PayloadLoggingClientFilter());
49 private void setTimeouts() {
50 Integer httpReadTimeout = readOptionalInteger("HTTP_READ_TIMEOUT_MS", DEFAULT_HTTP_READ_TIMEOUT_MS);
51 Integer httpConnectTimeout = readOptionalInteger("HTTP_CONNECT_TIMEOUT_MS", DEFAULT_HTTP_CONNECT_TIMEOUT_MS);
53 // restore once we migrate to once we migrate to javax.ws.rs-api 2.1
54 // clientBuilder.connectTimeout(30, TimeUnit.SECONDS);
55 // clientBuilder.readTimeout(30, TimeUnit.SECONDS);
57 // Setting jersey specific properties is ugly, such behavior should be removed
58 // once we migrate to javax.ws.rs-api 2.1
59 clientBuilder.property("jersey.config.client.readTimeout", httpReadTimeout);
60 clientBuilder.property("jersey.config.client.connectTimeout", httpConnectTimeout);
63 public Properties getProperties(String propertiesFileName) throws FileNotFoundException, IOException {
64 // Check System property, then environment variable then default if null
65 String propDir = System.getProperty(SDNC_CONFIG_DIR);
66 if (propDir == null || propDir.length() < 1) {
67 propDir = System.getenv(SDNC_CONFIG_DIR);
69 if (propDir == null || propDir.length() < 1) {
70 propDir = SDNC_CONFIG_DIR_DEFAULT;
72 Properties properties = new Properties();
73 // forward slash is checked to support path src/test/resources on windows machine
74 if (!propDir.endsWith(File.separator) && !propDir.endsWith("/")) {
75 propDir = propDir + File.separator;
77 String path = propDir + propertiesFileName;
78 properties.load(new FileInputStream(path));
79 logger.trace("Initialized properties from ({}) properties ({})", path, properties);
83 protected void addBasicAuthCredentials(String username, String password) {
84 String basicAuthValue = getBasicAuthValue(username,password);
85 clientBuilder.register(new BasicAuthFilter(basicAuthValue));
88 protected String getBasicAuthValue(String userName, String password) {
89 String token = userName + ":" + password;
91 return "Basic " + Base64.getEncoder().encodeToString(token.getBytes());
92 } catch (Exception e) {
93 logger.error("getBasicAuthValue threw an exception, credentials will be null", e);
98 public ClientBuilder getClientBuilder() {
102 private Integer readOptionalInteger(String propertyName, Integer defaultValue) {
103 String stringValue = System.getProperty(propertyName);
104 if (stringValue != null && stringValue.length() > 0) {
106 return Integer.valueOf(stringValue);
107 } catch (NumberFormatException e) {
108 logger.warn("property " + propertyName + " had the value " + stringValue + " that could not be converted to an Integer, default " + defaultValue + " will be used instead", e);