c553238bd60513ef52d2ade5623516a4b6f5f245
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / http / client / HttpRequestProcessor.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
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.driver.vnfm.svnfm.http.client;
18
19 import java.io.IOException;
20 import java.net.URI;
21
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.HttpPatch;
26 import org.apache.http.client.methods.HttpPost;
27 import org.apache.http.client.methods.HttpRequestBase;
28 import org.apache.http.entity.ByteArrayEntity;
29 import org.apache.http.entity.StringEntity;
30 import org.apache.http.impl.client.CloseableHttpClient;
31 import org.apache.http.impl.client.HttpClientBuilder;
32 import org.apache.http.util.EntityUtils;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.web.bind.annotation.RequestMethod;
37
38 public class HttpRequestProcessor {
39         private static final Logger logger = LoggerFactory.getLogger(HttpRequestProcessor.class);
40         private CloseableHttpClient httpClient;
41         private HttpRequestBase httpRequest;
42         
43         public HttpRequestProcessor(HttpClientBuilder httpClientBuilder, RequestMethod requestMethod)
44         {
45                 httpClient = httpClientBuilder.build();
46                 httpRequest = HttpClientUtils.getHttpRequest(requestMethod);
47         }
48         
49         public HttpResult process(String url) throws ClientProtocolException, IOException
50         {
51                 httpRequest.setURI(URI.create(url));
52                 
53                 HttpResponse response = httpClient.execute(httpRequest);
54                 httpRequest.releaseConnection();
55 //              httpClient.close();
56                 HttpResult httpResult = buildHttpResult(response);
57                 
58                 return httpResult;
59         }
60
61         private HttpResult buildHttpResult(HttpResponse response) throws IOException {
62                 HttpEntity resEntity = response.getEntity();
63                 String responseContent = "";
64                 if (resEntity != null) {
65                         responseContent = EntityUtils.toString(resEntity, CommonConstants.UTF_8);
66                         EntityUtils.consume(resEntity);
67                 }
68                 
69                 HttpResult httpResult = new HttpResult();
70                 httpResult.setStatusCode(response.getStatusLine().getStatusCode());
71                 httpResult.setStatusCause(response.getStatusLine().getReasonPhrase());
72                 httpResult.setHeaders(response.getAllHeaders());
73                 httpResult.setContent(responseContent);
74                 
75                 return httpResult;
76         }
77
78         public void addHdeader(String key, String value) {
79                 httpRequest.setHeader(key, value);
80                 
81         }
82
83         public void addPostEntity(String bodyStr) {
84                 if(httpRequest instanceof HttpPost) {
85                         ((HttpPost)httpRequest).setEntity(new StringEntity(bodyStr, CommonConstants.UTF_8));
86                 } else if(httpRequest instanceof HttpPatch) {
87                         ((HttpPatch)httpRequest).setEntity(new StringEntity(bodyStr, CommonConstants.UTF_8));
88                 }
89         }
90         
91         public void addBytesPostEntity(byte[] byteArray) {
92                 ((HttpPost)httpRequest).setEntity(new ByteArrayEntity(byteArray));
93         }
94 }