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