2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.util;
26 import java.io.IOException;
27 import java.io.UnsupportedEncodingException;
28 import java.net.MalformedURLException;
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;
50 public class httpClient {
52 private static final EELFLogger logger = EELFManager.getInstance().getLogger(httpClient.class);
54 private static Configuration configuration = ConfigurationFactory.getConfiguration();
56 @SuppressWarnings("deprecation")
57 public static int postMethod(String protocol, String ip, int port, String path, String payload,
58 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(new AuthScope(ip, port), new UsernamePasswordCredentials(
80 configuration.getProperty("username"), configuration.getProperty("password")));
81 CloseableHttpClient client =
82 HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
86 HttpResponse response = client.execute(post);
87 httpCode = response.getStatusLine().getStatusCode();
88 } catch (IOException e) {
89 throw new APPCException(e);
94 @SuppressWarnings("deprecation")
95 public static int putMethod(String protocol, String ip, int port, String path, String payload,
96 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(new AuthScope(ip, port), new UsernamePasswordCredentials(
118 configuration.getProperty("username"), configuration.getProperty("password")));
119 CloseableHttpClient client =
120 HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
124 HttpResponse response = client.execute(put);
125 httpCode = response.getStatusLine().getStatusCode();
126 } catch (IOException e) {
127 throw new APPCException(e);
132 @SuppressWarnings("deprecation")
133 public static String getMethod(String protocol, String ip, int port, String path,
134 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(new AuthScope(ip, port), new UsernamePasswordCredentials(
152 configuration.getProperty("username"), configuration.getProperty("password")));
153 CloseableHttpClient client =
154 HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
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);
170 @SuppressWarnings("deprecation")
171 public static int deleteMethod(String protocol, String ip, int port, String path,
172 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(new AuthScope(ip, port), new UsernamePasswordCredentials(
190 configuration.getProperty("username"), configuration.getProperty("password")));
191 CloseableHttpClient client =
192 HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
197 HttpResponse response = client.execute(delete);
198 httpCode = response.getStatusLine().getStatusCode();
199 } catch (IOException e) {
200 throw new APPCException(e);