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;
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;
32 import org.glassfish.jersey.client.ClientConfig;
33 import org.glassfish.jersey.client.ClientProperties;
34 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
36 public class DmaapClientUtil {
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 };
44 public static ClientConfig getClientConfig(Properties properties) {
45 ClientConfig config = new ClientConfig();
46 if (properties != null && !properties.isEmpty()) {
47 setHttpClientProperties(config, properties);
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]));
61 public static WebTarget getTarget(ClientConfig config, final String path, final String username,
62 final String password) {
65 client = ClientBuilder.newClient(config);
67 client = ClientBuilder.newClient();
69 HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal(username, password);
70 client.register(feature);
72 return client.target(path);
75 public static WebTarget getTarget(ClientConfig config, final String path) {
78 if (config != null&&config.getProperties().size()>0) {
79 client = ClientBuilder.newClient(config);
81 client = ClientBuilder.newClient();
83 return client.target(path);
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();
91 public static Response postResponsewtCambriaAuth(WebTarget target, String username, String password, byte[] data,
93 return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password)
94 .post(Entity.entity(data, contentType));
98 public static Response getResponsewtBasicAuth(WebTarget target, String authHeader) {
100 return target.request().header("Authorization", "Basic " + authHeader).get();
104 public static Response postResponsewtBasicAuth(WebTarget target, String authHeader, byte[] data,
105 String contentType) {
107 return target.request().header("Authorization", "Basic " + authHeader).post(Entity.entity(data, contentType));
111 public static Response getResponsewtNoAuth(WebTarget target) {
113 return target.request().get();
117 public static Response postResponsewtNoAuth(WebTarget target, byte[] data, String contentType) {
118 return target.request().post(Entity.entity(data, contentType));