2 * Copyright 2016 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openo.nfvo.vnfmadapter.service.csm.connect;
19 import java.io.IOException;
20 import java.io.UnsupportedEncodingException;
21 import java.util.ArrayList;
22 import java.util.List;
24 import javax.net.ssl.SSLHandshakeException;
26 import org.apache.commons.httpclient.Header;
27 import org.apache.commons.httpclient.HttpClient;
28 import org.apache.commons.httpclient.HttpMethod;
29 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
30 import org.apache.commons.httpclient.methods.DeleteMethod;
31 import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
32 import org.apache.commons.httpclient.methods.GetMethod;
33 import org.apache.commons.httpclient.methods.PostMethod;
34 import org.apache.commons.httpclient.methods.PutMethod;
35 import org.apache.commons.httpclient.methods.StringRequestEntity;
36 import org.apache.commons.httpclient.protocol.Protocol;
37 import org.apache.commons.lang.StringUtils;
38 import org.openo.nfvo.vnfmadapter.common.VnfmException;
39 import org.openo.nfvo.vnfmadapter.service.constant.Constant;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.http.HttpRequest;
45 * HTTP Request class.</br>
48 * @version NFVO 0.5 Sep 14, 2016
50 public final class HttpRequests {
52 private static final Logger LOG = LoggerFactory.getLogger(HttpRequest.class);
54 private static MultiThreadedHttpConnectionManager httpClientMgr;
56 private static final int PORT = 31943;
58 private HttpRequests() {
63 httpClientMgr = new MultiThreadedHttpConnectionManager();
64 httpClientMgr.getParams().setStaleCheckingEnabled(true);
65 httpClientMgr.getParams().setMaxTotalConnections(20);
66 httpClientMgr.getParams().setDefaultMaxConnectionsPerHost(100);
70 * Request builder.</br>
73 * @version NFVO 0.5 Sep 14, 2016
75 public static class Builder {
77 private final List<Header> headers = new ArrayList<>(10);
79 private String paramsJson;
81 private HttpClient client;
83 private HttpMethod httpMethod;
85 private String encoding;
89 private String authenticateMode;
95 * @param authenticateMode
98 public Builder(String authenticateMode) {
99 this.authenticateMode = authenticateMode;
100 client = new HttpClient(httpClientMgr);
101 client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
102 client.getHttpConnectionManager().getParams().setSoTimeout(30000);
103 encoding = Constant.ENCODEING;
115 public Builder addHeader(String name, String value) {
116 headers.add(new Header(name, value));
129 public Builder addHeaders(Header header, Header... headers) {
131 this.headers.add(header);
133 if(headers != null && headers.length > 0) {
134 for(Header h : headers) {
149 public Builder addHeaders(List<Header> headers) {
150 if(headers != null && !headers.isEmpty()) {
151 this.headers.addAll(headers);
163 * @throws VnfmException
166 public Builder setUrl(String url, String path) throws VnfmException {
167 if(StringUtils.isEmpty(url)) {
168 throw new VnfmException("com.huawei.nfvo.vcmmadapter.fusionsphere.check.httprequest.url");
171 this.url = url + path;
173 LOG.info("setUrl: url =" + url);
175 Protocol.registerProtocol("https",
176 new Protocol("https", SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
189 * @throws VnfmException
192 public Builder setUrl(String url, String path, int defPort) throws VnfmException {
193 if(StringUtils.isEmpty(url)) {
194 throw new VnfmException("com.huawei.nfvo.vcmmadapter.fusionsphere.check.httprequest.url");
197 this.url = url + path;
199 LOG.info("setUrl: url =" + url);
201 Protocol.registerProtocol("https",
202 new Protocol("https", SslProtocolSocketFactory.getInstance().get(authenticateMode), defPort));
214 public Builder post() {
215 this.httpMethod = new PostMethod(url);
226 public Builder get() {
227 this.httpMethod = new GetMethod(url);
238 public Builder put() {
239 this.httpMethod = new PutMethod(url);
250 public Builder delete() {
251 this.httpMethod = new DeleteMethod(url);
263 public Builder setParams(String json) {
264 this.paramsJson = json;
276 public Builder setEncoding(String encode) {
277 this.encoding = encode;
288 public String request() {
289 String result = null;
291 result = executeMethod().getResponseBodyAsString();
292 } catch(SSLHandshakeException e) {
293 LOG.error(String.format("function=request, msg=http request url: %s, SSLHandshake Fail : ", url), e);
295 LOG.error("function=request, msg=SSLHandshake Fail, start refresh certificate ...");
296 SslProtocolSocketFactory socketFactory = SslProtocolSocketFactory.getInstance();
297 socketFactory.refresh(authenticateMode);
298 Protocol.registerProtocol("https",
299 new Protocol("https", SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
300 LOG.error("function=request, msg=SSLHandshake Fail, certificate refresh successful .");
302 result = executeMethod().getResponseBodyAsString();
303 } catch(IOException ioe) {
304 LOG.error(String.format("function=request, msg=http request url: %s, error: ", url), ioe);
305 } catch(VnfmException ose) {
306 LOG.error(String.format("function=request, msg=http request url: %s, error: ", url), ose);
308 } catch(IOException | VnfmException e) {
309 LOG.error(String.format("function=request, msg=http request url: %s, error: ", url), e);
311 httpMethod.releaseConnection();
317 * Execute the HTTP method
321 * @throws VnfmException
322 * @throws IOException
325 public HttpMethod execute() throws VnfmException, IOException {
328 } catch(SSLHandshakeException e) {
329 LOG.error(String.format("function=execute, msg=http request url: %s, SSLHandshake Fail : ", url), e);
330 LOG.error("function=execute, SSLHandshake Fail, start refresh certificate ...");
331 SslProtocolSocketFactory socketFactory = SslProtocolSocketFactory.getInstance();
332 socketFactory.refresh(authenticateMode);
333 Protocol.registerProtocol("https",
334 new Protocol("https", SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
335 LOG.error("function=execute, SSLHandshake Fail, certificate refresh successful .");
342 private HttpMethod executeMethod() throws VnfmException, IOException {
343 if(httpMethod == null) {
344 httpMethod = new GetMethod(url);
349 client.executeMethod(httpMethod);
354 private void handleParams() throws UnsupportedEncodingException {
355 if(paramsJson != null && !paramsJson.isEmpty()) {
356 StringRequestEntity stringRequestEntity =
357 new StringRequestEntity(paramsJson, "application/json", encoding);
358 String contentLengthString = String.valueOf(stringRequestEntity.getContentLength());
360 if(httpMethod instanceof PostMethod || httpMethod instanceof PutMethod) {
361 ((EntityEnclosingMethod)httpMethod).setRequestEntity(stringRequestEntity);
362 ((EntityEnclosingMethod)httpMethod).addRequestHeader("Content-Length", contentLengthString);
364 httpMethod.setQueryString(paramsJson);
366 addHeader("Content-Type", String.format("application/json;charset=%s", encoding));
369 for(Header header : headers) {
370 httpMethod.addRequestHeader(header);