Fix nexus-iq issues
[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 IOException
44         {
45                 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType);
46                 //Map<String, String> headerMap = new HashMap<String, String>();
47                 if(headerMap != null && !headerMap.isEmpty())
48                 {
49                         /*for(String key : headerMap.keySet())
50                         {
51                                 processor.addHdeader(key, headerMap.get(key));
52                         }*/
53
54                         for (HashMap.Entry<String, String> entry : headerMap.entrySet()) {
55                                 processor.addHeader(entry.getKey(), entry.getValue());
56                         }
57                         
58                         if(bodyString.length() > 0 && !"null".equalsIgnoreCase(bodyString))
59                         {
60                                 processor.addPostEntity(bodyString);
61                         }
62                         
63                 }
64                 return processor.process(url);
65         }
66         
67         public HttpResult processBytes(String url, RequestMethod methodType, HashMap<String, String> headerMap, byte[] byteArray) throws IOException
68         {
69                 HttpRequestProcessor processor = new HttpRequestProcessor(httpClientBuilder, methodType);
70                 if(headerMap != null && !headerMap.isEmpty())
71                 {
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"
75
76                         /*for(String key : headerMap.keySet())
77                         {
78                                 processor.addHeader(key, headerMap.get(key));
79                         }*/
80                         
81                         if(null != byteArray && byteArray.length > 0)
82                         {
83                                 processor.addBytesPostEntity(byteArray);
84                         }
85                         
86                 }
87                 return processor.process(url);
88         }
89         
90         public void setHttpClientBuilder(HttpClientBuilder httpClientBuilder) {
91                 this.httpClientBuilder = httpClientBuilder;
92         }
93         
94         public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath) 
95         {
96                 try 
97                 {
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()){
106                     saveDir.mkdir();
107                 }          
108                 File file = new File(saveDir+File.separator+fileName);    
109                 try(FileOutputStream fos = new FileOutputStream(file)){
110                         fos.write(getData); 
111                 }
112                 
113                 if(inputStream!=null){
114                     inputStream.close();
115                 }
116                 }
117                 catch ( IOException e ) {
118                         logger.info("write file fail", e);  
119                         return false;
120                 }
121         
122         logger.info("info: "+ urlStr + " download success"); 
123         return true;
124     }
125
126
127     public static byte[] readInputStream(InputStream inputStream) throws IOException {  
128         byte[] buffer = new byte[1024];  
129         int len = 0;  
130         try(ByteArrayOutputStream bos = new ByteArrayOutputStream()){
131         while((len = inputStream.read(buffer)) != -1) {  
132             bos.write(buffer, 0, len);  
133         }  
134         return bos.toByteArray();  
135         }
136     }
137
138         @Override
139         public HttpResult process(String url) throws ClientProtocolException, IOException {
140                 return process(url, RequestMethod.GET, null, null);
141         }
142 }