89fe8505ab500f8e63d3cde6f884050e14ba7d84
[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     }
92
93     @Override
94     public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulOptions options,
95             RestfulAsyncCallback callback) throws ServiceException {
96
97     }
98
99     @Override
100     public RestfulResponse post(String servicePath, RestfulParametes restParametes) throws ServiceException {
101         return null;
102     }
103
104     @Override
105     public RestfulResponse post(String servicePath, RestfulParametes restParametes, RestfulOptions options)
106             throws ServiceException {
107         return null;
108     }
109
110     @Override
111     public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
112             throws ServiceException {
113
114     }
115
116     @Override
117     public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulOptions options,
118             RestfulAsyncCallback callback) throws ServiceException {
119
120     }
121
122     @Override
123     public RestfulResponse delete(String servicePath, RestfulParametes restParametes) throws ServiceException {
124         return null;
125     }
126
127     @Override
128     public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
129             throws ServiceException {
130
131     }
132
133     @Override
134     public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulOptions options,
135             RestfulAsyncCallback callback) throws ServiceException {
136
137     }
138
139     @Override
140     public RestfulResponse patch(String servicePath, RestfulParametes restParametes) throws ServiceException {
141         return null;
142     }
143
144     @Override
145     public RestfulResponse patch(String servicePath, RestfulParametes restParametes, RestfulOptions options)
146             throws ServiceException {
147         return null;
148     }
149
150     @Override
151     public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback)
152             throws ServiceException {
153
154     }
155
156     @Override
157     public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulOptions options,
158             RestfulAsyncCallback callback) throws ServiceException {
159
160     }
161
162     @Override
163     public RestfulResponse get(String servicePath, RestfulParametes restParametes, RestfulOptions option)
164             throws ServiceException {
165         ContentExchange exchange = new ContentExchange(true);
166         exchange.setURL(servicePath);
167         exchange.setMethod("GET");
168         restParametes.getHeaderMap().entrySet().stream()
169                 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
170
171         try {
172             client.send(exchange);
173         } catch(IOException e) {
174             LOG.error("IOException", e);
175         }
176         try {
177             int exchangeState = exchange.waitForDone();
178             if(exchangeState == HttpExchange.STATUS_COMPLETED) {
179                 String res = exchange.getResponseContent();
180                 LOG.info(res);
181
182                 RestfulResponse restfulResponse = new RestfulResponse();
183                 restfulResponse.setResponseJson(exchange.getResponseContent());
184                 restfulResponse.setStatus(exchange.getResponseStatus());
185                 return restfulResponse;
186             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
187                 throw new ServiceException(
188                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
189             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
190                 throw new ServiceException(
191                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
192             }
193         } catch(InterruptedException e) {
194             LOG.error("InterruptedException", e);
195             // Restore interrupted state...
196             Thread.currentThread().interrupt();
197         } catch(UnsupportedEncodingException e) {
198             LOG.error("UnsupportedEncodingException", e);
199         }
200         return null;
201     }
202
203     @Override
204     public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options)
205             throws ServiceException {
206         ContentExchange exchange = new ContentExchange(true);
207         exchange.setURL(servicePath);
208         exchange.setMethod("PUT");
209         exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
210
211         restParametes.getHeaderMap().entrySet().stream()
212                 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
213
214         try {
215             client.send(exchange);
216         } catch(IOException e) {
217             LOG.error("IOException", e);
218         }
219
220         try {
221             int exchangeState = exchange.waitForDone();
222             if(exchangeState == HttpExchange.STATUS_COMPLETED) {
223                 String res = exchange.getResponseContent();
224                 LOG.info(res);
225
226                 RestfulResponse restfulResponse = new RestfulResponse();
227                 restfulResponse.setResponseJson(exchange.getResponseContent());
228                 restfulResponse.setStatus(exchange.getResponseStatus());
229                 return restfulResponse;
230             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
231                 throw new ServiceException(
232                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
233             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
234                 throw new ServiceException(
235                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
236             }
237         } catch(InterruptedException e) {
238             LOG.error("InterruptedException", e);
239             // Restore interrupted state...
240             Thread.currentThread().interrupt();
241         } catch(UnsupportedEncodingException e) {
242             LOG.error("UnsupportedEncodingException", e);
243         }
244         return null;
245     }
246
247     @Override
248     public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options)
249             throws ServiceException {
250         ContentExchange exchange = new ContentExchange(true);
251
252         String encodeParams = RequestUtil.encodeParams(restParametes);
253         if(encodeParams.isEmpty()) {
254             exchange.setURL(servicePath);
255         } else {
256             exchange.setURL(servicePath + "?" + encodeParams);
257         }
258         exchange.setMethod("DELETE");
259         if(restParametes.getRawData() != null) {
260             exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
261         }
262
263         restParametes.getHeaderMap().entrySet().stream()
264                 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
265
266         try {
267             client.send(exchange);
268         } catch(IOException e) {
269             LOG.error("IOException", e);
270         }
271
272         try {
273             int exchangeState = exchange.waitForDone();
274             if(exchangeState == HttpExchange.STATUS_COMPLETED) {
275                 String res = exchange.getResponseContent();
276                 LOG.info(res);
277
278                 RestfulResponse restfulResponse = new RestfulResponse();
279                 restfulResponse.setResponseJson(exchange.getResponseContent());
280                 restfulResponse.setStatus(exchange.getResponseStatus());
281                 return restfulResponse;
282             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
283                 throw new ServiceException(
284                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
285             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
286                 throw new ServiceException(
287                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
288             }
289         } catch(InterruptedException e) {
290             LOG.error("InterruptedException", e);
291             // Restore interrupted state...
292             Thread.currentThread().interrupt();
293         } catch(UnsupportedEncodingException e) {
294             LOG.error("InterruptedException", e);
295         }
296         return null;
297     }
298 }