incubator-awf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sleme...@apache.org
Subject svn commit: r1241701 - in /incubator/deft/trunk/src: main/java/org/apache/deft/web/ main/java/org/apache/deft/web/handler/ main/java/org/apache/deft/web/http/ main/java/org/apache/deft/web/http/protocol/ test/java/org/apache/deft/web/ test/java/org/apa...
Date Tue, 07 Feb 2012 23:38:21 GMT
Author: slemesle
Date: Tue Feb  7 23:38:21 2012
New Revision: 1241701

URL: http://svn.apache.org/viewvc?rev=1241701&view=rev
Log:
DEFT-191 - Integrate Continue patch

Added:
    incubator/deft/trunk/src/main/java/org/apache/deft/web/handler/HttpContinueRequestHandler.java
Modified:
    incubator/deft/trunk/src/main/java/org/apache/deft/web/Application.java
    incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpProtocol.java
    incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequest.java
    incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequestImpl.java
    incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java
    incubator/deft/trunk/src/main/java/org/apache/deft/web/http/protocol/HttpStatus.java
    incubator/deft/trunk/src/test/java/org/apache/deft/web/DeftSystemTest.java
    incubator/deft/trunk/src/test/java/org/apache/deft/web/http/protocol/HttpStatusTest.java

Modified: incubator/deft/trunk/src/main/java/org/apache/deft/web/Application.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/main/java/org/apache/deft/web/Application.java?rev=1241701&r1=1241700&r2=1241701&view=diff
==============================================================================
--- incubator/deft/trunk/src/main/java/org/apache/deft/web/Application.java (original)
+++ incubator/deft/trunk/src/main/java/org/apache/deft/web/Application.java Tue Feb  7 23:38:21
2012
@@ -26,6 +26,7 @@ import org.apache.deft.configuration.Con
 import org.apache.deft.util.HttpUtil;
 import org.apache.deft.web.handler.BadRequestRequestHandler;
 import org.apache.deft.web.handler.ForbiddenRequestHandler;
+import org.apache.deft.web.handler.HttpContinueRequestHandler;
 import org.apache.deft.web.handler.NotFoundRequestHandler;
 import org.apache.deft.web.handler.RequestHandler;
 import org.apache.deft.web.handler.RequestHandlerFactory;
@@ -127,6 +128,10 @@ public class Application {
             return ForbiddenRequestHandler.getInstance();
         }
 
+        if (request.expectContinue()) {
+            return HttpContinueRequestHandler.getInstance();
+        }
+
         return rh;
     }
 

Added: incubator/deft/trunk/src/main/java/org/apache/deft/web/handler/HttpContinueRequestHandler.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/main/java/org/apache/deft/web/handler/HttpContinueRequestHandler.java?rev=1241701&view=auto
==============================================================================
--- incubator/deft/trunk/src/main/java/org/apache/deft/web/handler/HttpContinueRequestHandler.java
(added)
+++ incubator/deft/trunk/src/main/java/org/apache/deft/web/handler/HttpContinueRequestHandler.java
Tue Feb  7 23:38:21 2012
@@ -0,0 +1,47 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.deft.web.handler;
+
+import org.apache.deft.web.http.HttpRequest;
+import org.apache.deft.web.http.HttpResponse;
+import org.apache.deft.web.http.protocol.HttpStatus;
+
+public class HttpContinueRequestHandler extends RequestHandler {
+
+	private final static HttpContinueRequestHandler instance = new HttpContinueRequestHandler();
+
+	private HttpContinueRequestHandler() {
+	}
+
+	public static final HttpContinueRequestHandler getInstance() {
+		return instance;
+	}
+
+	@Override
+	public void post(HttpRequest request, HttpResponse response) {
+		response.setStatus(HttpStatus.SUCCESS_CONTINUE);
+	}
+
+	@Override
+	public void put(HttpRequest request, HttpResponse response) {
+		response.setStatus(HttpStatus.SUCCESS_CONTINUE);
+	}
+
+}

Modified: incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpProtocol.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpProtocol.java?rev=1241701&r1=1241700&r2=1241701&view=diff
==============================================================================
--- incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpProtocol.java (original)
+++ incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpProtocol.java Tue Feb
 7 23:38:21 2012
