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 com.nokia.vfcadaptor.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.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.springframework.web.bind.annotation.RequestMethod;
33 import com.nokia.vfcadaptor.constant.CommonConstants;
35 public class HttpRequestProcessor {
36 private CloseableHttpClient httpClient;
37 private HttpRequestBase httpRequest;
39 public HttpRequestProcessor(HttpClientBuilder httpClientBuilder, RequestMethod requestMethod)
41 httpClient = httpClientBuilder.build();
42 httpRequest = HttpClientUtils.getHttpRequest(requestMethod);
45 public String process(String url) throws ClientProtocolException, IOException
47 httpRequest.setURI(URI.create(url));
49 HttpResponse response = httpClient.execute(httpRequest);
50 HttpEntity resEntity = response.getEntity();
51 String responseContent = "";
52 if (resEntity != null) {
53 responseContent = EntityUtils.toString(resEntity, CommonConstants.UTF_8);
54 EntityUtils.consume(resEntity);
58 return responseContent;
61 public void addHdeader(String key, String value) {
62 httpRequest.setHeader(key, value);
66 public void addPostEntity(String bodyStr) {
67 ((HttpPost)httpRequest).setEntity(new StringEntity(bodyStr, CommonConstants.UTF_8));