Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/servlet/DBFileServlet.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/servlet/DBFileServlet.java,v 1.4 2003/09/30 15:13:14 joe Exp $
2    * $Revision: 1.4 $
3    * $Date: 2003/09/30 15:13:14 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.servlet;
31  
32  import java.io.IOException;
33  import java.io.InputStream;
34  import java.io.ByteArrayInputStream;
35  
36  import java.util.Enumeration;
37  import java.util.StringTokenizer;
38  import java.sql.Blob;
39  import java.sql.SQLException;
40  import javax.servlet.ServletException;
41  import javax.servlet.http.HttpServlet;
42  import javax.servlet.http.HttpSession;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  import javax.servlet.ServletConfig;
46  import javax.servlet.ServletOutputStream;
47  
48  import com.RuntimeCollective.webapps.EntityBeanStore;
49  import com.RuntimeCollective.webapps.RuntimeParameters;
50  import com.RuntimeCollective.webapps.RuntimeDataSource;
51  import com.RuntimeCollective.webapps.bean.DBFile;
52  import com.RuntimeCollective.webapps.bean.ServletFile;
53  
54  
55  /** Serves DBFiles, based on the request URL.
56   *<p>
57   * The URL should be of the form:
58   * <blockquote><code>http://www.myServer.com/url-pattern/[real|tmp]/1234/filename</code></blockquote>
59   * where <code>url-pattern</code> is specified in <b>web.xml</b> as <code>param.servletFile.url</code>, 
60   * <code>[real|tmp]</code> is either "real", for the real file, or "tmp" for the temporary version,
61   * <code>1234</code> is the DBFile id, and <code>filename</code> is the DBFile's filename.
62   *
63   * @version $Id: DBFileServlet.java,v 1.4 2003/09/30 15:13:14 joe Exp $
64  */
65  public class DBFileServlet extends HttpServlet {
66  
67      /** Write the requested DBFile to the connection
68       *
69       * @param request The servlet request we are processing
70       * @param response The servlet response we are producing
71       *
72       * @exception IOException if an input/output error occurs
73       * @exception ServletException if a servlet error occurs
74       */
75      public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
76    // Output stream for the page
77    ServletOutputStream outStream = response.getOutputStream();
78    
79    RuntimeParameters.logDebug(this, "getContextPath(): "+request.getContextPath());
80    RuntimeParameters.logDebug(this, "getPathInfo(): "+request.getPathInfo());
81    RuntimeParameters.logDebug(this, "getPathTranslated(): "+request.getPathTranslated());
82    RuntimeParameters.logDebug(this, "getQueryString(): "+request.getQueryString());
83    RuntimeParameters.logDebug(this, "getRequestURI(): "+request.getRequestURI());
84    RuntimeParameters.logDebug(this, "getServletPath(): "+request.getServletPath());
85  
86  
87    
88    String path = request.getPathInfo();
89  
90    //getPathInfo(): /real/1234/blah.gif
91  
92    String realOrTmp = null;
93    int id = -1;
94    String filename = null;
95  
96    StringTokenizer st = new StringTokenizer(path, "/");
97    if (st.hasMoreTokens()) {
98        realOrTmp = st.nextToken();
99    }
100   if (st.hasMoreTokens()) {
101       try {
102     id = Integer.parseInt (st.nextToken() );
103       } catch (NumberFormatException e) {
104     RuntimeParameters.logWarn(this, "Bad id specified : " + id);
105       }
106   }
107   if (st.hasMoreTokens()) {
108       filename = st.nextToken();
109   }
110 
111   // make sure we have everything we need
112   if (realOrTmp == null
113       || id == -1
114       || filename == null) {
115       RuntimeParameters.logWarn(this, "DBFileServlet: Bad URL specified : " + path);
116       writeBlankResponse(outStream);
117       return;
118   }
119 
120   // are we serving the real file, or a temporary one?
121   boolean real;
122   if (realOrTmp.equals(ServletFile.TEMP_URL)) {
123       real = false;
124   } else if (realOrTmp.equals(ServletFile.REAL_URL)) {
125       real = true;
126   } else {
127       RuntimeParameters.logWarn(this, "DBFileServlet: first part of path must specify "+ServletFile.TEMP_URL+" or "+ServletFile.REAL_URL+" : " + path);
128       writeBlankResponse(outStream);
129       return;
130   }
131 
132 
133   byte[] fileData;
134   String mimeType;
135 
136   // Get the DBFile
137   DBFile fetchDBFile;
138   try {
139       fetchDBFile = (DBFile) RuntimeParameters.getStore().get("com.RuntimeCollective.webapps.bean.DBFile", id);
140   } catch (RuntimeException e) {
141       log("DBFile not in the database: "+id);
142       writeBlankResponse(outStream);
143       return;
144   }
145   mimeType = fetchDBFile.getMimeType();
146 
147   if (real) {
148       // Real file
149       fileData = fetchDBFile.getFileData();
150   } else {
151       // Temp file
152       fileData = ServletFile.getTempFileData(id);
153   }
154 
155 
156   // Set the response's mime type to match the resource
157   if (!mimeType.equals("")) {
158       RuntimeParameters.log(this, "MIME type : " + mimeType);
159       response.setContentType(mimeType);
160   } else {
161       RuntimeParameters.logWarn(this, "Missing MIME type! This shouldn't have been allowed in the db...");
162       writeBlankResponse(outStream);
163       return;
164   }
165 
166 
167   // serve the file's content
168   ByteArrayInputStream content = new ByteArrayInputStream(fetchDBFile.getFileData());
169   
170   try {    
171       // copy the data from the content to the response
172       byte buf[] = new byte[2048];
173       int size, total = 0;
174       while ((size = content.read(buf, 0, 2048)) > 0) {
175     outStream.write(buf, 0, size);
176     total += size;
177       }
178       outStream.flush();
179       outStream.close();
180       // Set the content length in the header to improve performance
181       response.setContentLength( total );
182   } catch (Exception e) {
183       RuntimeParameters.logWarn(this, "IO error serving a DBFile : " + e);
184       writeBlankResponse(outStream);
185       return;
186   }
187 
188 
189     }
190     
191     /**
192      * Write an empty String to the response
193      * @param outStream the output stream to write the blank response to
194      */
195     private void writeBlankResponse(ServletOutputStream outStream) throws IOException{
196   outStream.print("");
197     }
198     
199 }