2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.util;
24 import org.apache.http.HttpResponse;
25 import org.apache.http.HttpStatus;
26 import org.apache.http.auth.AuthScope;
27 import org.apache.http.auth.UsernamePasswordCredentials;
28 import org.apache.http.client.CredentialsProvider;
29 import org.apache.http.client.HttpClient;
30 import org.apache.http.client.methods.HttpDelete;
31 import org.apache.http.client.methods.HttpGet;
32 import org.apache.http.client.methods.HttpPost;
33 import org.apache.http.client.methods.HttpPut;
34 import org.apache.http.entity.StringEntity;
35 import org.apache.http.impl.client.BasicCredentialsProvider;
36 import org.apache.http.impl.client.CloseableHttpClient;
37 import org.apache.http.impl.client.DefaultHttpClient;
38 import org.apache.http.impl.client.HttpClients;
39 import org.openecomp.appc.configuration.Configuration;
40 import org.openecomp.appc.configuration.ConfigurationFactory;
41 import org.openecomp.appc.exceptions.APPCException;
42 import com.att.eelf.configuration.EELFLogger;
43 import com.att.eelf.configuration.EELFManager;
45 import java.io.IOException;
46 import java.io.UnsupportedEncodingException;
47 import java.net.MalformedURLException;
51 public class httpClient {
53 private static final EELFLogger logger = EELFManager.getInstance().getLogger(httpClient.class);
55 private static Configuration configuration = ConfigurationFactory.getConfiguration();
57 @SuppressWarnings("deprecation")
58 public static int postMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
60 logger.info("Sending POST request to " + path);
65 URL serviceUrl = new URL(protocol, ip, port, path);
66 post = new HttpPost(serviceUrl.toExternalForm());
67 post.setHeader("Content-Type", contentType);
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);
76 logger.debug("Sending request " + post);
78 CredentialsProvider credsProvider = new BasicCredentialsProvider();
79 credsProvider.setCredentials(
80 new AuthScope(ip, port),
81 new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
82 CloseableHttpClient client = HttpClients.custom()
83 .setDefaultCredentialsProvider(credsProvider).build();
87 HttpResponse response = client.execute(post);
88 httpCode = response.getStatusLine().getStatusCode();
89 } catch (IOException e) {
90 throw new APPCException(e);
95 @SuppressWarnings("deprecation")
96 public static int putMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
98 logger.info("Sending PUT request to " + path);
103 URL serviceUrl = new URL(protocol, ip, port, path);
104 put = new HttpPut(serviceUrl.toExternalForm());
105 put.setHeader("Content-Type", contentType);
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);
114 logger.debug("Sending request " + put);
116 CredentialsProvider credsProvider = new BasicCredentialsProvider();
117 credsProvider.setCredentials(
118 new AuthScope(ip, port),
119 new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
120 CloseableHttpClient client = HttpClients.custom()
121 .setDefaultCredentialsProvider(credsProvider).build();
125 HttpResponse response = client.execute(put);
126 httpCode = response.getStatusLine().getStatusCode();
127 } catch (IOException e) {
128 throw new APPCException(e);
133 @SuppressWarnings("deprecation")
134 public static String getMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
136 logger.info("Sending GET request to " + path);
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);
148 logger.debug("Sending request " + get);
150 CredentialsProvider credsProvider = new BasicCredentialsProvider();
151 credsProvider.setCredentials(
152 new AuthScope(ip, port),
153 new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
154 CloseableHttpClient client = HttpClients.custom()
155 .setDefaultCredentialsProvider(credsProvider).build();
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);
171 @SuppressWarnings("deprecation")
172 public static int deleteMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
174 logger.info("Sending DELETE request to " + path);
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);
186 logger.debug("Sending request " + delete);
188 CredentialsProvider credsProvider = new BasicCredentialsProvider();
189 credsProvider.setCredentials(
190 new AuthScope(ip, port),
191 new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
192 CloseableHttpClient client = HttpClients.custom()
193 .setDefaultCredentialsProvider(credsProvider).build();
199 HttpResponse response = client.execute(delete);
200 httpCode = response.getStatusLine().getStatusCode();
201 } catch (IOException e) {
202 throw new APPCException(e);