2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.analytics.system.fusion.controller;
40 * Raptor Blob Extract Servlet
44 import java.io.BufferedInputStream;
45 import java.io.BufferedOutputStream;
47 import java.io.FileInputStream;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.io.OutputStream;
52 import java.util.HashMap;
53 import java.util.List;
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
59 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
60 import org.onap.portalsdk.core.service.DataAccessService;
61 import org.springframework.web.servlet.ModelAndView;;
64 public class FileServletController {
66 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FileServletController.class);
68 private DataAccessService dataAccessService;
70 public ModelAndView handleRequestInternal(HttpServletRequest request,
71 HttpServletResponse response) throws Exception {
72 logger.debug(EELFLoggerDelegate.debugLogger, ("FileServletController:: f=" + request.getParameter("f")));
74 String fname = request.getParameter("f");
77 Map params = new HashMap();
78 params.put("fname", fname);
80 logger.debug(EELFLoggerDelegate.debugLogger, ("executing query: select file_blob from cr_report_file_history where file_name = :"
83 List<Object> fileFromDB = (List<Object>) getDataAccessService().executeNamedQuery("getFileWithName", params, null);
85 byte[] allBytesInBlob = null;
87 if (fileFromDB != null && fileFromDB.size() > 0) {
89 logger.debug(EELFLoggerDelegate.debugLogger, ("reading file blob from DB..."));
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];
100 while ((n=inBlob.read(buf))>=0) {
101 baos.write(buf, 0, n);
104 allBytesInBlob = baos.toByteArray();
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()); ] */
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());
119 } catch (Exception e) {
120 logger.error(EELFLoggerDelegate.debugLogger, ("An exception has occurred: " + e.getMessage()));
125 logger.error(EELFLoggerDelegate.debugLogger, ("ERROR: No BLOB returned from DB..."));
126 throw (new Exception("ERROR: No BLOB returned from DB..."));
129 serveFile(response, allBytesInBlob, fname);
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);
141 private void serveFile(HttpServletResponse response, File inFile)
143 OutputStream os = null;
144 InputStream is = null;
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() + "\"");
155 } catch (Exception ex) {
157 throw new Exception("Could not open output stream for file ");
159 throw new Exception("Could not open input stream for file ");
169 private void serveFile(HttpServletResponse response, byte[] outStream,
170 String name) throws Exception {
171 OutputStream os = null;
172 InputStream is = null;
175 response.setContentLength((int) outStream.length);
176 response.setContentType("application/octet-stream");
177 response.setHeader("Content-disposition", "attachment; filename=\""
179 copyStream(response, outStream);
180 } catch (Exception ex) {
182 throw new Exception("Could not open output stream for file ");
184 throw new Exception("Could not open input stream for file ");
194 private int copyStream(InputStream in, OutputStream out) throws IOException {
195 int bytes, totalBytes = 0;
197 byte[] b = new byte[4096];
199 while ((bytes = in.read(b, 0, b.length)) != -1) {
201 out.write(b, 0, bytes);
206 private int copyStream(HttpServletResponse response, byte[] outStream)
209 OutputStream os = new BufferedOutputStream(response.getOutputStream());
212 return outStream.length;
215 public DataAccessService getDataAccessService() {
216 return dataAccessService;
219 public void setDataAccessService(DataAccessService dataAccessService) {
220 this.dataAccessService = dataAccessService;