Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

services/org/openmobileis/services/servlet/FileServlet.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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 
00053         boolean headOnly = false;
00054         private String supPath = "";
00055 
00057         public FileServlet() {
00058         }
00059         
00060     public void init() throws ServletException {
00061         
00062         // This property gives FileServlet' sup. path.
00063         String suppath = this.getInitParameter("htdocs");
00064         if (suppath != null) {
00065             this.supPath = suppath;
00066         }
00067     }
00068 
00070         // copyright of the servlet.
00071         public String getServletInfo() {
00072                 return "servlet similar to a standard httpd";
00073         }
00074 
00076         // @param req the servlet request
00077         // @param req the servlet response
00078         // @exception ServletException when an exception has occurred
00079         public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
00080                 if (req.getMethod().equalsIgnoreCase("get"))
00081                         headOnly = false;
00082                 else
00083                         headOnly = true;
00084                 //String path = req.getServletPath();
00085                 String path = req.getPathInfo();
00086                 if (path == null || path.charAt(0) != '/') {
00087                         res.sendError(HttpServletResponse.SC_BAD_REQUEST);
00088                         return;
00089                 }
00090                 if (path.indexOf("/../") != -1 || path.endsWith("/..")) {
00091                         res.sendError(HttpServletResponse.SC_FORBIDDEN);
00092                         return;
00093                 }
00094 
00095                 // Make a version without the leading /.
00096 /*              String pathname = path.substring(1);
00097                 if (pathname.length() == 0)
00098                         pathname = "./";*/
00099                 // Try to find the directory or the file and store in HttpServletResponse
00100                 managePathname(req, res, headOnly, path, path);
00101         }
00102 
00103         private void managePathname(
00104                         HttpServletRequest req,
00105                         HttpServletResponse res,
00106                         boolean headOnly,
00107                         String path,
00108                         String pathname)
00109                         throws IOException {
00110                 String filename = pathname;
00111                 filename = getServletConfig().getServletContext().getRealPath(this.supPath+filename);
00112 
00113                 //translate file to system local.
00114                 IntlResourceManager resourceManager =
00115                         IntlResourceManager.getManager();
00116                 filename = resourceManager.getLocalizedFileName(filename);
00117 
00118                 File file = new File(filename);
00119                 if (file.exists()) {
00120                         if (!file.isDirectory())
00121                                 serveFile(req, res, headOnly, path, filename, file);
00122                         else {
00123                                 String indexFilename = null;
00124                                 if (pathname.charAt(pathname.length() - 1) == '/')
00125                                         indexFilename = filename + "index.html";
00126                                 else 
00127                                         indexFilename = filename + File.separatorChar + "index.html";
00128                                 File indexFile = new File(indexFilename);
00129                                 if (indexFile.exists())
00130                                         serveFile(req, res, headOnly, path, indexFilename, indexFile);
00131                                 else
00132                                         res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
00133                         } // end else !file.isDirectory
00134                 } else { // else file.exists
00135                         if (pathname.endsWith("/index.html"))
00136                                 managePathname(
00137                                         req,
00138                                         res,
00139                                         headOnly,
00140                                         path,
00141                                         pathname.substring(0, pathname.length() - 10));
00142                         else if (pathname.equals("index.html"))
00143                                 managePathname(req, res, headOnly, path, "./");
00144                         else
00145                                 res.sendError(HttpServletResponse.SC_NOT_FOUND);
00146                 } // end else
00147         }
00148 
00149         private void serveFile(
00150                         HttpServletRequest req,
00151                         HttpServletResponse res,
00152                         boolean headOnly,
00153                         String path,
00154                         String filename,
00155                         File file)
00156                         throws IOException {
00157                 try {
00158                         if (!file.canRead()) {
00159                                 res.sendError(HttpServletResponse.SC_FORBIDDEN);
00160                                 return;
00161                         }
00162 
00163                         res.setStatus(HttpServletResponse.SC_OK);
00164                         long lastMod = file.lastModified();
00165                         String mimeType = getServletConfig().getServletContext().getMimeType(filename);
00166                         res.setContentType(mimeType);
00167                         res.setContentLength((int) file.length());
00168                 //      res.setDateHeader("Last-modified", lastMod);
00169                         if (this.headOnly) // body is not required
00170                                 return;
00171                         OutputStream out = res.getOutputStream();
00172                         InputStream in = new FileInputStream(file);
00173                         copyStream(in, out);
00174                         in.close();
00175                         out.close();
00176                 } catch (Exception e) {
00177                         e.printStackTrace();
00178                 }
00179 
00180         }
00181 
00183         private void copyStream(InputStream in, OutputStream out) throws IOException {
00184                 byte[] buf = new byte[512];
00185                 int len;
00186                 while ((len = in.read(buf)) != -1) {
00187                         out.write(buf, 0, len);
00188                 }
00189         }
00190 
00191 }

Generated on Thu Oct 6 10:06:32 2005 for OpenMobileIS by  doxygen 1.4.3