9b670ce6e58809c7035fdb7589bd031e6086570c
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.resmanagement.common.util.restclient;
18
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
21
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.eclipse.jetty.client.ContentExchange;
25 import org.eclipse.jetty.client.HttpClient;
26 import org.eclipse.jetty.client.HttpExchange;
27 import org.eclipse.jetty.io.ByteArrayBuffer;
28 import org.eclipse.jetty.util.ssl.SslContextFactory;
29 import org.eclipse.jetty.util.thread.QueuedThreadPool;
30 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
31
32 public class HttpsRest extends HttpBaseRest {
33
34     private static final Logger LOG = LogManager.getLogger(HttpsRest.class);
35     public static final String IOEXCEPTION = "IOException";
36
37     public void initHttpsRest() {
38         SslContextFactory sslContextFactory = new SslContextFactory();
39         client = new HttpClient(sslContextFactory);
40         client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
41         client.setMaxConnectionsPerAddress(200); // max 200 concurrent connections to every address
42         client.setThreadPool(new QueuedThreadPool(250)); // max 250 threads
43         client.setTimeout(30000); // 30 seconds timeout; if no server reply, the request expires
44         try {
45             client.start();
46         } catch(Exception e) {
47             LOG.error("Exception", e);
48         }
49     }
50
51     @Override
52     public RestfulResponse get(String servicePath, RestfulParametes restParametes) throws ServiceException {
53         return null;
54     }
55
56     @Override
57     public RestfulResponse head(String servicePath, RestfulParametes restParametes, RestfulOptions options)
58             throws ServiceException {
59         return null;
60     }
61
62     @Override
63     public RestfulResponse head(String servicePath, RestfulParametes restParametes) throws ServiceException {
64         return null;
65     }
66
67     @Override
68     public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
69             throws ServiceException {
70         
71         //This functionality has not been implemented yet
72
73     }
74
75     @Override
76     public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulOptions options,
77             RestfulAsyncCallback callback) throws ServiceException {
78
79         //This functionality has not been implemented yet
80         
81     }
82
83     @Override
84     public RestfulResponse put(String servicePath, RestfulParametes restParametes) throws ServiceException {
85         return null;
86     }
87
88     @Override
89     public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
90             throws ServiceException {
91         
92         //This functionality has not been implemented yet
93         
94     }
95
96     @Override
97     public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulOptions options,
98             RestfulAsyncCallback callback) throws ServiceException {
99         
100         //This functionality has not been implemented yet
101
102     }
103
104     @Override
105     public RestfulResponse post(String servicePath, RestfulParametes restParametes) throws ServiceException {
106         return null;
107     }
108
109     @Override
110     public RestfulResponse post(String servicePath, RestfulParametes restParametes, RestfulOptions options)
111             throws ServiceException {
112         return null;
113     }
114
115     @Override
116     public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
117             throws ServiceException {
118         
119         //This functionality has not been implemented yet
120         
121     }
122
123     @Override
124     public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulOptions options,
125             RestfulAsyncCallback callback) throws ServiceException {
126         
127         //This functionality has not been implemented yet
128         
129     }
130
131     @Override
132     public RestfulResponse delete(String servicePath, RestfulParametes restParametes) throws ServiceException {
133         return null;
134     }
135
136     @Override
137     public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
138             throws ServiceException {
139         
140         //This functionality has not been implemented yet
141         
142     }
143
144     @Override
145     public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulOptions options,
146             RestfulAsyncCallback callback) throws ServiceException {
147
148         //This functionality has not been implemented yet
149         
150     }
151
152     @Override
153     public RestfulResponse patch(String servicePath, RestfulParametes restParametes) throws ServiceException {
154         return null;
155     }
156
157     @Override
158     public RestfulResponse patch(String servicePath, RestfulParametes restParametes, RestfulOptions options)
159             throws ServiceException {
160         return null;
161     }
162
163     @Override
164     public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
165             throws ServiceException {
166         
167         //This functionality has not been implemented yet
168         
169     }
170
171     @Override
172     public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulOptions options,
173             RestfulAsyncCallback callback) throws ServiceException {
174         
175         //This functionality has not been implemented yet
176         
177     }
178
179     @Override
180     public RestfulResponse get(String servicePath, RestfulParametes restParametes, RestfulOptions option)
181             throws ServiceException {
182         ContentExchange exchange = new ContentExchange(true);
183         exchange.setURL(servicePath);
184         exchange.setMethod("GET");
185         restParametes.getHeaderMap().entrySet().stream()
186                 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
187
188         try {
189             client.send(exchange);
190         } catch(IOException e) {
191             LOG.error(IOEXCEPTION, e);
192         }
193         try {
194             int exchangeState = exchange.waitForDone();
195             if(exchangeState == HttpExchange.STATUS_COMPLETED) {
196                 String res = exchange.getResponseContent();
197                 LOG.info(res);
198
199                 RestfulResponse restfulResponse = new RestfulResponse();
200                 restfulResponse.setResponseJson(exchange.getResponseContent());
201                 restfulResponse.setStatus(exchange.getResponseStatus());
202                 return restfulResponse;
203             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
204                 throw new ServiceException(
205                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
206             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
207                 throw new ServiceException(
208                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
209             }
210         } catch(InterruptedException e) {
211             LOG.error("InterruptedException", e);
212             // Restore interrupted state...
213             Thread.currentThread().interrupt();
214         } catch(UnsupportedEncodingException e) {
215             LOG.error("UnsupportedEncodingException", e);
216         }
217         return null;
218     }
219
220     @Override
221     public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options)
222             throws ServiceException {
223         ContentExchange exchange = new ContentExchange(true);
224         exchange.setURL(servicePath);
225         exchange.setMethod("PUT");
226         exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
227
228         restParametes.getHeaderMap().entrySet().stream()
229                 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
230
231         try {
232             client.send(exchange);
233         } catch(IOException e) {
234             LOG.error(IOEXCEPTION, e);
235         }
236
237         try {
238             int exchangeState = exchange.waitForDone();
239             if(exchangeState == HttpExchange.STATUS_COMPLETED) {
240                 String res = exchange.getResponseContent();
241                 LOG.info(res);
242
243                 RestfulResponse restfulResponse = new RestfulResponse();
244                 restfulResponse.setResponseJson(exchange.getResponseContent());
245                 restfulResponse.setStatus(exchange.getResponseStatus());
246                 return restfulResponse;
247             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
248                 throw new ServiceException(
249                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
250             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
251                 throw new ServiceException(
252                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
253             }
254         } catch(InterruptedException e) {
255             LOG.error("InterruptedException", e);
256             // Restore interrupted state...
257             Thread.currentThread().interrupt();
258         } catch(UnsupportedEncodingException e) {
259             LOG.error("UnsupportedEncodingException", e);
260         }
261         return null;
262     }
263
264     @Override
265     public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options)
266             throws ServiceException {
267         ContentExchange exchange = new ContentExchange(true);
268
269         String encodeParams = RequestUtil.encodeParams(restParametes);
270         if(encodeParams.isEmpty()) {
271             exchange.setURL(servicePath);
272         } else {
273             exchange.setURL(servicePath + "?" + encodeParams);
274         }
275         exchange.setMethod("DELETE");
276         if(restParametes.getRawData() != null) {
277             exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
278         }
279
280         restParametes.getHeaderMap().entrySet().stream()
281                 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
282
283         try {
284             client.send(exchange);
285         } catch(IOException e) {
286             LOG.error(IOEXCEPTION, e);
287         }
288
289         try {
290             int exchangeState = exchange.waitForDone();
291             if(exchangeState == HttpExchange.STATUS_COMPLETED) {
292                 String res = exchange.getResponseContent();
293                 LOG.info(res);
294
295                 RestfulResponse restfulResponse = new RestfulResponse();
296                 restfulResponse.setResponseJson(exchange.getResponseContent());
297                 restfulResponse.setStatus(exchange.getResponseStatus());
298                 return restfulResponse;
299             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
300                 throw new ServiceException(
301                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
302             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
303                 throw new ServiceException(
304                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
305             }
306         } catch(InterruptedException e) {
307             LOG.error("InterruptedException", e);
308             // Restore interrupted state...
309             Thread.currentThread().interrupt();
310         } catch(UnsupportedEncodingException e) {
311             LOG.error("InterruptedException", e);
312         }
313         return null;
314     }
315 }