[DMAAP-CLIENT] First sonar issues review part2
[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  *  Modifications Copyright © 2021 Orange.
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  *        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  *******************************************************************************/
24
25 package org.onap.dmaap.mr.client.impl;
26
27 import java.util.Properties;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.ClientBuilder;
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.client.WebTarget;
32 import javax.ws.rs.core.Response;
33
34 import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
35 import org.glassfish.jersey.client.ClientConfig;
36 import org.glassfish.jersey.client.ClientProperties;
37 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
38
39 public class DmaapClientUtil {
40
41     private DmaapClientUtil() {
42
43     }
44
45     private static final String MR_AUTH_CONSTANT = "X-CambriaAuth";
46     private static final String MR_DATE_CONSTANT = "X-CambriaDate";
47     private static final String[] httpClientProperties = {ClientProperties.CONNECT_TIMEOUT,
48         ClientProperties.READ_TIMEOUT, ClientProperties.PROXY_USERNAME, ClientProperties.PROXY_PASSWORD,
49         ClientProperties.PROXY_URI};
50
51     public static ClientConfig getClientConfig(Properties properties) {
52         ClientConfig config = new ClientConfig();
53         if (properties != null && !properties.isEmpty()) {
54             setHttpClientProperties(config, properties);
55         }
56         return config;
57     }
58
59     private static void setHttpClientProperties(ClientConfig config, Properties properties) {
60         for (String httpClientProperty : httpClientProperties) {
61             if ((properties.getProperty(httpClientProperty) != null)) {
62                 config.property(httpClientProperty, properties.getProperty(httpClientProperty));
63             }
64         }
65         if ((properties.getProperty(ClientProperties.PROXY_URI) != null)
66                 && !(properties.getProperty(ClientProperties.PROXY_URI).isEmpty())) {
67             config.connectorProvider(new ApacheConnectorProvider());
68         } // else the default connectorProvider (HttpConnectorProvider) will be used
69
70     }
71
72     public static WebTarget getTarget(ClientConfig config, final String path, final String username,
73                                       final String password) {
74         Client client = null;
75         if (config != null) {
76             client = ClientBuilder.newClient(config);
77         } else {
78             client = ClientBuilder.newClient();
79         }
80         HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal(username, password);
81         client.register(feature);
82
83         return client.target(path);
84     }
85
86     public static WebTarget getTarget(ClientConfig config, final String path) {
87
88         Client client = null;
89         if (config != null && config.getProperties().size() > 0) {
90             client = ClientBuilder.newClient(config);
91         } else {
92             client = ClientBuilder.newClient();
93         }
94         return client.target(path);
95     }
96
97     public static Response getResponsewtCambriaAuth(WebTarget target, String username, String password) {
98         return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password).get();
99
100     }
101
102     public static Response postResponsewtCambriaAuth(WebTarget target, String username, String password, byte[] data,
103                                                      String contentType) {
104         return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password)
105                 .post(Entity.entity(data, contentType));
106
107     }
108
109     public static Response getResponsewtBasicAuth(WebTarget target, String authHeader) {
110
111         return target.request().header("Authorization", "Basic " + authHeader).get();
112
113     }
114
115     public static Response postResponsewtBasicAuth(WebTarget target, String authHeader, byte[] data,
116                                                    String contentType) {
117
118         return target.request().header("Authorization", "Basic " + authHeader).post(Entity.entity(data, contentType));
119
120     }
121
122     public static Response getResponsewtNoAuth(WebTarget target) {
123
124         return target.request().get();
125
126     }
127
128     public static Response postResponsewtNoAuth(WebTarget target, byte[] data, String contentType) {
129         return target.request().post(Entity.entity(data, contentType));
130
131     }
132
133 }