support for post requests with auth
[dmaap/messagerouter/dmaapclient.git] / src / main / java / com / att / nsa / 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 com.att.nsa.mr.client.impl;
23
24 import javax.ws.rs.client.Client;
25 import javax.ws.rs.client.ClientBuilder;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.client.WebTarget;
28 import javax.ws.rs.core.Response;
29
30 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
31
32 public class DmaapClientUtil {
33
34         private static final String MR_AUTH_CONSTANT = "X-CambriaAuth";
35         private static final String MR_DATE_CONSTANT = "X-CambriaDate";
36
37         public static WebTarget getTarget(final String path, final String username, final String password) {
38
39                 Client client = ClientBuilder.newClient();
40                 HttpAuthenticationFeature feature = HttpAuthenticationFeature.universal(username, password);
41                 client.register(feature);
42
43                 return client.target(path);
44         }
45
46         public static WebTarget getTarget(final String path) {
47
48                 Client client = ClientBuilder.newClient();
49                 return client.target(path);
50         }
51
52         public static Response getResponsewtCambriaAuth(WebTarget target, String username, String password) {
53                 return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password).get();
54
55         }
56         
57         public static Response postResponsewtCambriaAuth(WebTarget target, String username, String password,byte[] data,  String contentType) {
58                 return target.request().header(MR_AUTH_CONSTANT, username).header(MR_DATE_CONSTANT, password).post(Entity.entity(data, contentType));
59
60         }
61
62         public static Response getResponsewtBasicAuth(WebTarget target, String authHeader) {
63
64                 return target.request().header("Authorization", "Basic " + authHeader).get();
65
66         }
67         
68         public static Response postResponsewtBasicAuth(WebTarget target, String authHeader,byte[] data,String contentType) {
69
70                 return target.request().header("Authorization", "Basic " + authHeader).post(Entity.entity(data, contentType));
71
72         }
73
74         public static Response getResponsewtNoAuth(WebTarget target) {
75
76                 return target.request().get();
77
78         }
79         
80         public static Response postResponsewtNoAuth(WebTarget target, byte[] data, String contentType) {
81                 return target.request().post(Entity.entity(data, contentType));
82
83         }
84
85 }