114dea639aefd4009db6ab5b80f005942019d167
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / http / client / HttpClientProcessorImpl.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.ByteArrayOutputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.HttpURLConnection;
25 import java.net.URL;
26 import java.util.HashMap;
27
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;
35
36 @Component
37 public class HttpClientProcessorImpl implements HttpClientProcessorInf{
38         private static final Logger logger = LoggerFactory.getLogger(HttpClientProcessorImpl.class);
39
40         @Autowired
41         private HttpClientBuilder httpClientBuilder;
42         
43         public HttpResult process(String url, RequestMethod methodType, HashMap<String, String> headerMap, String bodyString) throws ClientProtocolException, IOException
44         {
45                 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType);
46                 if(headerMap != null && !headerMap.isEmpty())
47                 {
48                         for(String key : headerMap.keySet())
49                         {
50                                 processor.addHdeader(key, headerMap.get(key));
51                         }
52                         
53                         if(null != bodyString && bodyString.length() > 0 && !bodyString.equalsIgnoreCase("null"))
54                         {
55                                 processor.addPostEntity(bodyString);
56                         }
57                         
58                 }
59                 return processor.process(url);
60         }
61         
62         public HttpResult processBytes(String url, RequestMethod methodType, HashMap<String, String> headerMap, byte[] byteArray) throws ClientProtocolException, IOException
63         {
64                 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType);
65                 if(headerMap != null && !headerMap.isEmpty())
66                 {
67                         for(String key : headerMap.keySet())
68                         {
69                                 processor.addHdeader(key, headerMap.get(key));
70                         }
71                         
72                         if(null != byteArray && byteArray.length > 0)
73                         {
74                                 processor.addBytesPostEntity(byteArray);
75                         }
76                         
77                 }
78                 return processor.process(url);
79         }
80         
81         public void setHttpClientBuilder(HttpClientBuilder httpClientBuilder) {
82                 this.httpClientBuilder = httpClientBuilder;
83         }
84         
85         public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath) 
86         {
87                 try 
88                 {
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()){
97                     saveDir.mkdir();
98                 }          
99                 File file = new File(saveDir+File.separator+fileName);    
100                 FileOutputStream fos = new FileOutputStream(file);     
101                 fos.write(getData); 
102                 if(fos!=null){
103                     fos.close();  
104                 }
105                 if(inputStream!=null){
106                     inputStream.close();
107                 }
108                 }
109                 catch ( IOException e ) {
110                         logger.info("write file fail", e);  
111                         return false;
112                 }
113         
114         logger.info("info: "+ urlStr + " download success"); 
115         return true;
116     }
117
118
119     public static byte[] readInputStream(InputStream inputStream) throws IOException {  
120         byte[] buffer = new byte[1024];  
121         int len = 0;  
122         ByteArrayOutputStream bos = new ByteArrayOutputStream();  
123         while((len = inputStream.read(buffer)) != -1) {  
124             bos.write(buffer, 0, len);  
125         }  
126         bos.close();  
127         return bos.toByteArray();  
128     }
129 }