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