From 7f535078ef80a7b7efa3e3325bfccb994fbd00e8 Mon Sep 17 00:00:00 2001 From: "Christopher Lott (cl778h)" Date: Thu, 31 Aug 2017 15:16:38 -0400 Subject: Rename packages to org.onap in 1.4.0-SNAPSHOT 19 - remove openecomp 72 - remediate Sonar scan issues 79 - removed unwanted left menu under Report 90 - apply approved license text Issue: PORTAL-19, PORTAL-72, PORTAL-79, PORTAL-90 Change-Id: I41a0ef5fba623d2242574bd15f2d9fb8029a496c Signed-off-by: Christopher Lott (cl778h) --- .../fusion/controller/FileServletController.java | 223 +++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java (limited to 'ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller') diff --git a/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java new file mode 100644 index 00000000..d3d095b3 --- /dev/null +++ b/ecomp-sdk/epsdk-analytics/src/main/java/org/onap/portalsdk/analytics/system/fusion/controller/FileServletController.java @@ -0,0 +1,223 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal SDK + * =================================================================== + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the “License”); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the “License”); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + */ +package org.onap.portalsdk.analytics.system.fusion.controller; +/** + * Raptor Blob Extract Servlet + * + */ + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.sql.Blob; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.service.DataAccessService; +import org.springframework.web.servlet.ModelAndView;; + + +public class FileServletController { + + private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FileServletController.class); + + private DataAccessService dataAccessService; + + public ModelAndView handleRequestInternal(HttpServletRequest request, + HttpServletResponse response) throws Exception { + logger.debug(EELFLoggerDelegate.debugLogger, ("FileServletController:: f=" + request.getParameter("f"))); + + String fname = request.getParameter("f"); + + try { + Map params = new HashMap(); + params.put("fname", fname); + + logger.debug(EELFLoggerDelegate.debugLogger, ("executing query: select file_blob from cr_report_file_history where file_name = :" + + fname)); + + List fileFromDB = (List) getDataAccessService().executeNamedQuery("getFileWithName", params, null); + + byte[] allBytesInBlob = null; + + if (fileFromDB != null && fileFromDB.size() > 0) { + + logger.debug(EELFLoggerDelegate.debugLogger, ("reading file blob from DB...")); + try { + + /*for weblogic setup + * if(Globals.isWeblogicServer()) { + weblogic.jdbc.vendor.oracle.OracleThinBlob aBlob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) ((org.hibernate.lob.SerializableBlob) fileFromDB + .get(0)).getWrappedBlob(); + InputStream inBlob = ((java.sql.Blob) aBlob).getBinaryStream(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buf = new byte[1024]; + int n = 0; + while ((n=inBlob.read(buf))>=0) { + baos.write(buf, 0, n); + } + inBlob.close(); + allBytesInBlob = baos.toByteArray(); + } else { */ + /* works in Hinernate3 [ oracle.sql.BLOB aBlob = (oracle.sql.BLOB) ((org.hibernate.lob.SerializableBlob) fileFromDB + .get(0)).getWrappedBlob(); + allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length()); ] */ + // } + + Object fileFromDBType = fileFromDB.get(0); + if(fileFromDBType instanceof byte[] ) // postgres + allBytesInBlob = (byte[]) fileFromDB.get(0); + else if (fileFromDBType instanceof Blob ) // oracle + allBytesInBlob = ((Blob) fileFromDB.get(0)).getBytes(1, (int) ((Blob) fileFromDB.get(0)).length()); + + + + } catch (Exception e) { + logger.error(EELFLoggerDelegate.debugLogger, ("An exception has occurred: " + e.getMessage())); + throw (e); + } + + } else { + logger.error(EELFLoggerDelegate.debugLogger, ("ERROR: No BLOB returned from DB...")); + throw (new Exception("ERROR: No BLOB returned from DB...")); + } + + serveFile(response, allBytesInBlob, fname); + return null; + } catch (Exception e) { + logger.error(EELFLoggerDelegate.debugLogger, ("Exception occurred..." + e.getMessage())); + Map errView = new HashMap(); + errView.put("error", "The requested resource was not found."); + //return new ModelAndView(getExceptionView(), "model", errView); + return null; + } + + } + + private void serveFile(HttpServletResponse response, File inFile) + throws Exception { + OutputStream os = null; + InputStream is = null; + try { + response.reset(); + is = new BufferedInputStream(new FileInputStream(inFile)); + os = new BufferedOutputStream(response.getOutputStream()); + response.setContentLength((int) inFile.length()); + response.setContentType("application/octet-stream"); + response.setHeader("Content-disposition", "attachment; filename=\"" + + inFile.getName() + "\""); + copyStream(is, os); + os.flush(); + } catch (Exception ex) { + if (os == null) + throw new Exception("Could not open output stream for file "); + if (is == null) + throw new Exception("Could not open input stream for file "); + } finally { + if (os != null) { + os.close(); + } + if (is != null) + is.close(); + } + } + + private void serveFile(HttpServletResponse response, byte[] outStream, + String name) throws Exception { + OutputStream os = null; + InputStream is = null; + try { + response.reset(); + response.setContentLength((int) outStream.length); + response.setContentType("application/octet-stream"); + response.setHeader("Content-disposition", "attachment; filename=\"" + + name + "\""); + copyStream(response, outStream); + } catch (Exception ex) { + if (os == null) + throw new Exception("Could not open output stream for file "); + if (is == null) + throw new Exception("Could not open input stream for file "); + } finally { + if (os != null) { + os.close(); + } + if (is != null) + is.close(); + } + } + + private int copyStream(InputStream in, OutputStream out) throws IOException { + int bytes, totalBytes = 0; + + byte[] b = new byte[4096]; + + while ((bytes = in.read(b, 0, b.length)) != -1) { + totalBytes += bytes; + out.write(b, 0, bytes); + } + return totalBytes; + } + + private int copyStream(HttpServletResponse response, byte[] outStream) + throws IOException { + + OutputStream os = new BufferedOutputStream(response.getOutputStream()); + os.write(outStream); + os.flush(); + return outStream.length; + } + + public DataAccessService getDataAccessService() { + return dataAccessService; + } + + public void setDataAccessService(DataAccessService dataAccessService) { + this.dataAccessService = dataAccessService; + } + +} -- cgit 1.2.3-korg