Title: [713] trunk/rails-integration/src: Fix PUT, DELETE, etc requests not being passed on to RailsServlet.
Revision
713
Author
tantalon
Date
2007-08-24 23:47:44 -0400 (Fri, 24 Aug 2007)

Log Message

Fix PUT, DELETE, etc requests not being passed on to RailsServlet.
Add support for HEAD to FileServlet.

Modified Paths

Diff

Modified: trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java (712 => 713)


--- trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java	2007-08-24 08:18:32 UTC (rev 712)
+++ trunk/rails-integration/src/main/java/org/jruby/webapp/FileServlet.java	2007-08-25 03:47:44 UTC (rev 713)
@@ -26,6 +26,14 @@
  */
 public class FileServlet extends HttpServlet {
 
+    private static final String METHOD_DELETE = "DELETE";
+    private static final String METHOD_HEAD = "HEAD";
+    private static final String METHOD_GET = "GET";
+    private static final String METHOD_OPTIONS = "OPTIONS";
+    private static final String METHOD_POST = "POST";
+    private static final String METHOD_PUT = "PUT";
+    private static final String METHOD_TRACE = "TRACE";
+
 	public static final String FALLBACK_SERVLET_PROPERTY = "files.default";
 
 	private static final String[] DEFAULT_WELCOME_FILES = {"index.html", "index.htm"};
@@ -189,7 +197,7 @@
 	/**
 	 * Transfer the file.
 	 */
-	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 		try {
 			// check the file and open it
 			File fileLocation = getFile(request);
@@ -231,8 +239,17 @@
 			String contentType = guessContentTypeFromName(fileLocation.getName());
 			response.setContentType(contentType);
 
-			// transfer the content
-			sendFile(fileLocation, response);
+			if (request.getMethod().equals(METHOD_HEAD)) {
+				// head requests don't send the body
+			} else if (request.getMethod().equals(METHOD_GET) || request.getMethod().equals(METHOD_POST)) {
+				// transfer the content
+				sendFile(fileLocation, response);
+			} else {
+				// anything else cannot be processed on the file
+				// alternatively we could forward to rails, but this
+				// approach is probably more consistent with other web servers
+				response.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED);
+			}
 
 		} catch (NotModifiedException e) {
 			response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
@@ -271,13 +288,6 @@
 	}
 
 	/**
-	 * 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 guessContentTypeFromName(String fileName) {

Modified: trunk/rails-integration/src/test/java/org/jruby/webapp/FileServletTest.java (712 => 713)


--- trunk/rails-integration/src/test/java/org/jruby/webapp/FileServletTest.java	2007-08-24 08:18:32 UTC (rev 712)
+++ trunk/rails-integration/src/test/java/org/jruby/webapp/FileServletTest.java	2007-08-25 03:47:44 UTC (rev 713)
@@ -3,9 +3,10 @@
 import org.xml.sax.SAXException;
 import org.apache.catalina.Context;
 import org.apache.catalina.Wrapper;
-import org.apache.catalina.deploy.ApplicationParameter;
 import java.io.IOException;
 import java.io.File;
+import java.io.InputStream;
+import java.io.ByteArrayInputStream;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletResponse;
 import com.meterware.httpunit.HttpNotFoundException;
@@ -13,6 +14,8 @@
 import com.meterware.httpunit.WebResponse;
 import com.meterware.httpunit.WebRequest;
 import com.meterware.httpunit.GetMethodWebRequest;
+import com.meterware.httpunit.PutMethodWebRequest;
+import com.meterware.httpunit.HeadMethodWebRequest;
 
 /**
  * Test case for FileServlet.
@@ -54,6 +57,15 @@
 		assertTrue(response.getContentLength() > 0);
 	}
 
+	public void testHead() throws ServletException, IOException, SAXException {
+		WebConversation sc = new WebConversation();
+		WebRequest request = new HeadMethodWebRequest(getContextUrl() + "/file.txt");
+		WebResponse response = sc.getResponse(request);
+		assertEquals(200, response.getResponseCode());
+		assertEquals("text/plain", response.getContentType());
+		assertEquals(0, response.getContentLength());
+	}
+
 	public void testFoundHtml() throws ServletException, IOException, SAXException {
 		WebConversation sc = new WebConversation();
 		WebResponse response = sc.getResponse(getContextUrl() + "/cached_file.html");
@@ -65,7 +77,7 @@
 	/**
 	 * Files that are not found are passed on to the default servlet.
 	 */
-	public void testDefaultServletNew() throws IOException, SAXException, ServletException {
+	public void testDefaultServlet() throws IOException, SAXException, ServletException {
 		context.addParameter(FileServlet.FALLBACK_SERVLET_PROPERTY, "mock");
 		addMockServlet();
 
@@ -74,6 +86,18 @@
 		assertEquals(MockServlet.MESSAGE, response.getText());
 	}
 
+	public void testDefaultServletOnPut() throws IOException, SAXException, ServletException {
+		context.addParameter(FileServlet.FALLBACK_SERVLET_PROPERTY, "mock");
+		addMockServlet();
+
+		WebConversation sc = new WebConversation();
+		InputStream source = new ByteArrayInputStream(new byte[0]);
+		WebRequest request = new PutMethodWebRequest(getContextUrl() + "/no_such_file.txt", source, "text/plain");
+		WebResponse response = sc.getResponse(request);
+		assertEquals(MockServlet.PUT_MESSAGE, response.getText());
+	}
+
+
 	public void testNotModified() throws ServletException, IOException, SAXException {
 		WebConversation sc = new WebConversation();
 

Modified: trunk/rails-integration/src/test/java/org/jruby/webapp/MockServlet.java (712 => 713)


--- trunk/rails-integration/src/test/java/org/jruby/webapp/MockServlet.java	2007-08-24 08:18:32 UTC (rev 712)
+++ trunk/rails-integration/src/test/java/org/jruby/webapp/MockServlet.java	2007-08-25 03:47:44 UTC (rev 713)
@@ -11,6 +11,7 @@
 public class MockServlet extends HttpServlet {
 
 	public static final String MESSAGE = "MockServlet says hi";
+	public static final String PUT_MESSAGE = "Put received";
 
 	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 		response.setStatus(200);
@@ -19,4 +20,11 @@
 		out.close();
 	}
 
+	protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+		response.setStatus(200);
+		PrintWriter out = response.getWriter();
+		out.print(PUT_MESSAGE);
+		out.close();
+	}
+
 }
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to