Skip to content

Commit

Permalink
Add suspendables for request and response filters
Browse files Browse the repository at this point in the history
Add some tests to ensure the filters are called as desired.
  • Loading branch information
victornoel committed Sep 5, 2016
1 parent 87e471a commit 241b2d4
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
javax.ws.rs.container.ContainerRequestFilter.filter
javax.ws.rs.container.ContainerResponseFilter.filter
org.glassfish.jersey.process.internal.Stage.apply
3 changes: 3 additions & 0 deletions comsat-jersey-server/src/main/resources/META-INF/suspendables
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ org.glassfish.jersey.server.model.ResourceMethodInvoker.apply
org.glassfish.jersey.server.ServerRuntime$Responder.process
org.glassfish.jersey.server.ServerRuntime$1.run
org.glassfish.jersey.server.ServerRuntime$2.run
org.glassfish.jersey.server.ContainerFilteringStage.apply
org.glassfish.jersey.server.ContainerFilteringStage$ResponseFilterStage.apply
org.glassfish.jersey.internal.Errors$1.call
org.glassfish.jersey.internal.Errors.process
org.glassfish.jersey.process.internal.RequestScope.runInScope
org.glassfish.jersey.process.internal.Stages.process
org.glassfish.jersey.server.ServerRuntime.process
org.glassfish.jersey.server.ApplicationHandler.handle
org.glassfish.jersey.servlet.internal.ResponseWriter.failure
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package co.paralleluniverse.fibers.jersey;

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.fibers.Suspendable;

@Provider
public class AddTestFiltersFeature implements Feature {

@Override
public boolean configure(FeatureContext context) {
context.register(TestRequestFilter.class);
context.register(TestResponseFilter.class);
return true;
}

}

class TestRequestFilter implements ContainerRequestFilter {

@Override
@Suspendable
public void filter(ContainerRequestContext requestContext) throws IOException {
try {
Fiber.sleep(5);
requestContext.getHeaders().add(FiberServletContainerTest.REQUEST_FILTER_HEADER,
FiberServletContainerTest.REQUEST_FILTER_HEADER_VALUE);
} catch (InterruptedException | SuspendExecution e) {
throw new Error(e);
}
}
}

class TestResponseFilter implements ContainerResponseFilter {

@Override
@Suspendable
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
try {
Fiber.sleep(5);
responseContext.getHeaders().add(FiberServletContainerTest.RESPONSE_FILTER_HEADER,
FiberServletContainerTest.RESPONSE_FILTER_HEADER_VALUE);
} catch (InterruptedException | SuspendExecution e) {
throw new Error(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
*/
package co.paralleluniverse.fibers.jersey;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -27,7 +33,6 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.After;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -64,6 +69,14 @@ public FiberServletContainerTest(Class<? extends EmbeddedServer> cls) {
}
private static final String PACKAGE_NAME_PREFIX = FiberServletContainerTest.class.getPackage().getName() + ".";

public static final String REQUEST_FILTER_HEADER = "test.filter.request.header";

public static final String REQUEST_FILTER_HEADER_VALUE = "ok";

public static final String RESPONSE_FILTER_HEADER = "test.filter.request.header";

public static final String RESPONSE_FILTER_HEADER_VALUE = "ok";

@Before
public void setUp() throws Exception {
this.server = cls.newInstance();
Expand Down Expand Up @@ -102,6 +115,14 @@ public Void handleEntity(HttpEntity entity) throws IOException {
assertEquals("sleep was 10", EntityUtils.toString(entity));
return null;
}

@Override
public Void handleResponse(HttpResponse response) throws HttpResponseException, IOException {
Header h = response.getFirstHeader(RESPONSE_FILTER_HEADER);
assertNotNull(h);
assertEquals(RESPONSE_FILTER_HEADER_VALUE, h.getValue());
return super.handleResponse(response);
}
};

@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,45 @@
*/
package co.paralleluniverse.fibers.jersey;

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.fibers.Suspendable;
import java.io.IOException;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import co.paralleluniverse.fibers.Fiber;
import co.paralleluniverse.fibers.SuspendExecution;
import co.paralleluniverse.fibers.Suspendable;

// snippet REST resource example
@Singleton
@Path("/service")
public class TestResource {
@GET
@Produces("text/plain")
@Suspendable // <------------- FIBER
public String get(@QueryParam("sleep") int sleep) throws IOException, SuspendExecution, InterruptedException {
public String get(@QueryParam("sleep") int sleep,
@HeaderParam(FiberServletContainerTest.REQUEST_FILTER_HEADER) String h)
throws SuspendExecution, InterruptedException {
Fiber.sleep(sleep); // <--- you may use fiber blocking calls here
return "sleep was "+sleep;
// ensure the request filter was called
if (FiberServletContainerTest.REQUEST_FILTER_HEADER_VALUE.equals(h)) {
return "sleep was " + sleep;
} else {
return "missing header!";
}

}
// snippet_exclude_begin
@POST
@Produces("text/plain")
@Suspendable // <------------- FIBER
public String post(@QueryParam("sleep") int sleep) throws IOException, SuspendExecution, InterruptedException {
return get(sleep);
public String post(@QueryParam("sleep") int sleep,
@HeaderParam(FiberServletContainerTest.REQUEST_FILTER_HEADER) String h)
throws SuspendExecution, InterruptedException {
return get(sleep, h);
}
// snippet_exclude_end
}
Expand Down

0 comments on commit 241b2d4

Please sign in to comment.