b2414a52608e0c6adc3490c7edfb2ff53d2e15a7
[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.HttpPost;
26 import org.apache.http.client.methods.HttpRequestBase;
27 import org.apache.http.entity.StringEntity;
28 import org.apache.http.impl.client.CloseableHttpClient;
29 import org.apache.http.impl.client.HttpClientBuilder;
30 import org.apache.http.util.EntityUtils;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.constant.CommonConstants;
32 import org.springframework.web.bind.annotation.RequestMethod;
33
34 public class HttpRequestProcessor {
35         private CloseableHttpClient httpClient;
36         private HttpRequestBase httpRequest;
37         
38         public HttpRequestProcessor(HttpClientBuilder httpClientBuilder, RequestMethod requestMethod)
39         {
40                 httpClient = httpClientBuilder.build();
41                 httpRequest = HttpClientUtils.getHttpRequest(requestMethod);
42         }
43         
44         public String process(String url) throws ClientProtocolException, IOException
45         {
46                 httpRequest.setURI(URI.create(url));
47                 
48                 HttpResponse response = httpClient.execute(httpRequest);
49                 HttpEntity resEntity = response.getEntity();
50                 String responseContent = "";
51                 if (resEntity != null) {
52                         responseContent = EntityUtils.toString(resEntity, CommonConstants.UTF_8);
53                         EntityUtils.consume(resEntity);
54                 }
55                 httpClient.close();
56                 
57                 return responseContent;
58         }
59
60         public void addHdeader(String key, String value) {
61                 httpRequest.setHeader(key, value);
62                 
63         }
64
65         public void addPostEntity(String bodyStr) {
66                 ((HttpPost)httpRequest).setEntity(new StringEntity(bodyStr, CommonConstants.UTF_8));
67                 
68         }
69 }