Added test case for RestClient Invoker
[appc.git] / appc-core / appc-common-bundle / src / main / java / org / onap / appc / rest / client / RestClientInvoker.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
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.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.rest.client;
27
28 import java.io.IOException;
29 import java.io.UnsupportedEncodingException;
30 import java.net.MalformedURLException;
31 import java.net.URL;
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;
45
46 @SuppressWarnings("deprecation")
47 public class RestClientInvoker {
48
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 ";
52
53     private URL url = null;
54     private String basicAuth = null;
55
56     public RestClientInvoker(URL url) {
57         this.url = url;
58     }
59
60     /**
61      * Sets the basic authentication header for the given user and password. If either entry is null
62      * then does not set basic auth
63      *
64      * @param user The user with optional domain name (for AAF)
65      * @param password The password for the user
66      */
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()));
71         }
72     }
73
74     public HttpResponse doPost(String path, String body) throws APPCException {
75         HttpPost post;
76
77         try {
78
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);
83
84             if (basicAuth != null) {
85                 post.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
86             }
87
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);
93         }
94         HttpClient client = HttpClientUtil.getHttpClient(url.getProtocol());
95
96         try {
97             return client.execute(post);
98         } catch (IOException e) {
99             throw new APPCException(e);
100         }
101     }
102
103     /**
104      * This is Generic method that can be used to perform REST Put operation
105      *
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
110      */
111     public HttpResponse doPut(String path, String body) throws APPCException {
112         HttpPut put;
113         try {
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);
118
119             if (basicAuth != null) {
120                 put.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
121             }
122
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);
128         }
129
130         HttpClient client = HttpClientUtil.getHttpClient(url.getProtocol());
131
132         try {
133             return client.execute(put);
134         } catch (IOException e) {
135             throw new APPCException(e);
136         }
137     }
138
139     public HttpResponse doGet(String path) throws APPCException {
140         HttpGet get;
141         try {
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);
146
147             if (basicAuth != null) {
148                 get.setHeader(HttpHeaders.AUTHORIZATION, BASIC + basicAuth);
149             }
150
151         } catch (Exception e) {
152             throw new APPCException(e);
153         }
154
155         try (CloseableHttpClient client = HttpClientUtil.getHttpClient(url.getProtocol())) {
156             return client.execute(get);
157         } catch (IOException e) {
158             throw new APPCException(e);
159         }
160     }
161
162
163 }