2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.util;
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;
48 import java.io.IOException;
49 import java.io.UnsupportedEncodingException;
50 import java.net.MalformedURLException;
54 public class httpClient {
56 private static final EELFLogger logger = EELFManager.getInstance().getLogger(httpClient.class);
58 private static Configuration configuration = ConfigurationFactory.getConfiguration();
60 @SuppressWarnings("deprecation")
61 public static int postMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
63 logger.info("Sending POST request to " + path);
68 URL serviceUrl = new URL(protocol, ip, port, path);
69 post = new HttpPost(serviceUrl.toExternalForm());
70 post.setHeader("Content-Type", contentType);
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);
79 logger.debug("Sending request " + post);
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();
90 HttpResponse response = client.execute(post);
91 httpCode = response.getStatusLine().getStatusCode();
92 } catch (IOException e) {
93 throw new APPCException(e);
98 @SuppressWarnings("deprecation")
99 public static int putMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
101 logger.info("Sending PUT request to " + path);
106 URL serviceUrl = new URL(protocol, ip, port, path);
107 put = new HttpPut(serviceUrl.toExternalForm());
108 put.setHeader("Content-Type", contentType);
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);
117 logger.debug("Sending request " + put);
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();
128 HttpResponse response = client.execute(put);
129 httpCode = response.getStatusLine().getStatusCode();
130 } catch (IOException e) {
131 throw new APPCException(e);
136 @SuppressWarnings("deprecation")
137 public static String getMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
139 logger.info("Sending GET request to " + path);
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);
151 logger.debug("Sending request " + get);
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();
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);
174 @SuppressWarnings("deprecation")
175 public static int deleteMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
177 logger.info("Sending DELETE request to " + path);
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);
189 logger.debug("Sending request " + delete);
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();
202 HttpResponse response = client.execute(delete);
203 httpCode = response.getStatusLine().getStatusCode();
204 } catch (IOException e) {
205 throw new APPCException(e);