a2ce8af2afcbdb32b726fa3ddec06cb2bf2ff74f
[appc.git] / appc-core / appc-common-bundle / java / org / onap / appc / util / httpClient.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  * 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.util;
25
26 import java.io.IOException;
27 import java.io.UnsupportedEncodingException;
28 import java.net.MalformedURLException;
29 import java.net.URL;
30 import org.apache.http.HttpResponse;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.auth.AuthScope;
33 import org.apache.http.auth.UsernamePasswordCredentials;
34 import org.apache.http.client.CredentialsProvider;
35 import org.apache.http.client.methods.HttpDelete;
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.BasicCredentialsProvider;
41 import org.apache.http.impl.client.CloseableHttpClient;
42 import org.apache.http.impl.client.HttpClients;
43 import org.onap.appc.configuration.Configuration;
44 import org.onap.appc.configuration.ConfigurationFactory;
45 import org.onap.appc.exceptions.APPCException;
46 import com.att.eelf.configuration.EELFLogger;
47 import com.att.eelf.configuration.EELFManager;
48
49
50 public class httpClient {
51
52     private static final EELFLogger logger = EELFManager.getInstance().getLogger(httpClient.class);
53
54     private static Configuration configuration = ConfigurationFactory.getConfiguration();
55
56     @SuppressWarnings("deprecation")
57     public static int postMethod(String protocol, String ip, int port, String path, String payload,
58             String contentType) throws APPCException {
59
60         logger.info("Sending POST request to " + path);
61
62         HttpPost post;
63         try {
64
65             URL serviceUrl = new URL(protocol, ip, port, path);
66             post = new HttpPost(serviceUrl.toExternalForm());
67             post.setHeader("Content-Type", contentType);
68
69             StringEntity entity = new StringEntity(payload);
70             entity.setContentType(contentType);
71             post.setEntity(new StringEntity(payload));
72         } catch (UnsupportedEncodingException | MalformedURLException e) {
73             throw new APPCException(e);
74         }
75
76         logger.debug("Sending request " + post);
77
78         CredentialsProvider credsProvider = new BasicCredentialsProvider();
79         credsProvider.setCredentials(new AuthScope(ip, port), new UsernamePasswordCredentials(
80                 configuration.getProperty("username"), configuration.getProperty("password")));
81         CloseableHttpClient client =
82                 HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
83
84         int httpCode;
85         try {
86             HttpResponse response = client.execute(post);
87             httpCode = response.getStatusLine().getStatusCode();
88         } catch (IOException e) {
89             throw new APPCException(e);
90         }
91         return httpCode;
92     }
93
94     @SuppressWarnings("deprecation")
95     public static int putMethod(String protocol, String ip, int port, String path, String payload,
96             String contentType) throws APPCException {
97
98         logger.info("Sending PUT request to " + path);
99
100         HttpPut put;
101         try {
102
103             URL serviceUrl = new URL(protocol, ip, port, path);
104             put = new HttpPut(serviceUrl.toExternalForm());
105             put.setHeader("Content-Type", contentType);
106
107             StringEntity entity = new StringEntity(payload);
108             entity.setContentType(contentType);
109             put.setEntity(new StringEntity(payload));
110         } catch (UnsupportedEncodingException | MalformedURLException e) {
111             throw new APPCException(e);
112         }
113
114         logger.debug("Sending request " + put);
115
116         CredentialsProvider credsProvider = new BasicCredentialsProvider();
117         credsProvider.setCredentials(new AuthScope(ip, port), new UsernamePasswordCredentials(
118                 configuration.getProperty("username"), configuration.getProperty("password")));
119         CloseableHttpClient client =
120                 HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
121
122         int httpCode;
123         try {
124             HttpResponse response = client.execute(put);
125             httpCode = response.getStatusLine().getStatusCode();
126         } catch (IOException e) {
127             throw new APPCException(e);
128         }
129         return httpCode;
130     }
131
132     @SuppressWarnings("deprecation")
133     public static String getMethod(String protocol, String ip, int port, String path,
134             String contentType) throws APPCException {
135
136         logger.info("Sending GET request to " + path);
137
138         HttpGet get;
139         try {
140
141             URL serviceUrl = new URL(protocol, ip, port, path);
142             get = new HttpGet(serviceUrl.toExternalForm());
143             get.setHeader("Content-Type", contentType);
144         } catch (MalformedURLException e) {
145             throw new APPCException(e);
146         }
147
148         logger.debug("Sending request " + get);
149
150         CredentialsProvider credsProvider = new BasicCredentialsProvider();
151         credsProvider.setCredentials(new AuthScope(ip, port), new UsernamePasswordCredentials(
152                 configuration.getProperty("username"), configuration.getProperty("password")));
153         CloseableHttpClient client =
154                 HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
155
156         int httpCode;
157         String result;
158
159         try {
160             HttpResponse response = client.execute(get);
161             httpCode = response.getStatusLine().getStatusCode();
162             result = (httpCode == HttpStatus.SC_OK) ? response.getEntity().toString() : null;
163         } catch (IOException e) {
164             throw new APPCException(e);
165         }
166
167         return result;
168     }
169
170     @SuppressWarnings("deprecation")
171     public static int deleteMethod(String protocol, String ip, int port, String path,
172             String contentType) throws APPCException {
173
174         logger.info("Sending DELETE request to " + path);
175
176         HttpDelete delete;
177         try {
178
179             URL serviceUrl = new URL(protocol, ip, port, path);
180             delete = new HttpDelete(serviceUrl.toExternalForm());
181             delete.setHeader("Content-Type", contentType);
182         } catch (MalformedURLException e) {
183             throw new APPCException(e);
184         }
185
186         logger.debug("Sending request " + delete);
187
188         CredentialsProvider credsProvider = new BasicCredentialsProvider();
189         credsProvider.setCredentials(new AuthScope(ip, port), new UsernamePasswordCredentials(
190                 configuration.getProperty("username"), configuration.getProperty("password")));
191         CloseableHttpClient client =
192                 HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
193
194         int httpCode;
195
196         try {
197             HttpResponse response = client.execute(delete);
198             httpCode = response.getStatusLine().getStatusCode();
199         } catch (IOException e) {
200             throw new APPCException(e);
201         }
202
203         return httpCode;
204     }
205 }