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 java.io.IOException;
28 import java.io.UnsupportedEncodingException;
29 import java.net.MalformedURLException;
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;
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,
59 String contentType) throws APPCException {
61 logger.info("Sending POST request to " + path);
66 URL serviceUrl = new URL(protocol, ip, port, path);
67 post = new HttpPost(serviceUrl.toExternalForm());
68 post.setHeader("Content-Type", contentType);
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);
77 logger.debug("Sending request " + post);
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();
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,
97 String contentType) throws APPCException {
99 logger.info("Sending PUT request to " + path);
104 URL serviceUrl = new URL(protocol, ip, port, path);
105 put = new HttpPut(serviceUrl.toExternalForm());
106 put.setHeader("Content-Type", contentType);
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);
115 logger.debug("Sending request " + put);
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();
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,
135 String contentType) throws APPCException {
137 logger.info("Sending GET request to " + path);
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);
149 logger.debug("Sending request " + get);
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();
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,
173 String contentType) throws APPCException {
175 logger.info("Sending DELETE request to " + path);
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);
187 logger.debug("Sending request " + delete);
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();
198 HttpResponse response = client.execute(delete);
199 httpCode = response.getStatusLine().getStatusCode();
200 } catch (IOException e) {
201 throw new APPCException(e);