1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
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;
35 public class DmaapClientUtil {
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 };
43 public static ClientConfig getClientConfig(Properties properties) {
44 ClientConfig config = new ClientConfig();
45 if (properties != null && !properties.isEmpty()) {
46 setHttpClientProperties(config, properties);
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]));
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
64 public static WebTarget getTarget(ClientConfig config, final String path, final String username,
65 final String password) {
68 client = ClientBuilder.newClient(config);
70 client = ClientBuilder.newClient();
72 HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal(username, password);
73 client.register(feature);
75 return client.target(path);
78 public static WebTarget getTarget(ClientConfig config, final String path) {
81 if (config != null&&config.getProperties().size()>0) {
82 client = ClientBuilder.newClient(config);
84 client = ClientBuilder.newClient();
86 return client.target(path);
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();
94 public static Response postResponsewtCambriaAuth(WebTarget target, String username, String password, byte[] data,
96 return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password)
97 .post(Entity.entity(data, contentType));
101 public static Response getResponsewtBasicAuth(WebTarget target, String authHeader) {
103 return target.request().header("Authorization", "Basic " + authHeader).get();
107 public static Response postResponsewtBasicAuth(WebTarget target, String authHeader, byte[] data,
108 String contentType) {
110 return target.request().header("Authorization", "Basic " + authHeader).post(Entity.entity(data, contentType));
114 public static Response getResponsewtNoAuth(WebTarget target) {
116 return target.request().get();
120 public static Response postResponsewtNoAuth(WebTarget target, byte[] data, String contentType) {
121 return target.request().post(Entity.entity(data, contentType));