7215424dfddc3f74ddb216faeaa416b9b787d956
[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.client.HttpClient;
20 import org.eclipse.jetty.http.HttpMethods;
21 import org.eclipse.jetty.util.thread.QueuedThreadPool;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
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      * Initializing Rest options.<br/>
39      * 
40      * @param options: rest options.
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.setMaxConnectionsPerAddress(iValue);
55
56             iValue = option.getIntOption(RestfulClientConst.THREAD_KEY_NAME);
57             // max threads
58             client.setThreadPool(new QueuedThreadPool(iValue));
59             iValue = option.getIntOption(RestfulClientConst.CONN_TIMEOUT_KEY_NAME);
60             client.setConnectTimeout(iValue);
61             iValue = option.getRestTimeout();
62             defaultTimeout = iValue;
63             client.setTimeout(iValue);
64
65             iValue = option.getIntOption(RestfulClientConst.IDLE_TIMEOUT_KEY_NAME);
66             client.setIdleTimeout(iValue);
67             iValue = option.getIntOption(RestfulClientConst.MAX_RESPONSE_HEADER_SIZE);
68             client.setResponseHeaderSize(iValue);
69             iValue = option.getIntOption(RestfulClientConst.MAX_REQUEST_HEADER_SIZE);
70             client.setRequestHeaderSize(iValue);
71             // HttpClient.CONNECTOR_SOCKET
72             client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
73             client.start();
74             defaultIP = option.getStringOption(RestfulClientConst.HOST_KEY_NAME);
75             defaultPort = option.getIntOption(RestfulClientConst.PORT_KEY_NAME);
76         } catch(final Exception e) {
77             LOG.error("start httpclient error", e);
78             client = null;
79             throw new ServiceException("http client init failed.");
80         }
81     }
82
83     @Override
84     public RestfulResponse get(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
85         return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, null);
86     }
87
88     @Override
89     public RestfulResponse get(final String servicePath, final RestfulParametes restParametes,
90             final RestfulOptions option) throws ServiceException {
91         return this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, null);
92     }
93
94     @Override
95     public RestfulResponse head(final String servicePath, final RestfulParametes restParametes)
96             throws ServiceException {
97         return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, null, null);
98     }
99
100     @Override
101     public RestfulResponse head(final String servicePath, final RestfulParametes restParametes,
102             final RestfulOptions option) throws ServiceException {
103         return this.sendHttpRequest(HttpMethods.HEAD, servicePath, restParametes, option, null);
104     }
105
106     @Override
107     public void asyncGet(final String servicePath, final RestfulParametes restParametes,
108             final RestfulAsyncCallback callback) throws ServiceException {
109         if(callback == null) {
110             this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, new DefaultAsyncCallback());
111         } else {
112             this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, null, callback);
113         }
114     }
115
116     @Override
117     public void asyncGet(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
118             final RestfulAsyncCallback callback) throws ServiceException {
119         if(callback == null) {
120             this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, new DefaultAsyncCallback());
121         } else {
122             this.sendHttpRequest(HttpMethods.GET, servicePath, restParametes, option, callback);
123         }
124     }
125
126     @Override
127     public RestfulResponse put(final String servicePath, final RestfulParametes restParametes) throws ServiceException {
128         return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, null);
129     }
130
131     @Override
132     public RestfulResponse put(final String servicePath, final RestfulParametes restParametes,
133             final RestfulOptions option) throws ServiceException {
134         return this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, null);
135     }
136
137     @Override
138     public void asyncPut(final String servicePath, final RestfulParametes restParametes,
139             final RestfulAsyncCallback callback) throws ServiceException {
140         if(callback == null) {
141             this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, new DefaultAsyncCallback());
142         } else {
143             this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, null, callback);
144         }
145     }
146
147     @Override
148     public void asyncPut(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
149             final RestfulAsyncCallback callback) throws ServiceException {
150         if(callback == null) {
151             this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, new DefaultAsyncCallback());
152         } else {
153             this.sendHttpRequest(HttpMethods.PUT, servicePath, restParametes, option, callback);
154         }
155     }
156
157     @Override
158     public RestfulResponse post(final String servicePath, final RestfulParametes restParametes)
159             throws ServiceException {
160         return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, null);
161     }
162
163     @Override
164     public RestfulResponse post(final String servicePath, final RestfulParametes restParametes,
165             final RestfulOptions option) throws ServiceException {
166         return this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, null);
167     }
168
169     @Override
170     public void asyncPost(final String servicePath, final RestfulParametes restParametes,
171             final RestfulAsyncCallback callback) throws ServiceException {
172         if(callback == null) {
173             this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, new DefaultAsyncCallback());
174         } else {
175             this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, null, callback);
176         }
177     }
178
179     @Override
180     public void asyncPost(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
181             final RestfulAsyncCallback callback) throws ServiceException {
182         if(callback == null) {
183             this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, new DefaultAsyncCallback());
184         } else {
185             this.sendHttpRequest(HttpMethods.POST, servicePath, restParametes, option, callback);
186         }
187     }
188
189     @Override
190     public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes)
191             throws ServiceException {
192         return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, null);
193     }
194
195     @Override
196     public RestfulResponse delete(final String servicePath, final RestfulParametes restParametes,
197             final RestfulOptions option) throws ServiceException {
198         return this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, null);
199     }
200
201     @Override
202     public void asyncDelete(final String servicePath, final RestfulParametes restParametes,
203             final RestfulAsyncCallback callback) throws ServiceException {
204         if(callback == null) {
205             this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, new DefaultAsyncCallback());
206         } else {
207             this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, null, callback);
208         }
209     }
210
211     @Override
212     public void asyncDelete(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
213             final RestfulAsyncCallback callback) throws ServiceException {
214         if(callback == null) {
215             this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, new DefaultAsyncCallback());
216         } else {
217             this.sendHttpRequest(HttpMethods.DELETE, servicePath, restParametes, option, callback);
218         }
219     }
220
221     @Override
222     public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes)
223             throws ServiceException {
224         return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, null);
225     }
226
227     @Override
228     public RestfulResponse patch(final String servicePath, final RestfulParametes restParametes,
229             final RestfulOptions option) throws ServiceException {
230         return this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, null);
231     }
232
233     @Override
234     public void asyncPatch(final String servicePath, final RestfulParametes restParametes,
235             final RestfulAsyncCallback callback) throws ServiceException {
236         if(callback == null) {
237             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, new DefaultAsyncCallback());
238         } else {
239             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, null, callback);
240         }
241     }
242
243     @Override
244     public void asyncPatch(final String servicePath, final RestfulParametes restParametes, final RestfulOptions option,
245             final RestfulAsyncCallback callback) throws ServiceException {
246         if(callback == null) {
247             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, new DefaultAsyncCallback());
248         } else {
249             this.sendHttpRequest(HTTP_PATCH, servicePath, restParametes, option, callback);
250         }
251     }
252
253 }