d99e51578382e588497c3ccb8264f42aa41efe65
[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
20 import org.eclipse.jetty.client.ContentExchange;
21 import org.eclipse.jetty.client.HttpClient;
22 import org.eclipse.jetty.client.HttpExchange;
23 import org.eclipse.jetty.io.ByteArrayBuffer;
24 import org.eclipse.jetty.util.ssl.SslContextFactory;
25 import org.eclipse.jetty.util.thread.QueuedThreadPool;
26 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
27 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
28 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.HttpBaseRest;
29 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestHttpContentExchange;
30 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulAsyncCallback;
31 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulOptions;
32 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
33 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulResponse;
34 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import javax.ws.rs.core.Request;
39 import javax.ws.rs.core.UriBuilder;
40 import java.io.IOException;
41 import java.io.UnsupportedEncodingException;
42 import java.net.URLEncoder;
43 import java.util.Map;
44
45 public class HttpsRest extends HttpBaseRest {
46
47     private static final Logger LOG = LoggerFactory.getLogger(HttpsRest.class);
48
49     public void initHttpsRest() {
50         SslContextFactory sslContextFactory = new SslContextFactory();
51         client = new HttpClient(sslContextFactory);
52         client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
53         client.setMaxConnectionsPerAddress(200); // max 200 concurrent connections to every address
54         client.setThreadPool(new QueuedThreadPool(250)); // max 250 threads
55         client.setTimeout(30000); // 30 seconds timeout; if no server reply, the request expires
56         try {
57             client.start();
58         } catch (Exception e) {
59             e.printStackTrace();
60         }
61     }
62
63     @Override
64     public RestfulResponse get(String servicePath, RestfulParametes restParametes) throws ServiceException {
65         return null;
66     }
67
68     @Override
69     public RestfulResponse head(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
70         return null;
71     }
72
73     @Override
74     public RestfulResponse head(String servicePath, RestfulParametes restParametes) throws ServiceException {
75         return null;
76     }
77
78     @Override
79     public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
80
81     }
82
83     @Override
84     public void asyncGet(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
85
86     }
87
88     @Override
89     public RestfulResponse put(String servicePath, RestfulParametes restParametes) throws ServiceException {
90         return null;
91     }
92
93     @Override
94     public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
95
96     }
97
98     @Override
99     public void asyncPut(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
100
101     }
102
103     @Override
104     public RestfulResponse post(String servicePath, RestfulParametes restParametes) throws ServiceException {
105         return null;
106     }
107
108     @Override
109     public RestfulResponse post(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
110         return null;
111     }
112
113     @Override
114     public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
115
116     }
117
118     @Override
119     public void asyncPost(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
120
121     }
122
123     @Override
124     public RestfulResponse delete(String servicePath, RestfulParametes restParametes) throws ServiceException {
125         return null;
126     }
127
128     @Override
129     public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
130
131     }
132
133     @Override
134     public void asyncDelete(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
135
136     }
137
138     @Override
139     public RestfulResponse patch(String servicePath, RestfulParametes restParametes) throws ServiceException {
140         return null;
141     }
142
143     @Override
144     public RestfulResponse patch(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
145         return null;
146     }
147
148     @Override
149     public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulAsyncCallback callback) throws ServiceException {
150
151     }
152
153     @Override
154     public void asyncPatch(String servicePath, RestfulParametes restParametes, RestfulOptions options, RestfulAsyncCallback callback) throws ServiceException {
155
156     }
157
158     @Override
159     public RestfulResponse get(String servicePath, RestfulParametes restParametes, RestfulOptions option) throws ServiceException {
160         ContentExchange exchange = new ContentExchange(true);
161         exchange.setURL(servicePath);
162         exchange.setMethod("GET");
163         restParametes.getHeaderMap().entrySet().stream().forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
164
165         try {
166             client.send(exchange);
167         } catch (IOException e) {
168             e.printStackTrace();
169         }
170         try {
171             int exchangeState = exchange.waitForDone();
172             if (exchangeState == HttpExchange.STATUS_COMPLETED) {
173                 String res = exchange.getResponseContent();
174                 LOG.info(res);
175
176                 RestfulResponse restfulResponse = new RestfulResponse();
177                 restfulResponse.setResponseJson(exchange.getResponseContent());
178                 restfulResponse.setStatus(exchange.getStatus());
179                 return restfulResponse;
180             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
181                 throw new ServiceException(
182                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
183             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
184                 throw new ServiceException(
185                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
186             }
187         } catch (InterruptedException e) {
188             e.printStackTrace();
189         } catch (UnsupportedEncodingException e) {
190             e.printStackTrace();
191         }
192         return null;
193     }
194
195     @Override
196     public RestfulResponse put(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
197         ContentExchange exchange = new ContentExchange(true);
198         exchange.setURL(servicePath);
199         exchange.setMethod("PUT");
200         exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
201
202         restParametes.getHeaderMap().entrySet().stream().forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
203
204         try {
205             client.send(exchange);
206         } catch (IOException e) {
207             e.printStackTrace();
208         }
209
210         try {
211             int exchangeState = exchange.waitForDone();
212             if (exchangeState == HttpExchange.STATUS_COMPLETED) {
213                 String res = exchange.getResponseContent();
214                 LOG.info(res);
215
216                 RestfulResponse restfulResponse = new RestfulResponse();
217                 restfulResponse.setResponseJson(exchange.getResponseContent());
218                 restfulResponse.setStatus(exchange.getStatus());
219                 return restfulResponse;
220             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
221                 throw new ServiceException(
222                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
223             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
224                 throw new ServiceException(
225                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
226             }
227         } catch (InterruptedException e) {
228             e.printStackTrace();
229         } catch (UnsupportedEncodingException e) {
230             e.printStackTrace();
231         }
232         return null;
233     }
234
235     @Override
236     public RestfulResponse delete(String servicePath, RestfulParametes restParametes, RestfulOptions options) throws ServiceException {
237         ContentExchange exchange = new ContentExchange(true);
238
239         String encodeParams = RequestUtil.encodeParams(restParametes);
240         if (encodeParams.isEmpty()) {
241             exchange.setURL(servicePath);
242         } else {
243             exchange.setURL(servicePath + "?" + encodeParams);
244         }
245         exchange.setMethod("DELETE");
246         if (restParametes.getRawData() != null) {
247             exchange.setRequestContent(new ByteArrayBuffer(restParametes.getRawData()));
248         }
249
250         restParametes.getHeaderMap().entrySet().stream().forEach(entry -> exchange.setRequestHeader(entry.getKey(), entry.getValue()));
251
252         try {
253             client.send(exchange);
254         } catch (IOException e) {
255             e.printStackTrace();
256         }
257
258         try {
259             int exchangeState = exchange.waitForDone();
260             if (exchangeState == HttpExchange.STATUS_COMPLETED) {
261                 String res = exchange.getResponseContent();
262                 LOG.info(res);
263
264                 RestfulResponse restfulResponse = new RestfulResponse();
265                 restfulResponse.setResponseJson(exchange.getResponseContent());
266                 restfulResponse.setStatus(exchange.getStatus());
267                 return restfulResponse;
268             } else if(exchangeState == HttpExchange.STATUS_EXCEPTED) {
269                 throw new ServiceException(
270                         "request is exception: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXCEPTED));
271             } else if(exchangeState == HttpExchange.STATUS_EXPIRED) {
272                 throw new ServiceException(
273                         "request is expierd: " + RestHttpContentExchange.toState(HttpExchange.STATUS_EXPIRED));
274             }
275         } catch (InterruptedException e) {
276             e.printStackTrace();
277         } catch (UnsupportedEncodingException e) {
278             e.printStackTrace();
279         }
280         return null;
281     }
282 }