incubator-deft-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmee...@apache.org
Subject svn commit: r1154398 - in /incubator/deft/sandbox/src: main/java/org/apache/deft/configuration/ main/java/org/apache/deft/example/configuration/ main/java/org/apache/deft/web/ main/java/org/apache/deft/web/http/ test/java/org/apache/deft/configuration/
Date Fri, 05 Aug 2011 22:25:39 GMT
Author: jmeehan
Date: Fri Aug  5 22:25:39 2011
New Revision: 1154398

URL: http://svn.apache.org/viewvc?rev=1154398&view=rev
Log:
DEFT-147 - Prevent generation of ETags unless specifically requested.

Added:
    incubator/deft/sandbox/src/main/java/org/apache/deft/example/configuration/
    incubator/deft/sandbox/src/main/java/org/apache/deft/example/configuration/DeftConfigurationExample.java
  (with props)
Modified:
    incubator/deft/sandbox/src/main/java/org/apache/deft/configuration/Configuration.java
    incubator/deft/sandbox/src/main/java/org/apache/deft/web/Application.java
    incubator/deft/sandbox/src/main/java/org/apache/deft/web/HttpServer.java
    incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpProtocol.java
    incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponse.java
    incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java
    incubator/deft/sandbox/src/test/java/org/apache/deft/configuration/ConfigurationTest.java

Modified: incubator/deft/sandbox/src/main/java/org/apache/deft/configuration/Configuration.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/apache/deft/configuration/Configuration.java?rev=1154398&r1=1154397&r2=1154398&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/apache/deft/configuration/Configuration.java
(original)
+++ incubator/deft/sandbox/src/main/java/org/apache/deft/configuration/Configuration.java
Fri Aug  5 22:25:39 2011
@@ -46,6 +46,11 @@ public class Configuration {
     private String staticDirectory;
 
     /**
+     * Indicates whether ETags should be generated and applied.
+     */
+    private boolean createETags;
+
+    /**
      * Retrieve the package under which <code>RequestHandler</code>
      * implementations are to be found, for example "org.apache.deft".
      * 
@@ -84,4 +89,24 @@ public class Configuration {
     public void setStaticDirectory(String staticDirectory) {
         this.staticDirectory = staticDirectory;
     }
+
+    /**
+     * Determine whether ETags should currently be generated and applied.
+     * 
+     * @return <code>true</code> if they are to be created; <code>false</code>
+     *         otherwise.
+     */
+    public boolean shouldCreateETags() {
+        return createETags;
+    }
+
+    /**
+     * Set whether ETags should be generated and applied.
+     * 
+     * @param createETags <code>true</code> to create them; <code>false</code>
+     *            otherwise.
+     */
+    public void setCreateETags(boolean createETags) {
+        this.createETags = createETags;
+    }
 }

Added: incubator/deft/sandbox/src/main/java/org/apache/deft/example/configuration/DeftConfigurationExample.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/apache/deft/example/configuration/DeftConfigurationExample.java?rev=1154398&view=auto
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/apache/deft/example/configuration/DeftConfigurationExample.java
(added)
+++ incubator/deft/sandbox/src/main/java/org/apache/deft/example/configuration/DeftConfigurationExample.java
Fri Aug  5 22:25:39 2011
@@ -0,0 +1,61 @@
+/*
+ *  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.example.configuration;
+
+import org.apache.deft.configuration.Configuration;
+import org.apache.deft.io.IOLoop;
+import org.apache.deft.web.HttpServer;
+
+/**
+ * Instantiation of server, describing all configuration options.
+ */
+public class DeftConfigurationExample {
+
+    public static void main(String[] args) {
+
+        /*
+         * The only required configuration option is the base package, under
+         * which handlers are found. See the sub-package "handler" for examples
+         * and how to view them as part of this example application.
+         */
+        Configuration configuration = new Configuration();
+        configuration.setHandlerPackage("org.apache.deft.example.handler");
+
+        /*
+         * The directory from which content is retrieved, which defaults to
+         * "static" where not set.
+         */
+        configuration.setStaticDirectory("myStaticDirectory");
+
+        /*
+         * By default, ETags will not be generated and so this option should be
+         * set if they are required.
+         */
+        configuration.setCreateETags(true);
+
+        /*
+         * Create an instance of HttpServer with the given Configuration, bind
+         * to a single port and start.
+         */
+        HttpServer server = new HttpServer(configuration);
+        server.listen(8080);
+        IOLoop.INSTANCE.start();
+    }
+}

