Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java (656 => 657)
--- trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java 2007-06-22 23:30:34 UTC (rev 656)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java 2007-06-25 09:47:25 UTC (rev 657)
@@ -18,31 +18,31 @@
*/
public class FileServlet extends HttpServlet {
- public static final String FALLBACK_SERVLET_PROPERTY = "files.default";
+ public static final String FALLBACK_SERVLET_PROPERTY = "files.default";
- private static final String[] DEFAULT_WELCOME_FILES = { "index.jsp", "index.html", "index.htm" };
+ private static final String[] DEFAULT_WELCOME_FILES = {"index.jsp", "index.html", "index.htm"};
- private int bufferSize = 1024;
+ private int bufferSize = 1024;
- private String rootPath;
+ private String rootPath;
- private String directory;
+ private String directory;
- private RequestDispatcher defaultServletDispatcher;
+ private RequestDispatcher defaultServletDispatcher;
- /**
- * Initialize the servlet, and determine the webapp root.
- */
- public void init() throws ServletException {
- // determine the root of this webapp
- ServletContext context = getServletContext();
- rootPath = context.getRealPath("/");
- if (rootPath == null) {
- throw new ServletException("Cannot find the real path of this webapp, probably using a non-extracted WAR");
- }
- if (rootPath.endsWith("/")) {
- rootPath = rootPath.substring(0, rootPath.length() - 1);
- }
+ /**
+ * Initialize the servlet, and determine the webapp root.
+ */
+ public void init() throws ServletException {
+ // determine the root of this webapp
+ ServletContext context = getServletContext();
+ rootPath = context.getRealPath("/");
+ if (rootPath == null) {
+ throw new ServletException("Cannot find the real path of this webapp, probably using a non-extracted WAR");
+ }
+ if (rootPath.endsWith("/")) {
+ rootPath = rootPath.substring(0, rootPath.length() - 1);
+ }
directory = getServletConfig().getInitParameter("directory");
if (directory == null) {
@@ -50,145 +50,149 @@
}
// check for default fallback servlet
- String defaultServletName = getServletConfig().getInitParameter("defaultServlet");
+ String defaultServletName = getServletConfig().getInitParameter("defaultServlet");
if (defaultServletName == null) {
defaultServletName = context.getInitParameter(FileServlet.FALLBACK_SERVLET_PROPERTY);
}
if (defaultServletName != null && defaultServletName.length() != 0) {
- defaultServletDispatcher = context.getNamedDispatcher(defaultServletName);
- }
- }
+ defaultServletDispatcher = context.getNamedDispatcher(defaultServletName);
+ }
+ }
- public String[] getWelcomeFiles() {
- String[] welcomeFiles = (String[])getServletContext().getAttribute("org.apache.catalina.WELCOME_FILES");
- if (welcomeFiles == null) welcomeFiles = DEFAULT_WELCOME_FILES;
- return welcomeFiles;
- }
-
- protected String resolvePath(String relativePath) {
+ public void setDirectory(String directory) {
+ this.directory = directory;
+ }
+
+ public String[] getWelcomeFiles() {
+ String[] welcomeFiles = (String[]) getServletContext().getAttribute("org.apache.catalina.WELCOME_FILES");
+ if (welcomeFiles == null) welcomeFiles = DEFAULT_WELCOME_FILES;
+ return welcomeFiles;
+ }
+
+ protected String resolvePath(String relativePath) {
// if it's relative prepend the web apps root, otherwise just use this path
if (new File(directory).isAbsolute()) {
return directory + relativePath;
} else {
return rootPath + File.separator + directory + relativePath;
}
- }
+ }
- /**
- * Transfer the file.
- */
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- FileChannel in = null;
- try {
+ /**
+ * Transfer the file.
+ */
+ protected void doGet(HttpServletRequest request,
+ HttpServletResponse response) throws ServletException, IOException {
+ FileChannel in = null;
+ try {
- // find the location of the file
- String contextPath = request.getContextPath();
- String relativePath = request.getRequestURI().substring(contextPath.length());
+ // find the location of the file
+ String contextPath = request.getContextPath();
+ String relativePath = request.getRequestURI().substring(contextPath.length());
String realPath = resolvePath(relativePath);
realPath = realPath.replaceAll("\\\\", "/").replaceAll("//", "/");
- // check the file and open it
- File file = new File(realPath);
+ // check the file and open it
+ File file = new File(realPath);
- // check for welcome files if it's a directory
- if (file.isDirectory()) {
- String[] welcomeFiles = getWelcomeFiles();
- for(int i=0; i<welcomeFiles.length; i++) {
- File indexFile = new File(file, welcomeFiles[i]);
- if (indexFile.isFile()) {
- file = indexFile;
- break;
- }
- }
- }
+ // check for welcome files if it's a directory
+ if (file.isDirectory()) {
+ String[] welcomeFiles = getWelcomeFiles();
+ for (int i = 0; i < welcomeFiles.length; i++) {
+ File indexFile = new File(file, welcomeFiles[i]);
+ if (indexFile.isFile()) {
+ file = indexFile;
+ break;
+ }
+ }
+ }
- // check for a default extension
- if (!file.isFile()) {
- file = new File(file.getPath() + ".html");
- }
+ // check for a default extension
+ if (!file.isFile()) {
+ file = new File(file.getPath() + ".html");
+ }
- if (file.isFile()) {
- // file was found, all good
- } else if (defaultServletDispatcher != null) {
- // forward request to the default servlet
- defaultServletDispatcher.forward(request, response);
- return;
- } else {
- // file not found
- log("File not found: " + realPath);
- throw new FileNotFoundException(realPath);
- }
+ if (file.isFile()) {
+ // file was found, all good
+ } else if (defaultServletDispatcher != null) {
+ // forward request to the default servlet
+ defaultServletDispatcher.forward(request, response);
+ return;
+ } else {
+ // file not found
+ log("File not found: " + realPath);
+ throw new FileNotFoundException(realPath);
+ }
- // check for modifications
- long ifModifiedSince = request.getDateHeader("If-Modified-Since");
- long lastModified = file.lastModified();
- if (ifModifiedSince != -1 && lastModified < ifModifiedSince) {
- throw new NotModifiedException();
- }
+ // check for modifications
+ long ifModifiedSince = request.getDateHeader("If-Modified-Since");
+ long lastModified = file.lastModified();
+ if (ifModifiedSince != -1 && lastModified < ifModifiedSince) {
+ throw new NotModifiedException();
+ }
- // setup IO streams
- ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
- in = new FileInputStream(file).getChannel();
+ // setup IO streams
+ ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
+ in = new FileInputStream(file).getChannel();
- // start returning the response
- response.setContentType(getContentTypeFor(file.getName()));
- OutputStream out = response.getOutputStream();
+ // start returning the response
+ response.setContentType(getContentTypeFor(file.getName()));
+ OutputStream out = response.getOutputStream();
- // read the bytes, returning them in the response
- while (in.read(buffer) != -1) {
- out.write(buffer.array(), 0, buffer.position());
- buffer.clear();
- }
- out.close();
+ // read the bytes, returning them in the response
+ while (in.read(buffer) != -1) {
+ out.write(buffer.array(), 0, buffer.position());
+ buffer.clear();
+ }
+ out.close();
- } catch (NotModifiedException e) {
- response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
- } catch (FileNotFoundException e) {
- response.sendError(HttpServletResponse.SC_NOT_FOUND);
- } catch (IOException e) {
- response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e
- .getMessage());
- } finally {
- try {
- if (in != null) in.close();
- } catch (IOException ignore) {
- }
- }
- }
+ } catch (NotModifiedException e) {
+ response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
+ } catch (FileNotFoundException e) {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ } catch (IOException e) {
+ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e
+ .getMessage());
+ } finally {
+ try {
+ if (in != null) in.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
- /**
- * Static files treat GET and POST requests the same way.
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
+ /**
+ * Static files treat GET and POST requests the same way.
+ */
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ doGet(request, response);
+ }
- /**
- * Return the content-type the would be returned for this file name.
- */
- public String getContentTypeFor(String fileName) {
- // quick hack for types that are necessary, but not handled
- String lowerName = fileName.toLowerCase();
- if (lowerName.endsWith(".css")) {
- return "text/css";
- } else if (lowerName.endsWith(".js")) {
- return "text/js";
- }
- // everything else
- FileTypeMap typeMap = FileTypeMap.getDefaultFileTypeMap();
- return typeMap.getContentType(fileName);
- }
+ /**
+ * Return the content-type the would be returned for this file name.
+ */
+ public String getContentTypeFor(String fileName) {
+ // quick hack for types that are necessary, but not handled
+ String lowerName = fileName.toLowerCase();
+ if (lowerName.endsWith(".css")) {
+ return "text/css";
+ } else if (lowerName.endsWith(".js")) {
+ return "text/js";
+ }
+ // everything else
+ FileTypeMap typeMap = FileTypeMap.getDefaultFileTypeMap();
+ return typeMap.getContentType(fileName);
+ }
- /**
- * An exception when the source object has not been modified. While this
- * condition is not a failure, it is a break from the normal flow of
- * execution.
- */
- private static class NotModifiedException extends IOException {
- public NotModifiedException() {
- }
- }
+ /**
+ * An exception when the source object has not been modified. While this
+ * condition is not a failure, it is a break from the normal flow of
+ * execution.
+ */
+ private static class NotModifiedException extends IOException {
+ public NotModifiedException() {
+ }
+ }
}