Modify vnf package extract part
[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.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.springframework.web.bind.annotation.RequestMethod;
34
35 public class HttpRequestProcessor {
36         private CloseableHttpClient httpClient;
37         private HttpRequestBase httpRequest;
38         
39         public HttpRequestProcessor(HttpClientBuilder httpClientBuilder, RequestMethod requestMethod)
40         {
41                 httpClient = httpClientBuilder.build();
42                 httpRequest = HttpClientUtils.getHttpRequest(requestMethod);
43         }
44         
45         public HttpResult process(String url) throws ClientProtocolException, IOException
46         {
47                 httpRequest.setURI(URI.create(url));
48                 
49                 HttpResponse response = httpClient.execute(httpRequest);
50                 HttpResult httpResult = buildHttpResult(response);
51                 
52                 return httpResult;
53         }
54
55         private HttpResult buildHttpResult(HttpResponse response) throws IOException {
56                 HttpEntity resEntity = response.getEntity();
57                 String responseContent = "";
58                 if (resEntity != null) {
59                         responseContent = EntityUtils.toString(resEntity, CommonConstants.UTF_8);
60                         EntityUtils.consume(resEntity);
61                 }
62                 
63                 HttpResult httpResult = new HttpResult();
64                 httpResult.setStatusCode(response.getStatusLine().getStatusCode());
65                 httpResult.setStatusCause(response.getStatusLine().getReasonPhrase());
66                 httpResult.setHeaders(response.getAllHeaders());
67                 httpResult.setContent(responseContent);
68                 
69                 return httpResult;
70         }
71
72         public void addHdeader(String key, String value) {
73                 httpRequest.setHeader(key, value);
74                 
75         }
76
77         public void addPostEntity(String bodyStr) {
78                 ((HttpPost)httpRequest).setEntity(new StringEntity(bodyStr, CommonConstants.UTF_8));
79         }
80         
81         public void addBytesPostEntity(byte[] byteArray) {
82                 ((HttpPost)httpRequest).setEntity(new ByteArrayEntity(byteArray));
83         }
84 }