@@ -19,16 +19,18 @@
  */
 package org.apache.deft.web.http;
 
-import static org.apache.deft.web.http.HttpServerDescriptor.KEEP_ALIVE_TIMEOUT;
-import static org.apache.deft.web.http.HttpServerDescriptor.READ_BUFFER_SIZE;
-
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.*;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.FileChannel;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
 import java.util.Map;
 
+import com.google.common.collect.Maps;
 import org.apache.deft.io.IOHandler;
 import org.apache.deft.io.IOLoop;
 import org.apache.deft.io.buffer.DynamicByteBuffer;
@@ -38,8 +40,8 @@ import org.apache.deft.web.Application;
 import org.apache.deft.web.handler.RequestHandler;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
-import com.google.common.collect.Maps;
+import static org.apache.deft.web.http.HttpServerDescriptor.KEEP_ALIVE_TIMEOUT;
+import static org.apache.deft.web.http.HttpServerDescriptor.READ_BUFFER_SIZE;
 
 public class HttpProtocol implements IOHandler {
 
@@ -88,8 +90,8 @@ public class HttpProtocol implements IOH
 
         // Request is null when End-of-Stream have been reached
         // No need to do more things right now
-        if(request != null){
-
+        if(request != null){        	
+        	logger.debug("received request: \n"+request.toString()); 
             if (request.isKeepAlive()) {
                 ioLoop.addKeepAliveTimeout(clientChannel, Timeout.newKeepAliveTimeout(ioLoop,
clientChannel,
                         KEEP_ALIVE_TIMEOUT));
@@ -100,10 +102,9 @@ public class HttpProtocol implements IOH
 
             RequestHandler rh = application.getHandler(request);
             HttpRequestDispatcher.dispatch(rh, request, response);
-
-            // Only close if not async. In that case its up to RH to close it (+
-            // don't close if it's a partial request).
-            if (!rh.isMethodAsynchronous(request.getMethod()) && !(request instanceof
PartialHttpRequest)) {
+            
+            // Only close if not async. In that case its up to RH to close it
+            if (!rh.isMethodAsynchronous(request.getMethod()) ) {
                 response.finish();
             }
         }
@@ -266,7 +267,8 @@ public class HttpProtocol implements IOH
         request.setRemotePort(clientChannel.socket().getPort());
         request.setServerHost(clientChannel.socket().getLocalAddress());
         request.setServerPort(clientChannel.socket().getLocalPort());
-        return (request.isFinished() ? request : null);
+        
+        return (request.isFinished() || request.expectContinue() ? request : null);
     }
 
     @Override

Modified: incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequest.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequest.java?rev=1241701&r1=1241700&r2=1241701&view=diff
==============================================================================
--- incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequest.java (original)
+++ incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequest.java Tue Feb 
7 23:38:21 2012
@@ -105,7 +105,9 @@ public interface HttpRequest {
     public String getParameter(String name);
 
     /**
-     * TODO: JAVADOC, it's unclear to me (jvermillard)
+     * Returns a map of all parameters where each key is a parameter name,
+     * linked value is a Collection of String containing all known values for the parameter.
+     * When the parameter has no value, the collection will be empty.
      * 
      * @return all the request parameters
      */
@@ -179,5 +181,12 @@ public interface HttpRequest {
      * @return <code>true</code> if keep-alive requested
      */
     public boolean isKeepAlive();
+    
+    /**
+     * Check if the request expect a response with 100 Continue header
+     * 
+     * @return
+     */
+    public boolean expectContinue();
 
 }

Modified: incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequestImpl.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequestImpl.java?rev=1241701&r1=1241700&r2=1241701&view=diff
==============================================================================
--- incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequestImpl.java (original)
+++ incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpRequestImpl.java Tue Feb
 7 23:38:21 2012
@@ -36,6 +36,9 @@ import org.apache.deft.io.buffer.Dynamic
 import org.apache.deft.util.ArrayUtil;
 import org.apache.deft.web.http.protocol.HttpVerb;
 
+/**
+ * TODO SLM Cleanup old Parsing code 
+ */
 public class HttpRequestImpl implements HttpRequest {
 
     private IOLoop ioLoop;
@@ -311,7 +314,7 @@ public class HttpRequestImpl implements 
 
     /**
      * Returns a collection of all values associated with the provided
-     * parameter. If no values are found and empty collection is returned.
+     * parameter. If no values are found an empty collection is returned.
      */
     @Override
     public Collection<String> getParameterValues(String name) {
@@ -342,6 +345,11 @@ public class HttpRequestImpl implements 
                 result += key + ":" + value + "\n";
             }
         }
+        if (getBody() != null) {
+        	result += "--- BODY --- \n";
+            result += getBody();
+        }
+        
         return result;
     }
 
@@ -459,5 +467,9 @@ public class HttpRequestImpl implements 
         }
         return res;
     }
+    
+    public boolean expectContinue() {    	
+    	return (bodyBuffer != null && bodyBuffer.position() == 0) && headers.get("expect")
!= null;
+    }
 
 }

