read and set the jersey client properties
[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
26 import javax.ws.rs.client.Client;
27 import javax.ws.rs.client.ClientBuilder;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.client.WebTarget;
30 import javax.ws.rs.core.Response;
31
32 import org.glassfish.jersey.client.ClientConfig;
33 import org.glassfish.jersey.client.ClientProperties;
34 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
35
36 public class DmaapClientUtil {
37
38         private static final String MR_AUTH_CONSTANT = "X-CambriaAuth";
39         private static final String MR_DATE_CONSTANT = "X-CambriaDate";
40         private static final String[] httpClientProperties = { ClientProperties.CONNECT_TIMEOUT,
41                         ClientProperties.READ_TIMEOUT, ClientProperties.PROXY_USERNAME, ClientProperties.PROXY_PASSWORD,
42                         ClientProperties.PROXY_URI };
43
44         public static ClientConfig getClientConfig(Properties properties) {
45                 ClientConfig config = new ClientConfig();
46                 if (properties != null && !properties.isEmpty()) {
47                         setHttpClientProperties(config, properties);
48                 }
49                 return config;
50         }
51
52         private static void setHttpClientProperties(ClientConfig config, Properties properties) {
53                 for (int i = 0; i < httpClientProperties.length; i++) {
54                         if ((properties.getProperty(httpClientProperties[i]) != null)) {
55                                 config.property(httpClientProperties[i], properties.getProperty(httpClientProperties[i]));
56                         }
57                 }
58
59         }
60
61         public static WebTarget getTarget(ClientConfig config, final String path, final String username,
62                         final String password) {
63                 Client client = null;
64                 if (config != null) {
65                         client = ClientBuilder.newClient(config);
66                 } else {
67                         client = ClientBuilder.newClient();
68                 }
69                 HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal(username, password);
70                 client.register(feature);
71
72                 return client.target(path);
73         }
74
75         public static WebTarget getTarget(ClientConfig config, final String path) {
76
77                 Client client = null;
78                 if (config != null&&config.getProperties().size()>0) {
79                         client = ClientBuilder.newClient(config);
80                 } else {
81                         client = ClientBuilder.newClient();
82                 }
83                 return client.target(path);
84         }
85
86         public static Response getResponsewtCambriaAuth(WebTarget target, String username, String password) {
87                 return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password).get();
88
89         }
90
91         public static Response postResponsewtCambriaAuth(WebTarget target, String username, String password, byte[] data,
92                         String contentType) {
93                 return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password)
94                                 .post(Entity.entity(data, contentType));
95
96         }
97
98         public static Response getResponsewtBasicAuth(WebTarget target, String authHeader) {
99
100                 return target.request().header("Authorization", "Basic " + authHeader).get();
101
102         }
103
104         public static Response postResponsewtBasicAuth(WebTarget target, String authHeader, byte[] data,
105                         String contentType) {
106
107                 return target.request().header("Authorization", "Basic " + authHeader).post(Entity.entity(data, contentType));
108
109         }
110
111         public static Response getResponsewtNoAuth(WebTarget target) {
112
113                 return target.request().get();
114
115         }
116
117         public static Response postResponsewtNoAuth(WebTarget target, byte[] data, String contentType) {
118                 return target.request().post(Entity.entity(data, contentType));
119
120         }
121
122 }