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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmException;
 
  39 import org.onap.vfc.nfvo.vnfm.svnfm.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 VFC 1.0 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 VFC 1.0 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;
 
  94          * @param authenticateMode
 
  97         public Builder(String authenticateMode) {
 
  98             this.authenticateMode = authenticateMode;
 
  99             client = new HttpClient(httpClientMgr);
 
 100             client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
 
 101             client.getHttpConnectionManager().getParams().setSoTimeout(30000);
 
 102             encoding = Constant.ENCODEING;
 
 114         public Builder addHeader(String name, String value) {
 
 115             headers.add(new Header(name, value));
 
 128         public Builder addHeaders(Header header, Header... headers) {
 
 130                 this.headers.add(header);
 
 132             if(headers != null && headers.length > 0) {
 
 133                 for(Header h : headers) {
 
 148         public Builder addHeaders(List<Header> headers) {
 
 149             if(headers != null && !headers.isEmpty()) {
 
 150                 this.headers.addAll(headers);
 
 162          * @throws VnfmException
 
 165         public Builder setUrl(String url, String path) throws VnfmException {
 
 166             if(StringUtils.isEmpty(url)) {
 
 167                 throw new VnfmException("com.huawei.nfvo.vcmmadapter.fusionsphere.check.httprequest.url");
 
 170             this.url = url + path;
 
 172             LOG.info("setUrl: url =" + url);
 
 174             Protocol.registerProtocol(Constant.HTTPS,
 
 175                     new Protocol(Constant.HTTPS, SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
 
 188          * @throws VnfmException
 
 191         public Builder setUrl(String url, String path, int defPort) throws VnfmException {
 
 192             if(StringUtils.isEmpty(url)) {
 
 193                 throw new VnfmException("com.huawei.nfvo.vcmmadapter.fusionsphere.check.httprequest.url");
 
 196             this.url = url + path;
 
 198             LOG.info("setUrl: url =" + url);
 
 200             Protocol.registerProtocol(Constant.HTTPS, new Protocol(Constant.HTTPS,
 
 201                     SslProtocolSocketFactory.getInstance().get(authenticateMode), defPort));
 
 213         public Builder post() {
 
 214             this.httpMethod = new PostMethod(url);
 
 225         public Builder get() {
 
 226             this.httpMethod = new GetMethod(url);
 
 237         public Builder put() {
 
 238             this.httpMethod = new PutMethod(url);
 
 249         public Builder delete() {
 
 250             this.httpMethod = new DeleteMethod(url);
 
 262         public Builder setParams(String json) {
 
 263             this.paramsJson = json;
 
 275         public Builder setEncoding(String encode) {
 
 276             this.encoding = encode;
 
 287         public String request() {
 
 288             String result = null;
 
 290                 result = executeMethod().getResponseBodyAsString();
 
 291             } catch(SSLHandshakeException e) {
 
 292                 LOG.error(String.format("function=request, msg=http request url: %s, SSLHandshake Fail : ", url), e);
 
 294                     LOG.error("function=request, msg=SSLHandshake Fail, start refresh certificate ...");
 
 295                     SslProtocolSocketFactory socketFactory = SslProtocolSocketFactory.getInstance();
 
 296                     socketFactory.refresh(authenticateMode);
 
 297                     Protocol.registerProtocol(Constant.HTTPS, new Protocol(Constant.HTTPS,
 
 298                             SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
 
 299                     LOG.error("function=request, msg=SSLHandshake Fail, certificate refresh successful .");
 
 301                     result = executeMethod().getResponseBodyAsString();
 
 302                 } catch(IOException ioe) {
 
 303                     LOG.error(String.format("function=request, IOException msg=http request url: %s, error: ", url),
 
 305                 } catch(VnfmException ose) {
 
 306                     LOG.error(String.format("function=request, VnfmException msg=http request url: %s, error: ", url),
 
 309             } catch(IOException | VnfmException e) {
 
 310                 LOG.error(String.format("function=request, IOException msg=http request url: %s, error: ", url), e);
 
 312                 httpMethod.releaseConnection();
 
 318          * Execute the HTTP method
 
 322          * @throws VnfmException
 
 323          * @throws IOException
 
 326         public HttpMethod execute() throws VnfmException, IOException {
 
 329             } catch(SSLHandshakeException e) {
 
 330                 LOG.error(String.format("function=execute, msg=http request url: %s, SSLHandshake Fail : ", url), e);
 
 331                 LOG.error("function=execute, SSLHandshake Fail, start refresh certificate ...");
 
 332                 SslProtocolSocketFactory socketFactory = SslProtocolSocketFactory.getInstance();
 
 333                 socketFactory.refresh(authenticateMode);
 
 334                 Protocol.registerProtocol(Constant.HTTPS, new Protocol(Constant.HTTPS,
 
 335                         SslProtocolSocketFactory.getInstance().get(authenticateMode), PORT));
 
 336                 LOG.error("function=execute, SSLHandshake Fail, certificate refresh successful .");
 
 343         private HttpMethod executeMethod() throws VnfmException, IOException {
 
 344             if(httpMethod == null) {
 
 345                 httpMethod = new GetMethod(url);
 
 350             client.executeMethod(httpMethod);
 
 355         private void handleParams() throws UnsupportedEncodingException {
 
 356             if(paramsJson != null && !paramsJson.isEmpty()) {
 
 357                 StringRequestEntity stringRequestEntity =
 
 358                         new StringRequestEntity(paramsJson, "application/json", encoding);
 
 359                 String contentLengthString = String.valueOf(stringRequestEntity.getContentLength());
 
 361                 if(httpMethod instanceof PostMethod || httpMethod instanceof PutMethod) {
 
 362                     ((EntityEnclosingMethod)httpMethod).setRequestEntity(stringRequestEntity);
 
 363                     ((EntityEnclosingMethod)httpMethod).addRequestHeader("Content-Length", contentLengthString);
 
 365                     httpMethod.setQueryString(paramsJson);
 
 367                 addHeader("Content-Type", String.format("application/json;charset=%s", encoding));
 
 370             for(Header header : headers) {
 
 371                 httpMethod.addRequestHeader(header);