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