1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
25 package org.onap.dmaap.mr.client.impl;
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;
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;
39 public class DmaapClientUtil {
41 private DmaapClientUtil() {
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};
51 public static ClientConfig getClientConfig(Properties properties) {
52 ClientConfig config = new ClientConfig();
53 if (properties != null && !properties.isEmpty()) {
54 setHttpClientProperties(config, properties);
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));
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
72 public static WebTarget getTarget(ClientConfig config, final String path, final String username,
73 final String password) {
76 client = ClientBuilder.newClient(config);
78 client = ClientBuilder.newClient();
80 HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal(username, password);
81 client.register(feature);
83 return client.target(path);
86 public static WebTarget getTarget(ClientConfig config, final String path) {
89 if (config != null && config.getProperties().size() > 0) {
90 client = ClientBuilder.newClient(config);
92 client = ClientBuilder.newClient();
94 return client.target(path);
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();
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));
109 public static Response getResponsewtBasicAuth(WebTarget target, String authHeader) {
111 return target.request().header("Authorization", "Basic " + authHeader).get();
115 public static Response postResponsewtBasicAuth(WebTarget target, String authHeader, byte[] data,
116 String contentType) {
118 return target.request().header("Authorization", "Basic " + authHeader).post(Entity.entity(data, contentType));
122 public static Response getResponsewtNoAuth(WebTarget target) {
124 return target.request().get();
128 public static Response postResponsewtNoAuth(WebTarget target, byte[] data, String contentType) {
129 return target.request().post(Entity.entity(data, contentType));