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.ByteArrayOutputStream;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.HttpURLConnection;
26 import java.util.HashMap;
28 import org.apache.http.client.ClientProtocolException;
29 import org.apache.http.impl.client.HttpClientBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34 import org.springframework.web.bind.annotation.RequestMethod;
37 public class HttpClientProcessorImpl implements HttpClientProcessorInf{
38 private static final Logger logger = LoggerFactory.getLogger(HttpClientProcessorImpl.class);
41 private HttpClientBuilder httpClientBuilder;
43 public HttpResult process(String url, RequestMethod methodType, HashMap<String, String> headerMap, String bodyString) throws IOException
45 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType);
46 //Map<String, String> headerMap = new HashMap<String, String>();
47 if(headerMap != null && !headerMap.isEmpty())
49 /*for(String key : headerMap.keySet())
51 processor.addHdeader(key, headerMap.get(key));
54 for (HashMap.Entry<String, String> entry : headerMap.entrySet()) {
55 processor.addHeader(entry.getKey(), entry.getValue());
58 if(bodyString.length() > 0 && !"null".equalsIgnoreCase(bodyString))
60 processor.addPostEntity(bodyString);
64 return processor.process(url);
67 public HttpResult processBytes(String url, RequestMethod methodType, HashMap<String, String> headerMap, byte[] byteArray) throws IOException
69 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType);
70 if(headerMap != null && !headerMap.isEmpty())
72 for (HashMap.Entry<String, String> entry : headerMap.entrySet()) {
73 processor.addHeader(entry.getKey(), entry.getValue());
74 } //Iterate over the "entrySet" instead of the "keySet"
76 /*for(String key : headerMap.keySet())
78 processor.addHeader(key, headerMap.get(key));
81 if(null != byteArray && byteArray.length > 0)
83 processor.addBytesPostEntity(byteArray);
87 return processor.process(url);
90 public void setHttpClientBuilder(HttpClientBuilder httpClientBuilder) {
91 this.httpClientBuilder = httpClientBuilder;
94 public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath)
98 URL url = new URL(urlStr);
99 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
100 conn.setConnectTimeout(20*1000);
101 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
102 InputStream inputStream = conn.getInputStream();
103 byte[] getData = readInputStream(inputStream);
104 File saveDir = new File(savePath);
105 if(!saveDir.exists()){
108 File file = new File(saveDir+File.separator+fileName);
109 try(FileOutputStream fos = new FileOutputStream(file)){
113 if(inputStream!=null){
117 catch ( IOException e ) {
118 logger.info("write file fail", e);
122 logger.info("info: "+ urlStr + " download success");
127 public static byte[] readInputStream(InputStream inputStream) throws IOException {
128 byte[] buffer = new byte[1024];
130 try(ByteArrayOutputStream bos = new ByteArrayOutputStream()){
131 while((len = inputStream.read(buffer)) != -1) {
132 bos.write(buffer, 0, len);
134 return bos.toByteArray();
139 public HttpResult process(String url) throws ClientProtocolException, IOException {
140 return process(url, RequestMethod.GET, null, null);