KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > servlet > ShowTestResult


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 26, 2005
14  * @author Gretchen Moran
15  *
16  */

17
18 package org.pentaho.ui.servlet;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import javax.servlet.ServletException JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.pentaho.core.system.PentahoSystem;
30 import org.pentaho.messages.Messages;
31
32 public class ShowTestResult extends ServletBase {
33
34     /**
35      *
36      */

37     private static final long serialVersionUID = -360244121499172556L;
38
39     protected void doGet(HttpServletRequest JavaDoc arg0, HttpServletResponse JavaDoc arg1) throws ServletException JavaDoc, IOException JavaDoc {
40         doPost(arg0, arg1);
41     }
42
43     private static final Log logger = LogFactory.getLog(ShowTestResult.class);
44
45     public Log getLogger() {
46         return logger;
47     }
48
49     protected void doPost(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) throws ServletException JavaDoc, IOException JavaDoc {
50
51         getPentahoSession(req);
52         String JavaDoc fileName = req.getParameter("file"); //$NON-NLS-1$
53
String JavaDoc extension = req.getParameter("ext"); //$NON-NLS-1$
54

55         if (fileName == null) {
56             error(Messages.getErrorString("TESTRESULT.ERROR_0001_FILE_PARAMETER_EMPTY")); //$NON-NLS-1$
57
return;
58         }
59         fileName += extension;
60         String JavaDoc filePath;
61         if (fileName.charAt(0) != '/' && fileName.charAt(0) != '\\') {
62             filePath = PentahoSystem.getApplicationContext().getFileOutputPath("test/tmp/") + fileName; //$NON-NLS-1$
63
} else {
64             filePath = PentahoSystem.getApplicationContext().getFileOutputPath("test/tmp") + fileName; //$NON-NLS-1$
65
}
66
67         File JavaDoc file = new File JavaDoc(filePath);
68         if ((!file.exists()) || (!file.isFile())) {
69             error(Messages.getErrorString("IMAGE.ERROR_0002_FILE_NOT_FOUND", fileName)); //$NON-NLS-1$
70
return;
71         }
72
73         res.setContentLength((int) file.length());
74
75         String JavaDoc mimeType = getServletContext().getMimeType(fileName);
76         if ((null != mimeType) && (mimeType.length() > 0)) {
77             res.setContentType(mimeType);
78         }
79
80         // Open the file and output streams
81
FileInputStream JavaDoc in = new FileInputStream JavaDoc(file);
82         OutputStream JavaDoc out = res.getOutputStream();
83
84         try {
85             // Copy the contents of the file to the output stream
86
byte[] buf = new byte[1024];
87             int count = 0;
88             while ((count = in.read(buf)) >= 0) {
89                 out.write(buf, 0, count);
90             }
91         } finally {
92             in.close();
93             out.close();
94         }
95
96     }
97
98 }
99
Popular Tags