Third part of onap rename
[appc.git] / appc-common / src / main / java / org / onap / appc / util / httpClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.util;
26
27 import org.apache.http.HttpResponse;
28 import org.apache.http.HttpStatus;
29 import org.apache.http.auth.AuthScope;
30 import org.apache.http.auth.UsernamePasswordCredentials;
31 import org.apache.http.client.CredentialsProvider;
32 import org.apache.http.client.HttpClient;
33 import org.apache.http.client.methods.HttpDelete;
34 import org.apache.http.client.methods.HttpGet;
35 import org.apache.http.client.methods.HttpPost;
36 import org.apache.http.client.methods.HttpPut;
37 import org.apache.http.entity.StringEntity;
38 import org.apache.http.impl.client.BasicCredentialsProvider;
39 import org.apache.http.impl.client.CloseableHttpClient;
40 import org.apache.http.impl.client.DefaultHttpClient;
41 import org.apache.http.impl.client.HttpClients;
42 import org.onap.appc.configuration.Configuration;
43 import org.onap.appc.configuration.ConfigurationFactory;
44 import org.onap.appc.exceptions.APPCException;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47
48 import java.io.IOException;
49 import java.io.UnsupportedEncodingException;
50 import java.net.MalformedURLException;
51 import java.net.URL;
52
53
54 public class httpClient {
55
56     private static final EELFLogger logger = EELFManager.getInstance().getLogger(httpClient.class);
57
58     private static Configuration configuration = ConfigurationFactory.getConfiguration();
59
60     @SuppressWarnings("deprecation")
61     public static int postMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
62
63         logger.info("Sending POST request to " + path);
64
65         HttpPost post;
66         try {
67
68             URL serviceUrl = new URL(protocol, ip, port, path);
69             post = new HttpPost(serviceUrl.toExternalForm());
70             post.setHeader("Content-Type", contentType);
71
72             StringEntity entity = new StringEntity(payload);
73             entity.setContentType(contentType);
74             post.setEntity(new StringEntity(payload));
75         } catch (UnsupportedEncodingException | MalformedURLException e) {
76             throw new APPCException(e);
77         }
78
79         logger.debug("Sending request " + post);
80
81         CredentialsProvider credsProvider = new BasicCredentialsProvider();
82         credsProvider.setCredentials(
83                 new AuthScope(ip, port),
84                 new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
85         CloseableHttpClient client = HttpClients.custom()
86                 .setDefaultCredentialsProvider(credsProvider).build();
87
88         int httpCode;
89         try {
90             HttpResponse response = client.execute(post);
91             httpCode = response.getStatusLine().getStatusCode();
92         } catch (IOException e) {
93             throw new APPCException(e);
94         }
95         return httpCode;
96     }
97
98     @SuppressWarnings("deprecation")
99     public static int putMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
100
101         logger.info("Sending PUT request to " + path);
102
103         HttpPut put;
104         try {
105
106             URL serviceUrl = new URL(protocol, ip, port, path);
107             put = new HttpPut(serviceUrl.toExternalForm());
108             put.setHeader("Content-Type", contentType);
109
110             StringEntity entity = new StringEntity(payload);
111             entity.setContentType(contentType);
112             put.setEntity(new StringEntity(payload));
113         } catch (UnsupportedEncodingException | MalformedURLException e) {
114             throw new APPCException(e);
115         }
116
117         logger.debug("Sending request " + put);
118
119         CredentialsProvider credsProvider = new BasicCredentialsProvider();
120         credsProvider.setCredentials(
121                 new AuthScope(ip, port),
122                 new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
123         CloseableHttpClient client = HttpClients.custom()
124                 .setDefaultCredentialsProvider(credsProvider).build();
125
126         int httpCode;
127         try {
128             HttpResponse response = client.execute(put);
129             httpCode = response.getStatusLine().getStatusCode();
130         } catch (IOException e) {
131             throw new APPCException(e);
132         }
133         return httpCode;
134     }
135
136     @SuppressWarnings("deprecation")
137     public static String getMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
138
139         logger.info("Sending GET request to " + path);
140
141         HttpGet get;
142         try {
143
144             URL serviceUrl = new URL(protocol, ip, port, path);
145             get = new HttpGet(serviceUrl.toExternalForm());
146             get.setHeader("Content-Type", contentType);
147         } catch (MalformedURLException e) {
148             throw new APPCException(e);
149         }
150
151         logger.debug("Sending request " + get);
152
153         CredentialsProvider credsProvider = new BasicCredentialsProvider();
154         credsProvider.setCredentials(
155                 new AuthScope(ip, port),
156                 new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
157         CloseableHttpClient client = HttpClients.custom()
158                 .setDefaultCredentialsProvider(credsProvider).build();
159
160         int httpCode;
161         String result;
162
163         try {
164             HttpResponse response = client.execute(get);
165             httpCode = response.getStatusLine().getStatusCode();
166             result = (httpCode == HttpStatus.SC_OK) ? response.getEntity().toString() : null;
167         } catch (IOException e) {
168             throw new APPCException(e);
169         }
170
171         return result;
172     }
173
174     @SuppressWarnings("deprecation")
175     public static int deleteMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
176
177         logger.info("Sending DELETE request to " + path);
178
179         HttpDelete delete;
180         try {
181
182             URL serviceUrl = new URL(protocol, ip, port, path);
183             delete = new HttpDelete(serviceUrl.toExternalForm());
184             delete.setHeader("Content-Type", contentType);
185         } catch (MalformedURLException e) {
186             throw new APPCException(e);
187         }
188
189         logger.debug("Sending request " + delete);
190
191         CredentialsProvider credsProvider = new BasicCredentialsProvider();
192         credsProvider.setCredentials(
193                 new AuthScope(ip, port),
194                 new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
195         CloseableHttpClient client = HttpClients.custom()
196                 .setDefaultCredentialsProvider(credsProvider).build();
197
198         int httpCode;
199         String result;
200
201         try {
202             HttpResponse response = client.execute(delete);
203             httpCode = response.getStatusLine().getStatusCode();
204         } catch (IOException e) {
205             throw new APPCException(e);
206         }
207
208         return httpCode;
209     }
210 }