Adding UI extensibility
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / dal / rest / RestClientBuilder.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.dal.rest;
24
25 import javax.net.ssl.HostnameVerifier;
26 import javax.net.ssl.SSLContext;
27 import javax.net.ssl.SSLSession;
28
29 import org.onap.aai.sparky.security.SecurityContextFactory;
30 import org.onap.aai.sparky.security.SecurityContextFactoryImpl;
31
32 import com.sun.jersey.api.client.Client;
33 import com.sun.jersey.api.client.config.ClientConfig;
34 import com.sun.jersey.api.client.config.DefaultClientConfig;
35 import com.sun.jersey.client.urlconnection.HTTPSProperties;
36
37 /**
38  * This is a generic REST Client builder with flexible security validation. Sometimes it's nice to
39  * be able to disable server chain cert validation and hostname validation to work-around lab
40  * issues, but at the same time be able to provide complete validation with client cert + hostname +
41  * server cert chain validation. I used the ModelLoader REST client as a base and merged in the TSUI
42  * client I wrote which also validates the server hostname and server certificate chain.
43  *
44  * @author DAVEA
45  *
46  */
47 public class RestClientBuilder {
48
49   /*
50    * TODO: implement fluent interface?
51    */
52
53   private boolean useHttps;
54   private boolean validateServerHostname;
55   private int connectTimeoutInMs;
56   private int readTimeoutInMs;
57   protected SecurityContextFactory sslContextFactory;
58
59   /**
60    * Instantiates a new rest client builder.
61    */
62   public RestClientBuilder() {
63     validateServerHostname = false;
64     connectTimeoutInMs = 60000;
65     readTimeoutInMs = 60000;
66     useHttps = true;
67     sslContextFactory = new SecurityContextFactoryImpl();
68   }
69
70   public SecurityContextFactory getSslContextFactory() {
71     return sslContextFactory;
72   }
73
74   public void setSslContextFactory(SecurityContextFactory sslContextFactory) {
75     this.sslContextFactory = sslContextFactory;
76   }
77
78   public boolean isUseHttps() {
79     return useHttps;
80   }
81
82   public void setUseHttps(boolean useHttps) {
83     this.useHttps = useHttps;
84   }
85
86   public int getConnectTimeoutInMs() {
87     return connectTimeoutInMs;
88   }
89
90   public void setConnectTimeoutInMs(int connectTimeoutInMs) {
91     this.connectTimeoutInMs = connectTimeoutInMs;
92   }
93
94   public int getReadTimeoutInMs() {
95     return readTimeoutInMs;
96   }
97
98   public void setReadTimeoutInMs(int readTimeoutInMs) {
99     this.readTimeoutInMs = readTimeoutInMs;
100   }
101
102   public boolean isValidateServerHostname() {
103     return validateServerHostname;
104   }
105
106   public void setValidateServerHostname(boolean validateServerHostname) {
107     this.validateServerHostname = validateServerHostname;
108   }
109
110   public Client getClient() throws Exception {
111
112     Client client = null;
113     ClientConfig clientConfig = new DefaultClientConfig();
114
115     if (useHttps) {
116       SSLContext sslContext = sslContextFactory.getSecureContext();
117
118       if (validateServerHostname) {
119
120         clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
121             new HTTPSProperties(null, sslContext));
122
123       } else {
124         clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
125             new HTTPSProperties(new HostnameVerifier() {
126               @Override
127               public boolean verify(String string, SSLSession sslSession) {
128                 return true;
129               }
130             }, sslContext));
131
132       }
133     }
134
135     client = Client.create(clientConfig);
136
137     client.setConnectTimeout(connectTimeoutInMs);
138     client.setReadTimeout(readTimeoutInMs);
139
140     return client;
141
142   }
143
144 }