Modified: incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java?rev=1241701&r1=1241700&r2=1241701&view=diff
==============================================================================
--- incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java (original)
+++ incubator/deft/trunk/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java Tue
Feb  7 23:38:21 2012
@@ -19,11 +19,10 @@
  */
 package org.apache.deft.web.http;
 
-import static org.apache.deft.web.http.HttpServerDescriptor.WRITE_BUFFER_SIZE;
-
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.nio.MappedByteBuffer;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.FileChannel;
@@ -33,6 +32,10 @@ import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
+import com.google.common.base.CharMatcher;
+import com.google.common.base.Charsets;
+import com.google.common.base.Strings;
+import com.google.common.collect.Maps;
 import org.apache.deft.io.buffer.DynamicByteBuffer;
 import org.apache.deft.util.Closeables;
 import org.apache.deft.util.CookieUtil;
@@ -41,11 +44,7 @@ import org.apache.deft.util.HttpUtil;
 import org.apache.deft.web.http.protocol.HttpStatus;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
-import com.google.common.base.CharMatcher;
-import com.google.common.base.Charsets;
-import com.google.common.base.Strings;
-import com.google.common.collect.Maps;
+import static org.apache.deft.web.http.HttpServerDescriptor.WRITE_BUFFER_SIZE;
 
 public class HttpResponseImpl implements HttpResponse {
 
@@ -213,6 +212,7 @@ public class HttpResponseImpl implements
     public long finish() {
         long bytesWritten = 0;
         SocketChannel clientChannel = (SocketChannel) key.channel();
+        ByteBuffer buffer = null;
 
         if (key.attachment() instanceof MappedByteBuffer) {
             MappedByteBuffer mbb = (MappedByteBuffer) key.attachment();
@@ -224,9 +224,7 @@ public class HttpResponseImpl implements
                     Closeables.closeQuietly(key.channel());
                 }
             }
-            if (!mbb.hasRemaining()) {
-                protocol.closeOrRegisterForRead(key);
-            }
+            buffer = mbb;
         } else {
             if (clientChannel.isOpen()) {
                 if (!headersCreated) {
@@ -239,14 +237,14 @@ public class HttpResponseImpl implements
             // false)
             // (b) no DBB is attached (never had to register for write)
             if (key.attachment() instanceof DynamicByteBuffer) {
-                DynamicByteBuffer dbb = (DynamicByteBuffer) key.attachment();
-                if (!dbb.hasRemaining()) {
-                    protocol.closeOrRegisterForRead(key);
-                }
-            } else {
-                protocol.closeOrRegisterForRead(key);
+                buffer = ((DynamicByteBuffer) key.attachment()).getByteBuffer();
             }
         }
+        // Do Not Close the socket if there is more data to send or this is a CONTINUE
+        if ((buffer != null && buffer.hasRemaining()) || HttpStatus.SUCCESS_CONTINUE.equals(status))
{
+            return bytesWritten;
+        }
+        protocol.closeOrRegisterForRead(key);
         return bytesWritten;
     }
 

Modified: incubator/deft/trunk/src/main/java/org/apache/deft/web/http/protocol/HttpStatus.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/main/java/org/apache/deft/web/http/protocol/HttpStatus.java?rev=1241701&r1=1241700&r2=1241701&view=diff
==============================================================================
--- incubator/deft/trunk/src/main/java/org/apache/deft/web/http/protocol/HttpStatus.java (original)
+++ incubator/deft/trunk/src/main/java/org/apache/deft/web/http/protocol/HttpStatus.java Tue
Feb  7 23:38:21 2012
@@ -24,10 +24,14 @@ package org.apache.deft.web.http.protoco
  */
 public enum HttpStatus {
 
+	/**
+	 * 100 - CONTINUE
+	 */
+	SUCCESS_CONTINUE(100, "HTTP/1.1 100 Continue"),
     /**
      * 200 - OK
      */
-    SUCCESS_OK(200, "HTTP/1.1 200 OK"),
+    SUCCESS_OK(200, "HTTP/1.1 200 OK"),    
     /**
      * 201 - Created
      */

Modified: incubator/deft/trunk/src/test/java/org/apache/deft/web/DeftSystemTest.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/test/java/org/apache/deft/web/DeftSystemTest.java?rev=1241701&r1=1241700&r2=1241701&view=diff
==============================================================================
--- incubator/deft/trunk/src/test/java/org/apache/deft/web/DeftSystemTest.java (original)
+++ incubator/deft/trunk/src/test/java/org/apache/deft/web/DeftSystemTest.java Tue Feb  7
23:38:21 2012
@@ -310,6 +310,28 @@ public class DeftSystemTest {
     }
 
     @Test
+    public void postWithContinueTest() throws ClientProtocolException, IOException {
+
+        List<Header> headers = new LinkedList<Header>();
+        headers.add(new BasicHeader("Connection", "Close"));
+
+        HttpParams params = new BasicHttpParams();
+        params.setParameter("http.default-headers", headers);
+        params.setParameter("http.protocol.expect-continue", Boolean.TRUE);
+        HttpClient httpclient = new DefaultHttpClient(params);
+        HttpPost httppost = new HttpPost("http://localhost:" + PORT + "/post");
+        HttpResponse response = httpclient.execute(httppost);
+
+        assertNotNull(response);
+        assertEquals(HttpStatus.SUCCESS_OK.code(), response.getStatusLine().getStatusCode());
+        assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
+        assertEquals("OK", response.getStatusLine().getReasonPhrase());
+
+        String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
+        assertEquals("post", payLoad);
+    }
+
+    @Test
     public void putTest() throws ClientProtocolException, IOException {
 
         List<Header> headers = new LinkedList<Header>();

Modified: incubator/deft/trunk/src/test/java/org/apache/deft/web/http/protocol/HttpStatusTest.java
URL: http://svn.apache.org/viewvc/incubator/deft/trunk/src/test/java/org/apache/deft/web/http/protocol/HttpStatusTest.java?rev=1241701&r1=1241700&r2=1241701&view=diff
==============================================================================
--- incubator/deft/trunk/src/test/java/org/apache/deft/web/http/protocol/HttpStatusTest.java
(original)
+++ incubator/deft/trunk/src/test/java/org/apache/deft/web/http/protocol/HttpStatusTest.java
Tue Feb  7 23:38:21 2012
@@ -19,18 +19,15 @@
  */
 package org.apache.deft.web.http.protocol;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
 import org.junit.Test;
+import static org.junit.Assert.*;
 
 /**
  * Test cases for {@link HttpStatus}.
  */
 public class HttpStatusTest {
 
-    private static int STATUS_CODE_COUNT = 37;
+    private static int STATUS_CODE_COUNT = 38;
 
     @Test
     public void testCount() {
@@ -42,7 +39,7 @@ public class HttpStatusTest {
 
         for (final HttpStatus status : HttpStatus.values()) {
             if (status.name().startsWith("SUCCESS")) {
-                assertTrue("Incorrect: " + status.name(), status.code() >= 200 &&
status.code() < 300);
+                assertTrue("Incorrect: " + status.name(), status.code() >= 100 &&
status.code() < 300);
             } else if (status.name().startsWith("REDIRECTION")) {
                 assertTrue("Incorrect: " + status.name(), status.code() >= 300 &&
status.code() < 400);
             } else if (status.name().startsWith("CLIENT_ERROR")) {



Mime
View raw message