FileServlet.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  *  2004 Modified by Romain Beaugrand
00027  * 
00028  */
00029 
00030 package org.openmobileis.services.servlet;
00031 
00032 import java.io.*;
00033 
00034 import javax.servlet.ServletException;
00035 import javax.servlet.http.HttpServlet;
00036 import javax.servlet.http.HttpServletRequest;
00037 import javax.servlet.http.HttpServletResponse;
00038 
00039 import org.openmobileis.common.intl.IntlResourceManager;
00040 
00051 public class FileServlet extends HttpServlet {
00052   static final long serialVersionUID = 5521257935120563452L;
00053 
00054         boolean headOnly = false;
00055         private String supPath = "";
00056 
00058         public FileServlet() {
00059         }
00060         
00061     public void init() throws ServletException {
00062         
00063         // This property gives FileServlet' sup. path.
00064         String suppath = this.getInitParameter("htdocs");
00065         if (suppath != null) {
00066             this.supPath = suppath;
00067         }
00068     }
00069 
00071         // copyright of the servlet.
00072         public String getServletInfo() {
00073                 return "servlet similar to a standard httpd";
00074         }
00075 
00077         // @param req the servlet request
00078         // @param req the servlet response
00079         // @exception ServletException when an exception has occurred
00080         public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00081                 if (req.getMethod().equalsIgnoreCase("get"))
00082                         headOnly = false;
00083                 else
00084                         headOnly = true;
00085                 //String path = req.getServletPath();
00086                 String path = req.getPathInfo();
00087                 if (path == null || path.charAt(0) != '/') {
00088                         res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00089                         return;
00090                 }
00091                 if (path.indexOf("/../") != -1 || path.endsWith("/..")) {
00092                         res.sendError(HttpServletResponse.SC_FORBIDDEN);
00093                         return;
00094                 }
00095 
00096                 // Make a version without the leading /.
00097 /*              String pathname = path.substring(1);
00098                 if (pathname.length() == 0)
00099                         pathname = "./";*/
00100                 // Try to find the directory or the file and store in HttpServletResponse
00101                 managePathname(req, res, headOnly, path, path);
00102         }
00103 
00104         private void managePathname(
00105                         HttpServletRequest req,
00106                         HttpServletResponse res,
00107                         boolean headOnly,
00108                         String path,
00109                         String pathname)
00110                         throws IOException {
00111                 String filename = pathname;
00112                 filename = getServletConfig().getServletContext().getRealPath(this.supPath+filename);
00113 
00114                 //translate file to system local.
00115                 IntlResourceManager resourceManager =
00116                         IntlResourceManager.getManager();
00117                 filename = resourceManager.getLocalizedFileName(filename, true);
00118 
00119                 File file = new File(filename);
00120                 if (file.exists()) {
00121                         if (!file.isDirectory())
00122                                 serveFile(req, res, headOnly, path, filename, file);
00123                         else {
00124                                 String indexFilename = null;
00125                                 if (pathname.charAt(pathname.length() - 1) == '/')
00126                                         indexFilename = filename + "index.html";
00127                                 else 
00128                                         indexFilename = filename + File.separatorChar + "index.html";
00129                                 File indexFile = new File(indexFilename);
00130                                 if (indexFile.exists())
00131                                         serveFile(req, res, headOnly, path, indexFilename, indexFile);
00132                                 else
00133                                         res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
00134                         } // end else !file.isDirectory
00135                 } else { // else file.exists
00136                         if (pathname.endsWith("/index.html"))
00137                                 managePathname(
00138                                         req,
00139                                         res,
00140                                         headOnly,
00141                                         path,
00142                                         pathname.substring(0, pathname.length() - 10));
00143                         else if (pathname.equals("index.html"))
00144                                 managePathname(req, res, headOnly, path, "./");
00145                         else
00146                                 res.sendError(HttpServletResponse.SC_NOT_FOUND);
00147                 } // end else
00148         }
00149 
00150         private void serveFile(
00151                         HttpServletRequest req,
00152                         HttpServletResponse res,
00153                         boolean headOnly,
00154                         String path,
00155                         String filename,
00156                         File file)
00157                         throws IOException {
00158                 try {
00159                         if (!file.canRead()) {
00160                                 res.sendError(HttpServletResponse.SC_FORBIDDEN);
00161                                 return;
00162                         }
00163 
00164                         res.setStatus(HttpServletResponse.SC_OK);
00165         //              long lastMod = file.lastModified();
00166                         String mimeType = getServletConfig().getServletContext().getMimeType(filename);
00167                         res.setContentType(mimeType);
00168                         res.setContentLength((int) file.length());
00169                 //      res.setDateHeader("Last-modified", lastMod);
00170                         if (this.headOnly) // body is not required
00171                                 return;
00172                         OutputStream out = res.getOutputStream();
00173                         InputStream in = new FileInputStream(file);
00174                         copyStream(in, out);
00175                         in.close();
00176                         out.close();
00177                 } catch (Exception e) {
00178                         e.printStackTrace();
00179                 }
00180 
00181         }
00182 
00184         private void copyStream(InputStream in, OutputStream out) throws IOException {
00185                 byte[] buf = new byte[512];
00186                 int len;
00187                 while ((len = in.read(buf)) != -1) {
00188                         out.write(buf, 0, len);
00189                 }
00190         }
00191 
00192 }

Generated on Mon Dec 4 11:03:26 2006 for OpenMobileIS by  doxygen 1.5.1-p1