2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.rest.client;
28 import java.io.IOException;
29 import java.io.UnsupportedEncodingException;
30 import java.net.MalformedURLException;
32 import org.apache.commons.codec.binary.Base64;
33 import org.apache.http.HttpHeaders;
34 import org.apache.http.HttpResponse;
35 import org.apache.http.client.HttpClient;
36 import org.apache.http.client.methods.HttpGet;
37 import org.apache.http.client.methods.HttpPost;
38 import org.apache.http.client.methods.HttpPut;
39 import org.apache.http.entity.StringEntity;
40 import org.apache.http.impl.client.CloseableHttpClient;
41 import org.onap.appc.exceptions.APPCException;
42 import org.onap.appc.util.HttpClientUtil;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
46 @SuppressWarnings("deprecation")
47 public class RestClientInvoker {
49 private static final EELFLogger LOG = EELFManager.getInstance().getLogger(RestClientInvoker.class);
50 private static final String OPERATION_APPLICATION_JSON = " application/json";
51 private static final String BASIC = "Basic ";
53 private URL url = null;
54 private String basicAuth = null;
56 public RestClientInvoker(URL url) {
61 * Sets the basic authentication header for the given user and password. If either entry is null
62 * then does not set basic auth
64 * @param user The user with optional domain name (for AAF)
65 * @param password The password for the user
67 public void setAuthentication(String user, String password) {
68 if (user != null && password != null) {
69 String authStr = user + ":" + password;
70 basicAuth = new String(Base64.encodeBase64(authStr.getBytes()));
74 public HttpResponse doPost(String path, String body) throws APPCException {
79 URL postUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
80 post = new HttpPost(postUrl.toExternalForm());
81 post.setHeader(HttpHeaders.CONTENT_TYPE, OPERATION_APPLICATION_JSON);
82 post.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
84 if (basicAuth != null) {
85 post.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
88 StringEntity entity = new StringEntity(body);
89 entity.setContentType(OPERATION_APPLICATION_JSON);
90 post.setEntity(new StringEntity(body));
91 } catch (MalformedURLException | UnsupportedEncodingException e) {
92 throw new APPCException(e);
94 HttpClient client = HttpClientUtil.getHttpClient(url.getProtocol());
97 return client.execute(post);
98 } catch (IOException e) {
99 throw new APPCException(e);
104 * This is Generic method that can be used to perform REST Put operation
106 * @param path - path for put
107 * @param body - payload for put action which will be sent as request body.
108 * @return - HttpResponse object which is returned from put REST call.
109 * @throws APPCException when error occurs
111 public HttpResponse doPut(String path, String body) throws APPCException {
114 URL putUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
115 put = new HttpPut(putUrl.toExternalForm());
116 put.setHeader(HttpHeaders.CONTENT_TYPE, OPERATION_APPLICATION_JSON);
117 put.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
119 if (basicAuth != null) {
120 put.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
123 StringEntity entity = new StringEntity(body);
124 entity.setContentType(OPERATION_APPLICATION_JSON);
125 put.setEntity(new StringEntity(body));
126 } catch (UnsupportedEncodingException | MalformedURLException e) {
127 throw new APPCException(e);
130 HttpClient client = HttpClientUtil.getHttpClient(url.getProtocol());
133 return client.execute(put);
134 } catch (IOException e) {
135 throw new APPCException(e);
139 public HttpResponse doGet(String path) throws APPCException {
142 URL getUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
143 get = new HttpGet(getUrl.toExternalForm());
144 get.setHeader(HttpHeaders.CONTENT_TYPE, OPERATION_APPLICATION_JSON);
145 get.setHeader(HttpHeaders.ACCEPT, OPERATION_APPLICATION_JSON);
147 if (basicAuth != null) {
148 get.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
151 } catch (Exception e) {
152 throw new APPCException(e);
155 try (CloseableHttpClient client = HttpClientUtil.getHttpClient(url.getProtocol())) {
156 return client.execute(get);
157 } catch (IOException e) {
158 throw new APPCException(e);