Change nexus values to properties
[appc.git] / appc-common / src / main / java / org / openecomp / appc / util / httpClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.openecomp.appc.util;
23
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;
44
45 import java.io.IOException;
46 import java.io.UnsupportedEncodingException;
47 import java.net.MalformedURLException;
48 import java.net.URL;
49
50
51 public class httpClient {
52
53     private static final EELFLogger logger = EELFManager.getInstance().getLogger(httpClient.class);
54
55     private static Configuration configuration = ConfigurationFactory.getConfiguration();
56
57     @SuppressWarnings("deprecation")
58     public static int postMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
59
60         logger.info("Sending POST request to " + path);
61
62         HttpPost post;
63         try {
64
65             URL serviceUrl = new URL(protocol, ip, port, path);
66             post = new HttpPost(serviceUrl.toExternalForm());
67             post.setHeader("Content-Type", contentType);
68
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);
74         }
75
76         logger.debug("Sending request " + post);
77
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();
84
85         int httpCode;
86         try {
87             HttpResponse response = client.execute(post);
88             httpCode = response.getStatusLine().getStatusCode();
89         } catch (IOException e) {
90             throw new APPCException(e);
91         }
92         return httpCode;
93     }
94
95     @SuppressWarnings("deprecation")
96     public static int putMethod(String protocol, String ip, int port, String path, String payload, String contentType) throws APPCException {
97
98         logger.info("Sending PUT request to " + path);
99
100         HttpPut put;
101         try {
102
103             URL serviceUrl = new URL(protocol, ip, port, path);
104             put = new HttpPut(serviceUrl.toExternalForm());
105             put.setHeader("Content-Type", contentType);
106
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);
112         }
113
114         logger.debug("Sending request " + put);
115
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();
122
123         int httpCode;
124         try {
125             HttpResponse response = client.execute(put);
126             httpCode = response.getStatusLine().getStatusCode();
127         } catch (IOException e) {
128             throw new APPCException(e);
129         }
130         return httpCode;
131     }
132
133     @SuppressWarnings("deprecation")
134     public static String getMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
135
136         logger.info("Sending GET request to " + path);
137
138         HttpGet get;
139         try {
140
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);
146         }
147
148         logger.debug("Sending request " + get);
149
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();
156
157         int httpCode;
158         String result;
159
160         try {
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);
166         }
167
168         return result;
169     }
170
171     @SuppressWarnings("deprecation")
172     public static int deleteMethod(String protocol, String ip, int port, String path, String contentType) throws APPCException {
173
174         logger.info("Sending DELETE request to " + path);
175
176         HttpDelete delete;
177         try {
178
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);
184         }
185
186         logger.debug("Sending request " + delete);
187
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();
194
195         int httpCode;
196         String result;
197
198         try {
199             HttpResponse response = client.execute(delete);
200             httpCode = response.getStatusLine().getStatusCode();
201         } catch (IOException e) {
202             throw new APPCException(e);
203         }
204
205         return httpCode;
206     }
207 }