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 ClientProtocolException, IOException
45 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType);
46 if(headerMap != null && !headerMap.isEmpty())
48 for(String key : headerMap.keySet())
50 processor.addHdeader(key, headerMap.get(key));
53 if(null != bodyString && bodyString.length() > 0 && !bodyString.equalsIgnoreCase("null"))
55 processor.addPostEntity(bodyString);
59 return processor.process(url);
62 public HttpResult processBytes(String url, RequestMethod methodType, HashMap<String, String> headerMap, byte[] byteArray) throws ClientProtocolException, IOException
64 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType);
65 if(headerMap != null && !headerMap.isEmpty())
67 for(String key : headerMap.keySet())
69 processor.addHdeader(key, headerMap.get(key));
72 if(null != byteArray && byteArray.length > 0)
74 processor.addBytesPostEntity(byteArray);
78 return processor.process(url);
81 public void setHttpClientBuilder(HttpClientBuilder httpClientBuilder) {
82 this.httpClientBuilder = httpClientBuilder;
85 public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath)
89 URL url = new URL(urlStr);
90 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
91 conn.setConnectTimeout(10*1000);
92 conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
93 InputStream inputStream = conn.getInputStream();
94 byte[] getData = readInputStream(inputStream);
95 File saveDir = new File(savePath);
96 if(!saveDir.exists()){
99 File file = new File(saveDir+File.separator+fileName);
100 FileOutputStream fos = new FileOutputStream(file);
105 if(inputStream!=null){
109 catch ( IOException e ) {
110 logger.info("write file fail", e);
114 logger.info("info: "+ urlStr + " download success");
119 public static byte[] readInputStream(InputStream inputStream) throws IOException {
120 byte[] buffer = new byte[1024];
122 ByteArrayOutputStream bos = new ByteArrayOutputStream();
123 while((len = inputStream.read(buffer)) != -1) {
124 bos.write(buffer, 0, len);
127 return bos.toByteArray();