2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalsdk.analytics.system.fusion.controller;
22 * Raptor Blob Extract Servlet
26 import java.io.BufferedInputStream;
27 import java.io.BufferedOutputStream;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
34 import java.util.HashMap;
35 import java.util.List;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpServletResponse;
41 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
42 import org.openecomp.portalsdk.core.service.DataAccessService;
43 import org.springframework.web.servlet.ModelAndView;;
46 public class FileServletController {
48 private DataAccessService dataAccessService;
50 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FileServletController.class);
53 public ModelAndView handleRequestInternal(HttpServletRequest request,
54 HttpServletResponse response) throws Exception {
55 logger.debug(EELFLoggerDelegate.debugLogger, ("FileServletController:: f=" + request.getParameter("f")));
57 String fname = request.getParameter("f");
60 Map params = new HashMap();
61 params.put("fname", fname);
63 logger.debug(EELFLoggerDelegate.debugLogger, ("executing query: select file_blob from cr_report_file_history where file_name = :"
66 List<Object> fileFromDB = (List<Object>) getDataAccessService().executeNamedQuery("getFileWithName", params, null);
68 byte[] allBytesInBlob = null;
70 if (fileFromDB != null && fileFromDB.size() > 0) {
72 logger.debug(EELFLoggerDelegate.debugLogger, ("reading file blob from DB..."));
76 * if(Globals.isWeblogicServer()) {
77 weblogic.jdbc.vendor.oracle.OracleThinBlob aBlob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) ((org.hibernate.lob.SerializableBlob) fileFromDB
78 .get(0)).getWrappedBlob();
79 InputStream inBlob = ((java.sql.Blob) aBlob).getBinaryStream();
80 ByteArrayOutputStream baos = new ByteArrayOutputStream();
81 byte[] buf = new byte[1024];
83 while ((n=inBlob.read(buf))>=0) {
84 baos.write(buf, 0, n);
87 allBytesInBlob = baos.toByteArray();
89 /* works in Hinernate3 [ oracle.sql.BLOB aBlob = (oracle.sql.BLOB) ((org.hibernate.lob.SerializableBlob) fileFromDB
90 .get(0)).getWrappedBlob();
91 allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length()); ] */
94 Object fileFromDBType = fileFromDB.get(0);
95 if(fileFromDBType instanceof byte[] ) // postgres
96 allBytesInBlob = (byte[]) fileFromDB.get(0);
97 else if (fileFromDBType instanceof Blob ) // oracle
98 allBytesInBlob = ((Blob) fileFromDB.get(0)).getBytes(1, (int) ((Blob) fileFromDB.get(0)).length());
102 } catch (Exception e) {
103 logger.error(EELFLoggerDelegate.debugLogger, ("An exception has occurred: " + e.getMessage()));
108 logger.error(EELFLoggerDelegate.debugLogger, ("ERROR: No BLOB returned from DB..."));
109 throw (new Exception("ERROR: No BLOB returned from DB..."));
112 serveFile(response, allBytesInBlob, fname);
114 } catch (Exception e) {
115 logger.error(EELFLoggerDelegate.debugLogger, ("Exception occurred..." + e.getMessage()));
116 Map<String, Object> errView = new HashMap<String, Object>();
117 errView.put("error", "The requested resource was not found.");
118 //return new ModelAndView(getExceptionView(), "model", errView);
124 private void serveFile(HttpServletResponse response, File inFile)
126 OutputStream os = null;
127 InputStream is = null;
130 is = new BufferedInputStream(new FileInputStream(inFile));
131 os = new BufferedOutputStream(response.getOutputStream());
132 response.setContentLength((int) inFile.length());
133 response.setContentType("application/octet-stream");
134 response.setHeader("Content-disposition", "attachment; filename=\""
135 + inFile.getName() + "\"");
138 } catch (Exception ex) {
140 throw new Exception("Could not open output stream for file ");
142 throw new Exception("Could not open input stream for file ");
152 private void serveFile(HttpServletResponse response, byte[] outStream,
153 String name) throws Exception {
154 OutputStream os = null;
155 InputStream is = null;
158 response.setContentLength((int) outStream.length);
159 response.setContentType("application/octet-stream");
160 response.setHeader("Content-disposition", "attachment; filename=\""
162 copyStream(response, outStream);
163 } catch (Exception ex) {
165 throw new Exception("Could not open output stream for file ");
167 throw new Exception("Could not open input stream for file ");
177 private int copyStream(InputStream in, OutputStream out) throws IOException {
178 int bytes, totalBytes = 0;
180 byte[] b = new byte[4096];
182 while ((bytes = in.read(b, 0, b.length)) != -1) {
184 out.write(b, 0, bytes);
189 private int copyStream(HttpServletResponse response, byte[] outStream)
192 OutputStream os = new BufferedOutputStream(response.getOutputStream());
195 return outStream.length;
198 public DataAccessService getDataAccessService() {
199 return dataAccessService;
202 public void setDataAccessService(DataAccessService dataAccessService) {
203 this.dataAccessService = dataAccessService;