http proxy settings need a different connector
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / client / impl / DmaapClientUtil.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *
21  *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
23
24 import java.util.Properties;
25 import javax.ws.rs.client.Client;
26 import javax.ws.rs.client.ClientBuilder;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.client.WebTarget;
29 import javax.ws.rs.core.Response;
30 import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
31 import org.glassfish.jersey.client.ClientConfig;
32 import org.glassfish.jersey.client.ClientProperties;
33 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
34
35 public class DmaapClientUtil {
36
37         private static final String MR_AUTH_CONSTANT = "X-CambriaAuth";
38         private static final String MR_DATE_CONSTANT = "X-CambriaDate";
39         private static final String[] httpClientProperties = { ClientProperties.CONNECT_TIMEOUT,
40                         ClientProperties.READ_TIMEOUT, ClientProperties.PROXY_USERNAME, ClientProperties.PROXY_PASSWORD,
41                         ClientProperties.PROXY_URI };
42
43         public static ClientConfig getClientConfig(Properties properties) {
44                 ClientConfig config = new ClientConfig();
45                 if (properties != null && !properties.isEmpty()) {
46                         setHttpClientProperties(config, properties);
47                 }
48                 return config;
49         }
50
51         private static void setHttpClientProperties(ClientConfig config, Properties properties) {
52                 for (int i = 0; i < httpClientProperties.length; i++) {
53                         if ((properties.getProperty(httpClientProperties[i]) != null)) {
54                                 config.property(httpClientProperties[i], properties.getProperty(httpClientProperties[i]));
55                         }
56                 }
57                 if ((properties.getProperty(ClientProperties.PROXY_URI) != null) &&
58                         !(properties.getProperty(ClientProperties.PROXY_URI).isEmpty())) {
59                     config.connectorProvider(new ApacheConnectorProvider());
60                 } // else the default connectorProvider (HttpConnectorProvider) will be used
61
62         }
63
64         public static WebTarget getTarget(ClientConfig config, final String path, final String username,
65                         final String password) {
66                 Client client = null;
67                 if (config != null) {
68                         client = ClientBuilder.newClient(config);
69                 } else {
70                         client = ClientBuilder.newClient();
71                 }
72                 HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal(username, password);
73                 client.register(feature);
74
75                 return client.target(path);
76         }
77
78         public static WebTarget getTarget(ClientConfig config, final String path) {
79
80                 Client client = null;
81                 if (config != null&&config.getProperties().size()>0) {
82                         client = ClientBuilder.newClient(config);
83                 } else {
84                         client = ClientBuilder.newClient();
85                 }
86                 return client.target(path);
87         }
88
89         public static Response getResponsewtCambriaAuth(WebTarget target, String username, String password) {
90                 return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password).get();
91
92         }
93
94         public static Response postResponsewtCambriaAuth(WebTarget target, String username, String password, byte[] data,
95                         String contentType) {
96                 return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password)
97                                 .post(Entity.entity(data, contentType));
98
99         }
100
101         public static Response getResponsewtBasicAuth(WebTarget target, String authHeader) {
102
103                 return target.request().header("Authorization", "Basic " + authHeader).get();
104
105         }
106
107         public static Response postResponsewtBasicAuth(WebTarget target, String authHeader, byte[] data,
108                         String contentType) {
109
110                 return target.request().header("Authorization", "Basic " + authHeader).post(Entity.entity(data, contentType));
111
112         }
113
114         public static Response getResponsewtNoAuth(WebTarget target) {
115
116                 return target.request().get();
117
118         }
119
120         public static Response postResponsewtNoAuth(WebTarget target, byte[] data, String contentType) {
121                 return target.request().post(Entity.entity(data, contentType));
122
123         }
124
125 }