cc6f06a4d84ceee0d5b38e388779ba557b5fe3fe
[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 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;
16
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;
23
24     private static final Logger logger = LoggerFactory.getLogger(AbstractHttpAdapter.class);
25
26     public AbstractHttpAdapter() {
27         clientBuilder = ClientBuilder.newBuilder();
28         setTimeouts();
29         defaultHostNameVerifier();
30     }
31     
32     private void defaultHostNameVerifier() {
33         clientBuilder.hostnameVerifier(new HostnameVerifier() {
34             @Override
35             public boolean verify(String hostname, SSLSession session) {
36                 return true;
37             }
38         });
39     }
40
41     protected void enableMetricLogging() {
42         clientBuilder.register(new MetricLogClientFilter());
43     }
44
45     protected void enablePayloadLogging() {
46         clientBuilder.register(new PayloadLoggingClientFilter());
47     }
48
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);
52
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);
56
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);
61     }
62
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);
68         }
69         if (propDir == null || propDir.length() < 1) {
70             propDir = SDNC_CONFIG_DIR_DEFAULT;
71         }
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;
76         }
77         String path = propDir + propertiesFileName;
78         properties.load(new FileInputStream(path));
79         logger.trace("Initialized properties from ({}) properties ({})", path, properties);
80         return properties;
81     }
82
83     protected void addBasicAuthCredentials(String username, String password) {
84         String basicAuthValue = getBasicAuthValue(username,password);
85         clientBuilder.register(new BasicAuthFilter(basicAuthValue));
86     }
87
88     protected String getBasicAuthValue(String userName, String password) {
89         String token = userName + ":" + password;
90         try {
91             return "Basic " + Base64.getEncoder().encodeToString(token.getBytes());
92         } catch (Exception e) {
93             logger.error("getBasicAuthValue threw an exception, credentials will be null", e);
94         }
95         return null;
96     }
97
98     public ClientBuilder getClientBuilder() {
99         return clientBuilder;
100     }
101
102     private Integer readOptionalInteger(String propertyName, Integer defaultValue) {
103         String stringValue = System.getProperty(propertyName);
104         if (stringValue != null && stringValue.length() > 0) {
105             try {
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);
109             }
110         }
111         return defaultValue;
112     }
113
114 }