d3d095b35010c0249d1e5666b28b5d4077e46160
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.analytics.system.fusion.controller;
39 /**
40  * Raptor Blob Extract Servlet
41  * 
42  */
43
44 import java.io.BufferedInputStream;
45 import java.io.BufferedOutputStream;
46 import java.io.File;
47 import java.io.FileInputStream;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.io.OutputStream;
51 import java.sql.Blob;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Map;
55
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
58
59 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import org.onap.portalsdk.core.service.DataAccessService;
61 import org.springframework.web.servlet.ModelAndView;;
62
63
64 public class FileServletController  {
65
66         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FileServletController.class);
67
68         private DataAccessService dataAccessService;
69
70         public ModelAndView handleRequestInternal(HttpServletRequest request,
71                         HttpServletResponse response) throws Exception {
72                 logger.debug(EELFLoggerDelegate.debugLogger, ("FileServletController:: f=" + request.getParameter("f")));
73
74                 String fname = request.getParameter("f");
75
76                 try {
77                         Map params = new HashMap();
78                         params.put("fname", fname);
79
80                         logger.debug(EELFLoggerDelegate.debugLogger, ("executing query: select file_blob from cr_report_file_history where file_name = :"
81                                         + fname));
82
83                         List<Object> fileFromDB = (List<Object>) getDataAccessService().executeNamedQuery("getFileWithName", params, null);
84
85                         byte[] allBytesInBlob = null;
86
87                         if (fileFromDB != null && fileFromDB.size() > 0) {
88
89                                 logger.debug(EELFLoggerDelegate.debugLogger, ("reading file blob from DB..."));
90                                 try {
91                                         
92                             /*for weblogic setup
93                              * if(Globals.isWeblogicServer()) {
94                                 weblogic.jdbc.vendor.oracle.OracleThinBlob aBlob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) ((org.hibernate.lob.SerializableBlob) fileFromDB
95                                                                 .get(0)).getWrappedBlob();
96                                 InputStream inBlob = ((java.sql.Blob) aBlob).getBinaryStream();
97                                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
98                                 byte[] buf = new byte[1024];
99                                 int n = 0;
100                                 while ((n=inBlob.read(buf))>=0) {
101                                         baos.write(buf, 0, n);
102                                 }
103                                 inBlob.close();
104                                 allBytesInBlob = baos.toByteArray();
105                              } else { */
106                                         /* works in Hinernate3  [       oracle.sql.BLOB aBlob = (oracle.sql.BLOB) ((org.hibernate.lob.SerializableBlob) fileFromDB
107                                                                         .get(0)).getWrappedBlob();
108                                                         allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length()); ] */
109                             // }
110                                         
111                                         Object fileFromDBType = fileFromDB.get(0);
112                                         if(fileFromDBType instanceof byte[] ) // postgres
113                                                 allBytesInBlob = (byte[]) fileFromDB.get(0);
114                                         else if (fileFromDBType instanceof Blob ) // oracle
115                                                 allBytesInBlob = ((Blob) fileFromDB.get(0)).getBytes(1, (int) ((Blob) fileFromDB.get(0)).length());
116                                         
117                                         
118                                         
119                                 } catch (Exception e) {
120                                         logger.error(EELFLoggerDelegate.debugLogger, ("An exception has occurred: " + e.getMessage()));
121                                         throw (e);
122                                 }
123
124                         } else {
125                                 logger.error(EELFLoggerDelegate.debugLogger, ("ERROR: No BLOB returned from DB..."));
126                                 throw (new Exception("ERROR: No BLOB returned from DB..."));
127                         }
128
129                         serveFile(response, allBytesInBlob, fname);
130                         return null;
131                 } catch (Exception e) {
132                         logger.error(EELFLoggerDelegate.debugLogger, ("Exception occurred..." + e.getMessage()));
133                         Map<String, Object> errView = new HashMap<String, Object>();
134                         errView.put("error", "The requested resource was not found.");
135                         //return new ModelAndView(getExceptionView(), "model", errView);
136                         return null;
137                 }
138
139         }
140
141         private void serveFile(HttpServletResponse response, File inFile)
142                         throws Exception {
143                 OutputStream os = null;
144                 InputStream is = null;
145                 try {
146                         response.reset();
147                         is = new BufferedInputStream(new FileInputStream(inFile));
148                         os = new BufferedOutputStream(response.getOutputStream());
149                         response.setContentLength((int) inFile.length());
150                         response.setContentType("application/octet-stream");
151                         response.setHeader("Content-disposition", "attachment; filename=\""
152                                         + inFile.getName() + "\"");
153                         copyStream(is, os);
154                         os.flush();
155                 } catch (Exception ex) {
156                         if (os == null)
157                                 throw new Exception("Could not open output stream for file ");
158                         if (is == null)
159                                 throw new Exception("Could not open input stream for file ");
160                 } finally {
161                         if (os != null) {
162                                 os.close();
163                         }
164                         if (is != null)
165                                 is.close();
166                 }
167         }
168
169         private void serveFile(HttpServletResponse response, byte[] outStream,
170                         String name) throws Exception {
171                 OutputStream os = null;
172                 InputStream is = null;
173                 try {
174                         response.reset();
175                         response.setContentLength((int) outStream.length);
176                         response.setContentType("application/octet-stream");
177                         response.setHeader("Content-disposition", "attachment; filename=\""
178                                         + name + "\"");
179                         copyStream(response, outStream);
180                 } catch (Exception ex) {
181                         if (os == null)
182                                 throw new Exception("Could not open output stream for file ");
183                         if (is == null)
184                                 throw new Exception("Could not open input stream for file ");
185                 } finally {
186                         if (os != null) {
187                                 os.close();
188                         }
189                         if (is != null)
190                                 is.close();
191                 }
192         }
193
194         private int copyStream(InputStream in, OutputStream out) throws IOException {
195                 int bytes, totalBytes = 0;
196
197                 byte[] b = new byte[4096];
198
199                 while ((bytes = in.read(b, 0, b.length)) != -1) {
200                         totalBytes += bytes;
201                         out.write(b, 0, bytes);
202                 }
203                 return totalBytes;
204         }
205
206         private int copyStream(HttpServletResponse response, byte[] outStream)
207                         throws IOException {
208                 
209                 OutputStream os = new BufferedOutputStream(response.getOutputStream());
210                 os.write(outStream);
211                 os.flush();
212                 return outStream.length;
213         }
214
215         public DataAccessService getDataAccessService() {
216                 return dataAccessService;
217         }
218
219         public void setDataAccessService(DataAccessService dataAccessService) {
220                 this.dataAccessService = dataAccessService;
221         }
222
223 }