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