00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00063 String suppath = this.getInitParameter("htdocs");
00064 if (suppath != null) {
00065 this.supPath = suppath;
00066 }
00067 }
00068
00070
00071 public String getServletInfo() {
00072 return "servlet similar to a standard httpd";
00073 }
00074
00076
00077
00078
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
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
00096
00097
00098
00099
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
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 }
00134 } else {
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 }
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
00169 if (this.headOnly)
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 }