KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > servlets > FileServlet


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/servlets/FileServlet.java,v 1.23 2005/02/15 20:58:23 hkollmann Exp $
3  * $Revision: 1.23 $
4  * $Date: 2005/02/15 20:58:23 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.servlets;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.dbforms.config.DbFormsConfig;
30 import org.dbforms.config.DbFormsConfigRegistry;
31 import org.dbforms.config.Field;
32 import org.dbforms.config.FieldTypes;
33 import org.dbforms.config.Table;
34
35 import org.dbforms.util.SqlUtil;
36 import org.dbforms.util.StringUtil;
37 import org.dbforms.util.Util;
38 import org.dbforms.util.FileHolder;
39
40 import java.io.IOException JavaDoc;
41 import java.io.InputStream JavaDoc;
42
43 import java.sql.Connection JavaDoc;
44 import java.sql.PreparedStatement JavaDoc;
45 import java.sql.ResultSet JavaDoc;
46 import java.sql.SQLException JavaDoc;
47
48 import javax.servlet.ServletException JavaDoc;
49 import javax.servlet.ServletOutputStream JavaDoc;
50 import javax.servlet.http.HttpServlet JavaDoc;
51 import javax.servlet.http.HttpServletRequest JavaDoc;
52 import javax.servlet.http.HttpServletResponse JavaDoc;
53
54 /**
55  * #fixme - add appropriate exception-handling..
56  *
57  * @author joe peer
58  */

59 public class FileServlet extends HttpServlet JavaDoc {
60     // logging category for this class
61
private static Log logCat = LogFactory.getLog(FileServlet.class.getName());
62
63     /**
64      * Process the HTTP Get request
65      *
66      * @param request
67      * Description of the Parameter
68      * @param response
69      * Description of the Parameter
70      *
71      * @exception ServletException
72      * Description of the Exception
73      * @exception IOException
74      * Description of the Exception
75      */

76     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
77             throws ServletException JavaDoc, IOException JavaDoc {
78
79         // take Config-Object from application context - this object should have
80
// been
81
// initalized by Config-Servlet on Webapp/server-startup!
82
DbFormsConfig config = null;
83         try {
84             config = DbFormsConfigRegistry.instance().lookup();
85         } catch (Exception JavaDoc e) {
86             logCat.error(e);
87             throw new ServletException JavaDoc(e);
88         }
89
90         try {
91             String JavaDoc tf = request.getParameter("tf");
92             String JavaDoc keyValuesStr = request.getParameter("keyval");
93             if (!Util.isNull(keyValuesStr) && !("null".equals(keyValuesStr))) {
94
95                 int tableId = Integer.parseInt(StringUtil.getEmbeddedString(tf,
96                         0, '_'));
97                 Table table = config.getTable(tableId);
98                 int fieldId = Integer.parseInt(StringUtil.getEmbeddedString(tf,
99                         1, '_'));
100                 Field field = table.getField(fieldId);
101
102                 StringBuffer JavaDoc queryBuf = new StringBuffer JavaDoc();
103                 String JavaDoc dbConnectionName = request.getParameter("invname_"
104                         + tableId);
105                 Connection JavaDoc con = config.getConnection(dbConnectionName);
106
107                 // JPeer 03/2004 - optional parameter
108
String JavaDoc nameField = request.getParameter("nf");
109
110                 InputStream JavaDoc is = null;
111                 String JavaDoc fileName = null;
112
113                 queryBuf.append("SELECT ");
114                 queryBuf.append(field.getName());
115
116                 if (nameField != null) {
117                     queryBuf.append(", ");
118                     queryBuf.append(nameField);
119                 }
120
121                 queryBuf.append(" FROM ");
122                 queryBuf.append(table.getName());
123                 queryBuf.append(" WHERE ");
124                 queryBuf.append(table.getWhereClauseForKeyFields());
125
126                 // example: SELECT imageNameField FROM myTable WHERE myTable.key
127
// = ?
128
logCat.info("::doGet - query is [" + queryBuf + "]");
129
130                 PreparedStatement JavaDoc ps = con
131                         .prepareStatement(queryBuf.toString());
132                 table.populateWhereClauseWithKeyFields(keyValuesStr, ps, 1);
133
134                 ResultSet JavaDoc rs = ps.executeQuery();
135
136                 if (rs.next()) {
137                     // use the filesystem;
138
if (field.getType() == FieldTypes.DISKBLOB) {
139                         fileName = rs.getString(1);
140                         is = SqlUtil.readDiskBlob(fileName, field
141                                 .getDirectory(), request
142                                 .getParameter("defaultValue"));
143                     }
144                     // use the rdbms;
145
else if (field.getType() == FieldTypes.BLOB) {
146                         // if no fileholder is used (new BLOB model)
147
if (nameField != null) {
148                             fileName = rs.getString(2);
149                             is = SqlUtil.readDbFieldBlob(rs);
150                         } else {
151                             FileHolder fh = SqlUtil.readFileHolderBlob(rs);
152                             is = fh.getInputStreamFromBuffer();
153                             fileName = fh.getFileName();
154                         }
155                     }
156                 } else {
157                     logCat.info("::doGet - we have got no result using query "
158                             + queryBuf);
159                 }
160
161                 if (is != null) {
162                     writeToClient(request, response, fileName, is);
163                 }
164
165                 SqlUtil.closeConnection(con);
166             }
167         } catch (SQLException JavaDoc sqle) {
168             logCat.error("::doGet - SQL exception", sqle);
169         }
170     }
171
172     /**
173      * Process the HTTP Post request
174      *
175      * @param request
176      * Description of the Parameter
177      * @param response
178      * Description of the Parameter
179      *
180      * @exception ServletException
181      * Description of the Exception
182      * @exception IOException
183      * Description of the Exception
184      */

185     public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
186             throws ServletException JavaDoc, IOException JavaDoc {
187         doGet(request, response);
188     }
189
190     /**
191      * Write the content of the input file to the client.
192      *
193      * @param request
194      * DOCUMENT ME!
195      * @param response
196      * Description of the Parameter
197      * @param fileName
198      * Description of the Parameter
199      * @param is
200      * Description of the Parameter
201      *
202      * @exception IOException
203      * Description of the Exception
204      */

205     private void writeToClient(HttpServletRequest JavaDoc request,
206             HttpServletResponse JavaDoc response, String JavaDoc fileName, InputStream JavaDoc is)
207             throws IOException JavaDoc {
208         String JavaDoc contentType = request.getSession().getServletContext()
209                 .getMimeType(fileName);
210         logCat.info("::writeToClient- writing to client:" + fileName + " ct="
211                 + contentType);
212
213         if (!Util.isNull(contentType)) {
214             response.setContentType(contentType);
215         }
216
217         response.setHeader("Cache-control", "private"); // w/o this MSIE fails
218

219         // to "Open" the file
220
response.setHeader("Content-Disposition", "attachment; fileName=\""
221                 + fileName + "\"");
222
223         ServletOutputStream JavaDoc out = response.getOutputStream();
224         byte[] b = new byte[1024];
225         int read;
226
227         while ((read = is.read(b)) != -1)
228             out.write(b, 0, read);
229
230         out.close();
231     }
232 }
233
Popular Tags