aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmclaugh...@apache.org
Subject git commit: Apply GzipFilter to POSTs as well as GETs
Date Thu, 11 Sep 2014 21:05:24 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master 55fe834a7 -> a75bd4328


Apply GzipFilter to POSTs as well as GETs

Bugs closed: AURORA-697

Reviewed at https://reviews.apache.org/r/25548/


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/a75bd432
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/a75bd432
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/a75bd432

Branch: refs/heads/master
Commit: a75bd4328e85b0ebe935b95f940f0eea60a138b3
Parents: 55fe834
Author: Joshua Cohen <jcohen@twopensource.com>
Authored: Thu Sep 11 13:41:36 2014 -0700
Committer: David McLaughlin <dmclaughlin@twitter.com>
Committed: Thu Sep 11 13:41:36 2014 -0700

----------------------------------------------------------------------
 .../scheduler/http/JettyServerModule.java       | 16 ++++++--
 .../scheduler/http/ServletFilterTest.java       | 42 ++++++++++++++------
 2 files changed, 42 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/a75bd432/src/main/java/org/apache/aurora/scheduler/http/JettyServerModule.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/http/JettyServerModule.java b/src/main/java/org/apache/aurora/scheduler/http/JettyServerModule.java
index 83ba0e4..9d8654b 100644
--- a/src/main/java/org/apache/aurora/scheduler/http/JettyServerModule.java
+++ b/src/main/java/org/apache/aurora/scheduler/http/JettyServerModule.java
@@ -25,7 +25,9 @@ import javax.annotation.Nonnegative;
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.HttpMethod;
 
+import com.google.common.base.Joiner;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
@@ -113,6 +115,10 @@ public class JettyServerModule extends AbstractModule {
       PROPERTY_CONTAINER_REQUEST_FILTERS, GZIPContentEncodingFilter.class.getName(),
       PROPERTY_CONTAINER_RESPONSE_FILTERS, GZIPContentEncodingFilter.class.getName());
 
+  private static final Set<String> GZIP_FILTER_METHODS = ImmutableSet.of(
+      HttpMethod.GET,
+      HttpMethod.POST);
+
   @Override
   protected void configure() {
     bind(Runnable.class)
@@ -152,10 +158,6 @@ public class JettyServerModule extends AbstractModule {
     // Register /api end point
     Registration.registerServlet(binder(), "/api", SchedulerAPIServlet.class, true);
 
-    // NOTE: GzipFilter is applied only to /api instead of globally because the Jersey-managed
-    // servlets have a conflicting filter applied to them.
-    Registration.registerServletFilter(binder(), GzipFilter.class, "/api/*");
-    Registration.registerServletFilter(binder(), GzipFilter.class, "/apibetabeta/*");
     Registration.registerServletFilter(binder(), GuiceFilter.class, "/*");
 
     install(new JerseyServletModule() {
@@ -175,6 +177,12 @@ public class JettyServerModule extends AbstractModule {
         bind(LeaderRedirectFilter.class).in(Singleton.class);
         filterRegex("/scheduler(?:/.*)?").through(LeaderRedirectFilter.class);
 
+        // NOTE: GzipFilter is applied only to /api instead of globally because the Jersey-managed
+        // servlets have a conflicting filter applied to them.
+        bind(GzipFilter.class).in(Singleton.class);
+        filterRegex("/api(?:beta)?(?:/.*)?").through(GzipFilter.class, ImmutableMap.of(
+            "methods", Joiner.on(',').join(GZIP_FILTER_METHODS)));
+
         // Add CORS support for all /api end points.
         if (ENABLE_CORS_SUPPORT.get()) {
           bind(CorsFilter.class).toInstance(new CorsFilter(ENABLE_CORS_FOR.get()));

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/a75bd432/src/test/java/org/apache/aurora/scheduler/http/ServletFilterTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/http/ServletFilterTest.java b/src/test/java/org/apache/aurora/scheduler/http/ServletFilterTest.java
index 2c31df6..c2679c7 100644
--- a/src/test/java/org/apache/aurora/scheduler/http/ServletFilterTest.java
+++ b/src/test/java/org/apache/aurora/scheduler/http/ServletFilterTest.java
@@ -14,6 +14,7 @@
 package org.apache.aurora.scheduler.http;
 
 import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
 
 import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
@@ -23,8 +24,10 @@ import com.sun.jersey.api.client.ClientResponse.Status;
 import com.twitter.thrift.Endpoint;
 import com.twitter.thrift.ServiceInstance;
 
+import org.apache.aurora.gen.Response;
 import org.junit.Test;
 
+import static org.easymock.EasyMock.expect;
 import static org.junit.Assert.assertEquals;
 
 public class ServletFilterTest extends JettyServerModuleTest {
@@ -35,26 +38,41 @@ public class ServletFilterTest extends JettyServerModuleTest {
         .get(ClientResponse.class);
   }
 
-  private void assertContentEncoding(String path, Optional<String> encoding) {
-    assertEquals(encoding.orNull(), get(path).getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
+  protected ClientResponse post(String path, String body) {
+    return getRequestBuilder(path)
+        .header(HttpHeaders.ACCEPT_ENCODING, "gzip")
+        .type(MediaType.TEXT_PLAIN_TYPE)
+        .post(ClientResponse.class, body);
+  }
+
+  private void assertContentEncoding(ClientResponse response, Optional<String> encoding)
{
+    assertEquals(encoding.orNull(), response.getHeaders().getFirst(HttpHeaders.CONTENT_ENCODING));
+  }
+
+  private void assertGzipEncodedGet(String path) {
+    assertContentEncoding(get(path), Optional.of("gzip"));
   }
 
-  private void assertGzipEncoded(String path) {
-    assertContentEncoding(path, Optional.of("gzip"));
+  private void assertGzipEncodedPost(String path, String body) {
+    assertContentEncoding(post(path, body), Optional.of("gzip"));
   }
 
   @Test
   public void testGzipEncoding() throws Exception {
+    expect(thrift.getJobSummary("www-data")).andReturn(new Response()).times(2);
     replayAndStart();
 
-    assertContentEncoding("/", Optional.<String>absent());
-    assertGzipEncoded("/scheduler");
-    assertGzipEncoded("/scheduler/");
-    assertGzipEncoded("/scheduler/role");
-    assertGzipEncoded("/scheduler/role/");
-    assertGzipEncoded("/scheduler/role/env/");
-    assertGzipEncoded("/scheduler/role/env/job");
-    assertGzipEncoded("/scheduler/role/env/job/");
+    assertContentEncoding(get("/"), Optional.<String>absent());
+    assertGzipEncodedGet("/scheduler");
+    assertGzipEncodedGet("/scheduler/");
+    assertGzipEncodedGet("/scheduler/role");
+    assertGzipEncodedGet("/scheduler/role/");
+    assertGzipEncodedGet("/scheduler/role/env/");
+    assertGzipEncodedGet("/scheduler/role/env/job");
+    assertGzipEncodedGet("/scheduler/role/env/job/");
+
+    assertGzipEncodedPost("/api", "[1,\"getJobSummary\",1,0,{\"1\":{\"str\":\"www-data\"}}]");
+    assertGzipEncodedPost("/apibeta/getJobSummary", "{\"role\":\"www-data\"}");
   }
 
   private void assertResponseStatus(String path, Status expectedStatus) {


Mime
View raw message