65cbdd7645f82d412d515fc70bfbd2c167cf0514
[ccsdk/sli.git] /
1 package org.onap.ccsdk.sli.adaptors.base.http;
2
3 import java.io.File;
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
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;
18
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;
25
26     private static final Logger logger = LoggerFactory.getLogger(AbstractHttpAdapter.class);
27
28     public AbstractHttpAdapter() {
29         clientBuilder = ClientBuilder.newBuilder();
30         setTimeouts();
31         defaultHostNameVerifier();
32     }
33     
34     private void defaultHostNameVerifier() {
35         clientBuilder.hostnameVerifier(new HostnameVerifier() {
36             @Override
37             public boolean verify(String hostname, SSLSession session) {
38                 return true;
39             }
40         });
41     }
42
43     protected void enableMetricLogging() {
44         clientBuilder.register(new MetricLogClientFilter());
45     }
46
47     protected void enablePayloadLogging() {
48         clientBuilder.register(new PayloadLoggingClientFilter());
49     }
50
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);
54
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);
58
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);
63     }
64
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);
70         }
71         if (propDir == null || propDir.length() < 1) {
72             propDir = SDNC_CONFIG_DIR_DEFAULT;
73         }
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;
78         }
79         String path = propDir + propertiesFileName;
80         properties.load(new FileInputStream(path));
81         logger.trace("Initialized properties from ({}) properties ({})", path, properties);
82         return properties;
83     }
84
85     protected void addBasicAuthCredentials(String username, String password) {
86         String basicAuthValue = getBasicAuthValue(username,password);
87         clientBuilder.register(new BasicAuthFilter(basicAuthValue));
88     }
89
90     protected String getBasicAuthValue(String userName, String password) {
91         String token = userName + ":" + password;
92         try {
93             return "Basic " + Base64.getEncoder().encodeToString(token.getBytes());
94         } catch (Exception e) {
95             logger.error("getBasicAuthValue threw an exception, credentials will be null", e);
96         }
97         return null;
98     }
99
100     public ClientBuilder getClientBuilder() {
101         return clientBuilder;
102     }
103
104     private Integer readOptionalInteger(String propertyName, Integer defaultValue) {
105         String stringValue = System.getProperty(propertyName);
106         if (stringValue != null && stringValue.length() > 0) {
107             try {
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);
111             }
112         }
113         return defaultValue;
114     }
115
116 }