Fix SVNFM jetty-all vulnerability fix
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / common / restclient / HttpRest.java
1 /*
2  * Copyright 2016 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.vnfm.svnfm.vnfmadapter.common.restclient;
18
19 import org.eclipse.jetty.http.HttpMethod;
20 import org.eclipse.jetty.util.thread.QueuedThreadPool;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24
25 /**
26  * <br/>
27  * <p>
28  * </p>
29  *
30  * @author
31  * @version Aug 9, 2016
32  */
33 public class HttpRest extends HttpBaseRest {
34
35     private static final Logger LOG = LoggerFactory.getLogger(HttpRest.class);
36
37
38     /**
39      * Initializing Rest options.<br/>
40      *
41      * @throws ServiceException
42      * @since
43      */
44     public void initHttpRest(final RestfulOptions option) throws ServiceException {
45         if (option == null) {
46             client = null;
47             throw new ServiceException("option is null.");
48         }
49         createHttpClient();
50         try {
51             int iValue;
52             iValue = option.getIntOption(RestfulClientConst.MAX_CONN_PER_ADDR_KEY_NAME);
53 //            // max 200 concurrent,connections to every address
54             client.setMaxConnectionsPerDestination(iValue);
55             iValue = option.getIntOption(RestfulClientConst.THREAD_KEY_NAME);
56 //            // max threads
57             client.setExecutor(new QueuedThreadPool(iValue));
58             iValue = option.getIntOption(RestfulClientConst.CONN_TIMEOUT_KEY_NAME);
59             client.setConnectTimeout(iValue);
60
61             iValue = option.getRestTimeout();
62             defaultTimeout = iValue;
63             client.setIdleTimeout(iValue);
64             iValue = option.getIntOption(RestfulClientConst.IDLE_TIMEOUT_KEY_NAME);
65             client.setIdleTimeout(iValue);
66             client.start();
67             defaultIP = option.getStringOption(RestfulClientConst.HOST_KEY_NAME);
68             defaultPort = option.getIntOption(RestfulClientConst.PORT_KEY_NAME);
69         } catch (final Exception e) {
70             LOG.error("start httpclient error", e);
71             client = null;
72             throw new ServiceException("http client init failed.");
73         }
74     }
75
76     @Override
77     public RestfulResponse get(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
78         return this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, null);
79     }
80
81     @Override
82     public RestfulResponse get(final String servicePath, final RestfulParametes restParametes,
83                                final RestfulOptions option) throws ServiceException {
84         return this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, null);
85     }
86
87     @Override
88     public RestfulResponse head(final String servicePath, final RestfulParametes restParametes)
89             throws ServiceException {
90         return this.sendHttpRequest(HttpMethod.HEAD.asString(), servicePath, restParametes, null, null);
91     }
92
93     @Override
94     public RestfulResponse head(final String servicePath, final RestfulParametes restParametes,
95                                 final RestfulOptions option) throws ServiceException {
96         return this.sendHttpRequest(HttpMethod.HEAD.asString(), servicePath, restParametes, option, null);
97     }
98
99     @Override
100     public void asyncGet(final String servicePath, final RestfulParametes restParametes,
101                          final RestfulAsyncCallback callback) throws ServiceException {
102         if (callback == null) {
103             this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, new DefaultAsyncCallback());
104         } else {
105             this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, null, callback);
106         }
107     }
108
109     @Override
110     public void asyncGet(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
111                          final RestfulAsyncCallback callback) throws ServiceException {
112         if (callback == null) {
113             this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, new DefaultAsyncCallback());
114         } else {
115             this.sendHttpRequest(HttpMethod.GET.asString(), servicePath, restParametes, option, callback);
116         }
117     }
118
119     @Override
120     public RestfulResponse put(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
121         return this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, null);
122     }
123
124     @Override
125     public RestfulResponse put(final String servicePath, final RestfulParametes restParametes,
126                                final RestfulOptions option) throws ServiceException {
127         return this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, null);
128     }
129
130     @Override
131     public void asyncPut(final String servicePath, final RestfulParametes restParametes,
132                          final RestfulAsyncCallback callback) throws ServiceException {
133         if (callback == null) {
134             this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, new DefaultAsyncCallback());
135         } else {
136             this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, null, callback);
137         }
138     }
139
140     @Override
141     public void asyncPut(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
142                          final RestfulAsyncCallback callback) throws ServiceException {
143         if (callback == null) {
144             this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, new DefaultAsyncCallback());
145         } else {
146             this.sendHttpRequest(HttpMethod.PUT.asString(), servicePath, restParametes, option, callback);
147         }
148     }
149
150     @Override
151     public RestfulResponse post(final String servicePath, final RestfulParametes restParametes)
152             throws ServiceException {
153         return this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, null);
154     }
155
156     @Override
157     public RestfulResponse post(final String servicePath, final RestfulParametes restParametes,
158                                 final RestfulOptions option) throws ServiceException {
159         return this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, null);
160     }
161
162     @Override
163     public void asyncPost(final String servicePath, final RestfulParametes restParametes,
164                           final RestfulAsyncCallback callback) throws ServiceException {
165         if (callback == null) {
166             this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, new DefaultAsyncCallback());
167         } else {
168             this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, null, callback);
169         }
170     }
171
172     @Override
173     public void asyncPost(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
174                           final RestfulAsyncCallback callback) throws ServiceException {
175         if (callback == null) {
176             this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, new DefaultAsyncCallback());
177         } else {
178             this.sendHttpRequest(HttpMethod.POST.asString(), servicePath, restParametes, option, callback);
179         }
180     }
181
182     @Override
183     public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes)
184             throws ServiceException {
185         return this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, null);
186     }
187
188     @Override
189     public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes,
190                                   final RestfulOptions option) throws ServiceException {
191         return this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, null);
192     }
193
194     @Override
195     public void asyncDelete(final String servicePath, final RestfulParametes restParametes,
196                             final RestfulAsyncCallback callback) throws ServiceException {
197         if (callback == null) {
198             this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, new DefaultAsyncCallback());
199         } else {
200             this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, null, callback);
201         }
202     }
203
204     @Override
205     public void asyncDelete(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
206                             final RestfulAsyncCallback callback) throws ServiceException {
207         if (callback == null) {
208             this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, new DefaultAsyncCallback());
209         } else {
210             this.sendHttpRequest(HttpMethod.DELETE.asString(), servicePath, restParametes, option, callback);
211         }
212     }
213
214     @Override
215     public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes)
216             throws ServiceException {
217         return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, null);
218     }
219
220     @Override
221     public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes,
222                                  final RestfulOptions option) throws ServiceException {
223         return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, null);
224     }
225
226     @Override
227     public void asyncPatch(final String servicePath, final RestfulParametes restParametes,
228                            final RestfulAsyncCallback callback) throws ServiceException {
229         if (callback == null) {
230             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, new DefaultAsyncCallback());
231         } else {
232             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, callback);
233         }
234     }
235
236     @Override
237     public void asyncPatch(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
238                            final RestfulAsyncCallback callback) throws ServiceException {
239         if (callback == null) {
240             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, new DefaultAsyncCallback());
241         } else {
242             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, callback);
243         }
244     }
245
246 }