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;
13 import org.onap.ccsdk.sli.core.utils.common.EnvProperties;
14 import org.onap.logging.filter.base.MetricLogClientFilter;
15 import org.onap.logging.filter.base.PayloadLoggingClientFilter;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 public abstract class AbstractHttpAdapter {
20 protected static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
21 private static final String SDNC_CONFIG_DIR_DEFAULT = "/opt/sdnc/data/properties";
22 protected static final int DEFAULT_HTTP_CONNECT_TIMEOUT_MS = 60000; // 1 minute
23 protected static final int DEFAULT_HTTP_READ_TIMEOUT_MS = 1800000; // 30 minutes
24 protected ClientBuilder clientBuilder;
26 private static final Logger logger = LoggerFactory.getLogger(AbstractHttpAdapter.class);
28 public AbstractHttpAdapter() {
29 clientBuilder = ClientBuilder.newBuilder();
31 defaultHostNameVerifier();
34 private void defaultHostNameVerifier() {
35 clientBuilder.hostnameVerifier(new HostnameVerifier() {
37 public boolean verify(String hostname, SSLSession session) {
43 protected void enableMetricLogging() {
44 clientBuilder.register(new MetricLogClientFilter());
47 protected void enablePayloadLogging() {
48 clientBuilder.register(new PayloadLoggingClientFilter());
51 private void setTimeouts() {
52 Integer httpReadTimeout = readOptionalInteger("HTTP_READ_TIMEOUT_MS", DEFAULT_HTTP_READ_TIMEOUT_MS);
53 Integer httpConnectTimeout = readOptionalInteger("HTTP_CONNECT_TIMEOUT_MS", DEFAULT_HTTP_CONNECT_TIMEOUT_MS);
55 // restore once we migrate to once we migrate to javax.ws.rs-api 2.1
56 // clientBuilder.connectTimeout(30, TimeUnit.SECONDS);
57 // clientBuilder.readTimeout(30, TimeUnit.SECONDS);
59 // Setting jersey specific properties is ugly, such behavior should be removed
60 // once we migrate to javax.ws.rs-api 2.1
61 clientBuilder.property("jersey.config.client.readTimeout", httpReadTimeout);
62 clientBuilder.property("jersey.config.client.connectTimeout", httpConnectTimeout);
65 public Properties getProperties(String propertiesFileName) throws FileNotFoundException, IOException {
66 // Check System property, then environment variable then default if null
67 String propDir = System.getProperty(SDNC_CONFIG_DIR);
68 if (propDir == null || propDir.length() < 1) {
69 propDir = System.getenv(SDNC_CONFIG_DIR);
71 if (propDir == null || propDir.length() < 1) {
72 propDir = SDNC_CONFIG_DIR_DEFAULT;
74 Properties properties = new EnvProperties();
75 // forward slash is checked to support path src/test/resources on windows machine
76 if (!propDir.endsWith(File.separator) && !propDir.endsWith("/")) {
77 propDir = propDir + File.separator;
79 String path = propDir + propertiesFileName;
80 properties.load(new FileInputStream(path));
81 logger.trace("Initialized properties from ({}) properties ({})", path, properties);
85 protected void addBasicAuthCredentials(String username, String password) {
86 String basicAuthValue = getBasicAuthValue(username,password);
87 clientBuilder.register(new BasicAuthFilter(basicAuthValue));
90 protected String getBasicAuthValue(String userName, String password) {
91 String token = userName + ":" + password;
93 return "Basic " + Base64.getEncoder().encodeToString(token.getBytes());
94 } catch (Exception e) {
95 logger.error("getBasicAuthValue threw an exception, credentials will be null", e);
100 public ClientBuilder getClientBuilder() {
101 return clientBuilder;
104 private Integer readOptionalInteger(String propertyName, Integer defaultValue) {
105 String stringValue = System.getProperty(propertyName);
106 if (stringValue != null && stringValue.length() > 0) {
108 return Integer.valueOf(stringValue);
109 } catch (NumberFormatException e) {
110 logger.warn("property " + propertyName + " had the value " + stringValue + " that could not be converted to an Integer, default " + defaultValue + " will be used instead", e);