2  * Copyright 2016-2017, Nokia Corporation
 
   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.driver.vnfm.svnfm.http.client;
 
  19 import java.io.IOException;
 
  22 import org.apache.http.HttpEntity;
 
  23 import org.apache.http.HttpResponse;
 
  24 import org.apache.http.client.ClientProtocolException;
 
  25 import org.apache.http.client.methods.HttpPost;
 
  26 import org.apache.http.client.methods.HttpRequestBase;
 
  27 import org.apache.http.entity.ByteArrayEntity;
 
  28 import org.apache.http.entity.StringEntity;
 
  29 import org.apache.http.impl.client.CloseableHttpClient;
 
  30 import org.apache.http.impl.client.HttpClientBuilder;
 
  31 import org.apache.http.util.EntityUtils;
 
  32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
 
  33 import org.slf4j.Logger;
 
  34 import org.slf4j.LoggerFactory;
 
  35 import org.springframework.web.bind.annotation.RequestMethod;
 
  37 public class HttpRequestProcessor {
 
  38         private static final Logger logger = LoggerFactory.getLogger(HttpRequestProcessor.class);
 
  39         private CloseableHttpClient httpClient;
 
  40         private HttpRequestBase httpRequest;
 
  42         public HttpRequestProcessor(HttpClientBuilder httpClientBuilder, RequestMethod requestMethod)
 
  44                 httpClient = httpClientBuilder.build();
 
  45                 httpRequest = HttpClientUtils.getHttpRequest(requestMethod);
 
  48         public HttpResult process(String url) throws ClientProtocolException, IOException
 
  50                 httpRequest.setURI(URI.create(url));
 
  52                 HttpResponse response = httpClient.execute(httpRequest);
 
  53                 httpRequest.releaseConnection();
 
  54 //              httpClient.close();
 
  55                 HttpResult httpResult = buildHttpResult(response);
 
  60         private HttpResult buildHttpResult(HttpResponse response) throws IOException {
 
  61                 HttpEntity resEntity = response.getEntity();
 
  62                 String responseContent = "";
 
  63                 if (resEntity != null) {
 
  64                         responseContent = EntityUtils.toString(resEntity, CommonConstants.UTF_8);
 
  65                         EntityUtils.consume(resEntity);
 
  68                 HttpResult httpResult = new HttpResult();
 
  69                 httpResult.setStatusCode(response.getStatusLine().getStatusCode());
 
  70                 httpResult.setStatusCause(response.getStatusLine().getReasonPhrase());
 
  71                 httpResult.setHeaders(response.getAllHeaders());
 
  72                 httpResult.setContent(responseContent);
 
  77         public void addHdeader(String key, String value) {
 
  78                 httpRequest.setHeader(key, value);
 
  82         public void addPostEntity(String bodyStr) {
 
  83                 ((HttpPost)httpRequest).setEntity(new StringEntity(bodyStr, CommonConstants.UTF_8));
 
  86         public void addBytesPostEntity(byte[] byteArray) {
 
  87                 ((HttpPost)httpRequest).setEntity(new ByteArrayEntity(byteArray));