66b8f5f82fc52ad37ee03a02de8b19d42ca5205d
[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         } catch(UnsupportedEncodingException e) {
192             LOG.error("UnsupportedEncodingException", e);
193         }
194         return null;
195     }
196
197     @Override
198     public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options)
199             throws ServiceException {
200         ContentExchange exchange = new ContentExchange(true);
201         exchange.setURL(servicePath);
202         exchange.setMethod("PUT");
203         exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
204
205         restParametes.getHeaderMap().entrySet().stream()
206                 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
207
208         try {
209             client.send(exchange);
210         } catch(IOException e) {
211             LOG.error("IOException", e);
212         }
213
214         try {
215             int exchangeState = exchange.waitForDone();
216             if(exchangeState == HttpExchange.STATUS_COMPLETED) {
217                 String res = exchange.getResponseContent();
218                 LOG.info(res);
219
220                 RestfulResponse restfulResponse = new RestfulResponse();
221                 restfulResponse.setResponseJson(exchange.getResponseContent());
222                 restfulResponse.setStatus(exchange.getResponseStatus());
223                 return restfulResponse;
224             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
225                 throw new ServiceException(
226                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
227             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
228                 throw new ServiceException(
229                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
230             }
231         } catch(InterruptedException e) {
232             LOG.error("InterruptedException", e);
233             // Restore interrupted state...
234             Thread.currentThread().interrupt();
235         } catch(UnsupportedEncodingException e) {
236             LOG.error("UnsupportedEncodingException", e);
237         }
238         return null;
239     }
240
241     @Override
242     public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options)
243             throws ServiceException {
244         ContentExchange exchange = new ContentExchange(true);
245
246         String encodeParams = RequestUtil.encodeParams(restParametes);
247         if(encodeParams.isEmpty()) {
248             exchange.setURL(servicePath);
249         } else {
250             exchange.setURL(servicePath + "?" + encodeParams);
251         }
252         exchange.setMethod("DELETE");
253         if(restParametes.getRawData() != null) {
254             exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
255         }
256
257         restParametes.getHeaderMap().entrySet().stream()
258                 .forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
259
260         try {
261             client.send(exchange);
262         } catch(IOException e) {
263             LOG.error("IOException", e);
264         }
265
266         try {
267             int exchangeState = exchange.waitForDone();
268             if(exchangeState == HttpExchange.STATUS_COMPLETED) {
269                 String res = exchange.getResponseContent();
270                 LOG.info(res);
271
272                 RestfulResponse restfulResponse = new RestfulResponse();
273                 restfulResponse.setResponseJson(exchange.getResponseContent());
274                 restfulResponse.setStatus(exchange.getResponseStatus());
275                 return restfulResponse;
276             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
277                 throw new ServiceException(
278                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
279             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
280                 throw new ServiceException(
281                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
282             }
283         } catch(InterruptedException e) {
284             LOG.error("InterruptedException", e);
285             // Restore interrupted state...
286             Thread.currentThread().interrupt();
287         } catch(UnsupportedEncodingException e) {
288             LOG.error("InterruptedException", e);
289         }
290         return null;
291     }
292 }