001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jul 26, 2005
014: * @author Gretchen Moran
015: *
016: */
017:
018: package org.pentaho.ui.servlet;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.pentaho.core.system.PentahoSystem;
030: import org.pentaho.messages.Messages;
031:
032: public class ShowTestResult extends ServletBase {
033:
034: /**
035: *
036: */
037: private static final long serialVersionUID = -360244121499172556L;
038:
039: protected void doGet(HttpServletRequest arg0,
040: HttpServletResponse arg1) throws ServletException,
041: IOException {
042: doPost(arg0, arg1);
043: }
044:
045: private static final Log logger = LogFactory
046: .getLog(ShowTestResult.class);
047:
048: public Log getLogger() {
049: return logger;
050: }
051:
052: protected void doPost(HttpServletRequest req,
053: HttpServletResponse res) throws ServletException,
054: IOException {
055:
056: getPentahoSession(req);
057: String fileName = req.getParameter("file"); //$NON-NLS-1$
058: String extension = req.getParameter("ext"); //$NON-NLS-1$
059:
060: if (fileName == null) {
061: error(Messages
062: .getErrorString("TESTRESULT.ERROR_0001_FILE_PARAMETER_EMPTY")); //$NON-NLS-1$
063: return;
064: }
065: fileName += extension;
066: String filePath;
067: if (fileName.charAt(0) != '/' && fileName.charAt(0) != '\\') {
068: filePath = PentahoSystem.getApplicationContext()
069: .getFileOutputPath("test/tmp/") + fileName; //$NON-NLS-1$
070: } else {
071: filePath = PentahoSystem.getApplicationContext()
072: .getFileOutputPath("test/tmp") + fileName; //$NON-NLS-1$
073: }
074:
075: File file = new File(filePath);
076: if ((!file.exists()) || (!file.isFile())) {
077: error(Messages.getErrorString(
078: "IMAGE.ERROR_0002_FILE_NOT_FOUND", fileName)); //$NON-NLS-1$
079: return;
080: }
081:
082: res.setContentLength((int) file.length());
083:
084: String mimeType = getServletContext().getMimeType(fileName);
085: if ((null != mimeType) && (mimeType.length() > 0)) {
086: res.setContentType(mimeType);
087: }
088:
089: // Open the file and output streams
090: FileInputStream in = new FileInputStream(file);
091: OutputStream out = res.getOutputStream();
092:
093: try {
094: // Copy the contents of the file to the output stream
095: byte[] buf = new byte[1024];
096: int count = 0;
097: while ((count = in.read(buf)) >= 0) {
098: out.write(buf, 0, count);
099: }
100: } finally {
101: in.close();
102: out.close();
103: }
104:
105: }
106:
107: }
|