summaryrefslogtreecommitdiffstats
path: root/appc-common/src/main/java/org/onap/appc/util/httpClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'appc-common/src/main/java/org/onap/appc/util/httpClient.java')
-rw-r--r--appc-common/src/main/java/org/onap/appc/util/httpClient.java210
1 files changed, 210 insertions, 0 deletions
diff --git a/appc-common/src/main/java/org/onap/appc/util/httpClient.java b/appc-common/src/main/java/org/onap/appc/util/httpClient.java
new file mode 100644
index 000000000..edb27c6f6
--- /dev/null
+++ b/appc-common/src/main/java/org/onap/appc/util/httpClient.java
@@ -0,0 +1,210 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Copyright (C) 2017 Amdocs
+ * =============================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.appc.util;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.BasicCredentialsProvider;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.onap.appc.configuration.Configuration;
+import org.onap.appc.configuration.ConfigurationFactory;
+import org.onap.appc.exceptions.APPCException;
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+
+public class httpClient {
+
+ private static final EELFLogger logger = EELFManager.getInstance().getLogger(httpClient.class);
+
+ private static Configuration configuration = ConfigurationFactory.getConfiguration();
+
+ @SuppressWarnings("deprecation")
+ public static int postMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
+
+ logger.info("Sending POST request to " + path);
+
+ HttpPost post;
+ try {
+
+ URL serviceUrl = new URL(protocol, ip, port, path);
+ post = new HttpPost(serviceUrl.toExternalForm());
+ post.setHeader("Content-Type", contentType);
+
+ StringEntity entity = new StringEntity(payload);
+ entity.setContentType(contentType);
+ post.setEntity(new StringEntity(payload));
+ } catch (UnsupportedEncodingException | MalformedURLException e) {
+ throw new APPCException(e);
+ }
+
+ logger.debug("Sending request " + post);
+
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope(ip, port),
+ new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
+ CloseableHttpClient client = HttpClients.custom()
+ .setDefaultCredentialsProvider(credsProvider).build();
+
+ int httpCode;
+ try {
+ HttpResponse response = client.execute(post);
+ httpCode = response.getStatusLine().getStatusCode();
+ } catch (IOException e) {
+ throw new APPCException(e);
+ }
+ return httpCode;
+ }
+
+ @SuppressWarnings("deprecation")
+ public static int putMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
+
+ logger.info("Sending PUT request to " + path);
+
+ HttpPut put;
+ try {
+
+ URL serviceUrl = new URL(protocol, ip, port, path);
+ put = new HttpPut(serviceUrl.toExternalForm());
+ put.setHeader("Content-Type", contentType);
+
+ StringEntity entity = new StringEntity(payload);
+ entity.setContentType(contentType);
+ put.setEntity(new StringEntity(payload));
+ } catch (UnsupportedEncodingException | MalformedURLException e) {
+ throw new APPCException(e);
+ }
+
+ logger.debug("Sending request " + put);
+
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope(ip, port),
+ new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
+ CloseableHttpClient client = HttpClients.custom()
+ .setDefaultCredentialsProvider(credsProvider).build();
+
+ int httpCode;
+ try {
+ HttpResponse response = client.execute(put);
+ httpCode = response.getStatusLine().getStatusCode();
+ } catch (IOException e) {
+ throw new APPCException(e);
+ }
+ return httpCode;
+ }
+
+ @SuppressWarnings("deprecation")
+ public static String getMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
+
+ logger.info("Sending GET request to " + path);
+
+ HttpGet get;
+ try {
+
+ URL serviceUrl = new URL(protocol, ip, port, path);
+ get = new HttpGet(serviceUrl.toExternalForm());
+ get.setHeader("Content-Type", contentType);
+ } catch (MalformedURLException e) {
+ throw new APPCException(e);
+ }
+
+ logger.debug("Sending request " + get);
+
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope(ip, port),
+ new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
+ CloseableHttpClient client = HttpClients.custom()
+ .setDefaultCredentialsProvider(credsProvider).build();
+
+ int httpCode;
+ String result;
+
+ try {
+ HttpResponse response = client.execute(get);
+ httpCode = response.getStatusLine().getStatusCode();
+ result = (httpCode == HttpStatus.SC_OK) ? response.getEntity().toString() : null;
+ } catch (IOException e) {
+ throw new APPCException(e);
+ }
+
+ return result;
+ }
+
+ @SuppressWarnings("deprecation")
+ public static int deleteMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
+
+ logger.info("Sending DELETE request to " + path);
+
+ HttpDelete delete;
+ try {
+
+ URL serviceUrl = new URL(protocol, ip, port, path);
+ delete = new HttpDelete(serviceUrl.toExternalForm());
+ delete.setHeader("Content-Type", contentType);
+ } catch (MalformedURLException e) {
+ throw new APPCException(e);
+ }
+
+ logger.debug("Sending request " + delete);
+
+ CredentialsProvider credsProvider = new BasicCredentialsProvider();
+ credsProvider.setCredentials(
+ new AuthScope(ip, port),
+ new UsernamePasswordCredentials(configuration.getProperty("username"), configuration.getProperty("password")));
+ CloseableHttpClient client = HttpClients.custom()
+ .setDefaultCredentialsProvider(credsProvider).build();
+
+ int httpCode;
+ String result;
+
+ try {
+ HttpResponse response = client.execute(delete);
+ httpCode = response.getStatusLine().getStatusCode();
+ } catch (IOException e) {
+ throw new APPCException(e);
+ }
+
+ return httpCode;
+ }
+}