2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.dal.rest;
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;
33 import javax.net.ssl.HostnameVerifier;
34 import javax.net.ssl.SSLContext;
35 import javax.net.ssl.SSLSession;
37 import org.openecomp.sparky.security.SecurityContextFactory;
38 import org.openecomp.sparky.security.SecurityContextFactoryImpl;
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.
51 public class RestClientBuilder {
54 * TODO: implement fluent interface?
57 private boolean useHttps;
58 private boolean validateServerHostname;
59 private int connectTimeoutInMs;
60 private int readTimeoutInMs;
61 protected SecurityContextFactory sslContextFactory;
64 * Instantiates a new rest client builder.
66 public RestClientBuilder() {
67 validateServerHostname = false;
68 connectTimeoutInMs = 60000;
69 readTimeoutInMs = 60000;
71 sslContextFactory = new SecurityContextFactoryImpl();
74 public SecurityContextFactory getSslContextFactory() {
75 return sslContextFactory;
78 public void setSslContextFactory(SecurityContextFactory sslContextFactory) {
79 this.sslContextFactory = sslContextFactory;
82 public boolean isUseHttps() {
86 public void setUseHttps(boolean useHttps) {
87 this.useHttps = useHttps;
90 public int getConnectTimeoutInMs() {
91 return connectTimeoutInMs;
94 public void setConnectTimeoutInMs(int connectTimeoutInMs) {
95 this.connectTimeoutInMs = connectTimeoutInMs;
98 public int getReadTimeoutInMs() {
99 return readTimeoutInMs;
102 public void setReadTimeoutInMs(int readTimeoutInMs) {
103 this.readTimeoutInMs = readTimeoutInMs;
106 public boolean isValidateServerHostname() {
107 return validateServerHostname;
110 public void setValidateServerHostname(boolean validateServerHostname) {
111 this.validateServerHostname = validateServerHostname;
114 public Client getClient() throws Exception {
116 Client client = null;
117 ClientConfig clientConfig = new DefaultClientConfig();
120 SSLContext sslContext = sslContextFactory.getSecureContext();
122 if (validateServerHostname) {
124 clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
125 new HTTPSProperties(null, sslContext));
128 clientConfig.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
129 new HTTPSProperties(new HostnameVerifier() {
131 public boolean verify(String string, SSLSession sslSession) {
139 client = Client.create(clientConfig);
141 client.setConnectTimeout(connectTimeoutInMs);
142 client.setReadTimeout(readTimeoutInMs);