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 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
00064 String suppath = this.getInitParameter("htdocs");
00065 if (suppath != null) {
00066 this.supPath = suppath;
00067 }
00068 }
00069
00071
00072 public String getServletInfo() {
00073 return "servlet similar to a standard httpd";
00074 }
00075
00077
00078
00079
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
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
00097
00098
00099
00100
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
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 }
00135 } else {
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 }
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
00166 String mimeType = getServletConfig().getServletContext().getMimeType(filename);
00167 res.setContentType(mimeType);
00168 res.setContentLength((int) file.length());
00169
00170 if (this.headOnly)
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 }