2 * Copyright 2016-2017 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.onap.vfc.nfvo.vnfm.svnfm.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.apache.http.client.methods.*;
41 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
42 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.http.HttpRequest;
48 * HTTP Request class.</br>
51 * @version VFC 1.0 Sep 14, 2016
53 public final class HttpRequests {
55 private static final Logger LOG = LoggerFactory.getLogger(HttpRequest.class);
57 private static MultiThreadedHttpConnectionManager httpClientMgr;
59 private static final int PORT = 31943;
61 private HttpRequests() {
66 httpClientMgr = new MultiThreadedHttpConnectionManager();
67 httpClientMgr.getParams().setStaleCheckingEnabled(true);
68 httpClientMgr.getParams().setMaxTotalConnections(20);
69 httpClientMgr.getParams().setDefaultMaxConnectionsPerHost(100);
73 * Request builder.</br>
76 * @version VFC 1.0 Sep 14, 2016
78 public static class Builder {
80 private final List<Header> headers = new ArrayList<>(10);
82 private String paramsJson;
84 private HttpClient client;
86 private HttpMethod httpMethod;
88 private String encoding;
92 private String authenticateMode;
97 * @param authenticateMode
100 public Builder(String authenticateMode) {
101 this.authenticateMode = authenticateMode;
102 client = new HttpClient(httpClientMgr);
103 client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
104 client.getHttpConnectionManager().getParams().setSoTimeout(30000);
105 encoding = Constant.ENCODEING;
117 public Builder addHeader(String name, String value) {
118 headers.add(new Header(name, value));
131 public Builder addHeaders(Header header, Header... headers) {
133 this.headers.add(header);
135 if(headers != null && headers.length > 0) {
136 for(Header h : headers) {
151 public Builder addHeaders(List<Header> headers) {
152 if(headers != null && !headers.isEmpty()) {
153 this.headers.addAll(headers);
165 * @throws VnfmException
168 public Builder setUrl(String url, String path) throws VnfmException {
169 if(StringUtils.isEmpty(url)) {
170 throw new VnfmException("com.huawei.nfvo.vcmmadapter.fusionsphere.check.httprequest.url");
173 this.url = url + path;
175 LOG.info("setUrl: url =" + url);
177 Protocol.registerProtocol(Constant.HTTPS,
178 new Protocol(Constant.HTTPS, SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
191 * @throws VnfmException
194 public Builder setUrl(String url, String path, int defPort) throws VnfmException {
195 if(StringUtils.isEmpty(url)) {
196 throw new VnfmException("com.huawei.nfvo.vcmmadapter.fusionsphere.check.httprequest.url");
199 this.url = url + path;
201 LOG.info("setUrl: url =" + url);
203 Protocol.registerProtocol(Constant.HTTPS, new Protocol(Constant.HTTPS,
204 SslProtocolSocketFactory.getInstance().get(authenticateMode), defPort));
216 public Builder post() {
217 this.httpMethod = new PostMethod(url);
228 public Builder get() {
229 this.httpMethod = new GetMethod(url);
240 public Builder put() {
241 this.httpMethod = new PutMethod(url);
252 public Builder delete() {
253 this.httpMethod = new DeleteMethod(url);
265 public Builder setParams(String json) {
266 this.paramsJson = json;
278 public Builder setEncoding(String encode) {
279 this.encoding = encode;
290 public String request() {
291 String result = null;
293 result = executeMethod().getResponseBodyAsString();
294 } catch(SSLHandshakeException e) {
295 LOG.error(String.format("function=request, msg=http request url: %s, SSLHandshake Fail : ", url), e);
297 LOG.error("function=request, msg=SSLHandshake Fail, start refresh certificate ...");
298 SslProtocolSocketFactory socketFactory = SslProtocolSocketFactory.getInstance();
299 socketFactory.refresh(authenticateMode);
300 Protocol.registerProtocol(Constant.HTTPS, new Protocol(Constant.HTTPS,
301 SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
302 LOG.error("function=request, msg=SSLHandshake Fail, certificate refresh successful .");
304 result = executeMethod().getResponseBodyAsString();
305 } catch(IOException ioe) {
306 LOG.error(String.format("function=request, IOException msg=http request url: %s, error: ", url),
308 } catch(VnfmException ose) {
309 LOG.error(String.format("function=request, VnfmException msg=http request url: %s, error: ", url),
312 } catch(IOException | VnfmException e) {
313 LOG.error(String.format("function=request, IOException msg=http request url: %s, error: ", url), e);
315 httpMethod.releaseConnection();
321 * Execute the HTTP method
325 * @throws VnfmException
326 * @throws IOException
329 public HttpMethod execute() throws VnfmException, IOException {
332 } catch(SSLHandshakeException e) {
333 LOG.error(String.format("function=execute, msg=http request url: %s, SSLHandshake Fail : ", url), e);
334 LOG.error("function=execute, SSLHandshake Fail, start refresh certificate ...");
335 SslProtocolSocketFactory socketFactory = SslProtocolSocketFactory.getInstance();
336 socketFactory.refresh(authenticateMode);
337 Protocol.registerProtocol(Constant.HTTPS, new Protocol(Constant.HTTPS,
338 SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
339 LOG.error("function=execute, SSLHandshake Fail, certificate refresh successful .");
346 private HttpMethod executeMethod() throws VnfmException, IOException {
347 if(httpMethod == null) {
348 httpMethod = new GetMethod(url);
353 client.executeMethod(httpMethod);
358 private void handleParams() throws UnsupportedEncodingException {
359 if(paramsJson != null && !paramsJson.isEmpty()) {
360 StringRequestEntity stringRequestEntity =
361 new StringRequestEntity(paramsJson, "application/json", encoding);
362 String contentLengthString = String.valueOf(stringRequestEntity.getContentLength());
364 if(httpMethod instanceof PostMethod || httpMethod instanceof PutMethod) {
365 ((EntityEnclosingMethod)httpMethod).setRequestEntity(stringRequestEntity);
366 ((EntityEnclosingMethod)httpMethod).addRequestHeader("Content-Length", contentLengthString);
368 httpMethod.setQueryString(paramsJson);
370 addHeader("Content-Type", String.format("application/json;charset=%s", encoding));
373 for(Header header : headers) {
374 httpMethod.addRequestHeader(header);