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