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