Propchange: incubator/deft/sandbox/src/main/java/org/apache/deft/example/configuration/DeftConfigurationExample.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/deft/sandbox/src/main/java/org/apache/deft/web/Application.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/apache/deft/web/Application.java?rev=1154398&r1=1154397&r2=1154398&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/apache/deft/web/Application.java (original)
+++ incubator/deft/sandbox/src/main/java/org/apache/deft/web/Application.java Fri Aug  5 22:25:39
2011
@@ -22,6 +22,7 @@ package org.apache.deft.web;
 import java.util.Map;
 import java.util.regex.Pattern;
 
+import org.apache.deft.configuration.Configuration;
 import org.apache.deft.util.HttpUtil;
 import org.apache.deft.web.handler.BadRequestRequestHandler;
 import org.apache.deft.web.handler.ForbiddenRequestHandler;
@@ -58,6 +59,11 @@ public class Application {
      */
     private String staticContentDir;
 
+    /**
+     * A copy of the <code>Configuration</code> used to create this type.
+     */
+    private Configuration configuration;
+
     public Application(Map<String, RequestHandler> handlers) {
         ImmutableMap.Builder<String, RequestHandler> builder = new ImmutableMap.Builder<String,
RequestHandler>();
         ImmutableMap.Builder<String, RequestHandler> capturingBuilder = new ImmutableMap.Builder<String,
RequestHandler>();
@@ -163,4 +169,23 @@ public class Application {
     void setStaticContentDir(String scd) {
         staticContentDir = scd;
     }
+
+    /**
+     * Set the <code>Configuration</code> for use with this type.
+     * 
+     * @param configuration the <code>Configuration</code> to apply.
+     */
+    public void setConfiguration(Configuration configuration) {
+
+        this.configuration = configuration;
+    }
+
+    /**
+     * Retrieve the <code>Configuration</code> used by this type.
+     * 
+     * @return the current <code>Configuration</code>.
+     */
+    public Configuration getConfiguration() {
+        return configuration;
+    }
 }

Modified: incubator/deft/sandbox/src/main/java/org/apache/deft/web/HttpServer.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/apache/deft/web/HttpServer.java?rev=1154398&r1=1154397&r2=1154398&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/apache/deft/web/HttpServer.java (original)
+++ incubator/deft/sandbox/src/main/java/org/apache/deft/web/HttpServer.java Fri Aug  5 22:25:39
2011
@@ -58,6 +58,7 @@ public class HttpServer {
 
         application = createApplication(configuration.getHandlerPackage());
         application.setStaticContentDir(configuration.getStaticDirectory());
+        application.setConfiguration(configuration);
     }
 
     protected Application createApplication(String packageName) {

Modified: incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpProtocol.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpProtocol.java?rev=1154398&r1=1154397&r2=1154398&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpProtocol.java (original)
+++ incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpProtocol.java Fri Aug
 5 22:25:39 2011
@@ -90,7 +90,10 @@ public class HttpProtocol implements IOH
             ioLoop.addKeepAliveTimeout(clientChannel, Timeout.newKeepAliveTimeout(ioLoop,
clientChannel,
                     KEEP_ALIVE_TIMEOUT));
         }
+
         HttpResponse response = new HttpResponseImpl(this, key, request.isKeepAlive());
+        response.setCreateETag(application.getConfiguration().shouldCreateETags());
+
         RequestHandler rh = application.getHandler(request);
         HttpRequestDispatcher.dispatch(rh, request, response);
 

Modified: incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponse.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponse.java?rev=1154398&r1=1154397&r2=1154398&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponse.java (original)
+++ incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponse.java Fri Aug
 5 22:25:39 2011
@@ -45,6 +45,15 @@ public interface HttpResponse {
     void setHeader(String header, String value);
 
     /**
+     * Set whether ETags should be generated and applied. By default, they are
+     * not.
+     * 
+     * @param create <code>true</code> to generate and apply; <code>false</code>
+     *            otherwise.
+     */
+    void setCreateETag(boolean create);
+
+    /**
      * Add a cookie to response.
      * 
      * @see #setCookie(String, String, Integer, String, String, boolean,
@@ -168,9 +177,10 @@ public interface HttpResponse {
     /**
      * Should only be invoked by third party asynchronous request handlers (or
      * by the Deft framework for synchronous request handlers). If no previous
-     * (explicit) flush is invoked, the "Content-Length" and "Etag" header will
-     * be calculated and inserted to the HTTP response.
+     * (explicit) flush is invoked, the "Content-Length" and (where configured)
+     * "ETag" header will be calculated and inserted to the HTTP response.
      * 
+     * @see #setCreateETag(boolean)
      */
     long finish();
 }

Modified: incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java?rev=1154398&r1=1154397&r2=1154398&view=diff
==============================================================================
--- incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java (original)
+++ incubator/deft/sandbox/src/main/java/org/apache/deft/web/http/HttpResponseImpl.java Fri
Aug  5 22:25:39 2011
@@ -62,6 +62,8 @@ public class HttpResponseImpl implements
     private boolean headersCreated = false;
     private DynamicByteBuffer responseData = DynamicByteBuffer.allocate(WRITE_BUFFER_SIZE);
 
+    private boolean createETag;
+
     public HttpResponseImpl(HttpProtocol protocol, SelectionKey key, boolean keepAlive) {
         this.protocol = protocol;
         this.key = key;
@@ -75,6 +77,12 @@ public class HttpResponseImpl implements
         this.status = status;
     }
 
+    /** @{inheritDoc */
+    @Override
+    public void setCreateETag(boolean create) {
+        createETag = create;
+    }
+
     @Override
     public void setHeader(String header, String value) {
         headers.put(header, value);
@@ -240,7 +248,8 @@ public class HttpResponseImpl implements
     }
 
     private void setEtagAndContentLength() {
-        if (responseData.position() > 0) {
+        
+        if (createETag && responseData.position() > 0) {
             setHeader("Etag", HttpUtil.getEtag(responseData.array()));
         }
         setHeader("Content-Length", String.valueOf(responseData.position()));
@@ -310,5 +319,4 @@ public class HttpResponseImpl implements
 
         return bytesWritten;
     }
-
 }

Modified: incubator/deft/sandbox/src/test/java/org/apache/deft/configuration/ConfigurationTest.java
URL: http://svn.apache.org/viewvc/incubator/deft/sandbox/src/test/java/org/apache/deft/configuration/ConfigurationTest.java?rev=1154398&r1=1154397&r2=1154398&view=diff
==============================================================================
--- incubator/deft/sandbox/src/test/java/org/apache/deft/configuration/ConfigurationTest.java
(original)
+++ incubator/deft/sandbox/src/test/java/org/apache/deft/configuration/ConfigurationTest.java
Fri Aug  5 22:25:39 2011
@@ -21,6 +21,7 @@ package org.apache.deft.configuration;
 
 import static org.apache.deft.configuration.Configuration.DEFAULT_STATIC_DIRECTORY;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 import org.junit.Test;
@@ -58,4 +59,15 @@ public class ConfigurationTest {
         configuration.setStaticDirectory("files");
         assertEquals("files", configuration.getStaticDirectory());
     }
+
+    @Test
+    public void testShouldCreateETags() {
+
+        Configuration configuration = new Configuration();
+
+        assertFalse(configuration.shouldCreateETags());
+
+        configuration.setCreateETags(true);
+        assertTrue(configuration.shouldCreateETags());
+    }
 }



Mime
View raw message