Skip to content

Commit

Permalink
#32: inject GatewayRequestContext into filters with @context instead of
Browse files Browse the repository at this point in the history
@Inject in order to support CDI (weld)
  • Loading branch information
bbilger committed Mar 8, 2017
1 parent 9f6ed24 commit 5fa552d
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import java.io.IOException;
import java.net.URI;

import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;

import com.jrestless.aws.gateway.io.GatewayRequest;
Expand Down Expand Up @@ -63,10 +63,10 @@ public class DynamicProxyBasePathFilter implements ContainerRequestFilter {
private static final String PROXY_END = "+}";

// request-scoped proxy
private final GatewayRequest gatwayRequest;
private GatewayRequest gatwayRequest;

@Inject
DynamicProxyBasePathFilter(GatewayRequest gatwayRequest) {
@Context
void setGatewayRequest(GatewayRequest gatwayRequest) {
this.gatwayRequest = gatwayRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@

import javax.activation.DataSource;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.StreamingOutput;

Expand Down Expand Up @@ -67,12 +67,16 @@ public class GatewayBinaryResponseFilter implements ContainerResponseFilter {

static final int PRIORITY_OFFSET = 100;

private final boolean binaryCompressionOnly;
private Configuration configuration;

@Inject
GatewayBinaryResponseFilter(Configuration configuration) {
@Context
void setConfiguration(Configuration configuration) {
this.configuration = configuration;
}

private boolean isBinaryCompressionOnly() {
Object binaryCompressionOnlyProp = configuration.getProperty(BINARY_COMPRESSION_ONLY_PROPERTY);
binaryCompressionOnly = binaryCompressionOnlyProp == null
return binaryCompressionOnlyProp == null
|| PropertiesHelper.isProperty(binaryCompressionOnlyProp);
}

Expand All @@ -89,6 +93,7 @@ public final void filter(ContainerRequestContext requestContext, ContainerRespon
* "Content-Encoding" header is available (and thus will be compressed)
* and binaryCompressionOnly is disabled
*/
boolean binaryCompressionOnly = isBinaryCompressionOnly();
if (binaryResponse || compressedResponse && !binaryCompressionOnly) {
responseContext.getHeaders().putSingle(HEADER_BINARY_RESPONSE, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Nonnull;
import javax.ws.rs.container.ContainerRequestContext;
Expand All @@ -29,11 +28,7 @@

abstract class AuthorizerFilter implements ContainerRequestFilter {

private final GatewayRequest gatewayRequest;

AuthorizerFilter(@Nonnull GatewayRequest gatewayRequest) {
this.gatewayRequest = Objects.requireNonNull(gatewayRequest);
}
protected abstract GatewayRequest getGatewayRequest();

@Override
public final void filter(ContainerRequestContext requestContext) throws IOException {
Expand All @@ -47,7 +42,7 @@ public final void filter(ContainerRequestContext requestContext) throws IOExcept
}

private Map<String, Object> getAuthorizerData() {
GatewayRequestContext gatewayRequestContext = gatewayRequest.getRequestContext();
GatewayRequestContext gatewayRequestContext = getGatewayRequest().getRequestContext();
if (gatewayRequestContext == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import javax.annotation.Nonnull;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.ws.rs.Priorities;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;

import org.slf4j.Logger;
Expand Down Expand Up @@ -52,9 +52,16 @@ public class CognitoUserPoolAuthorizerFilter extends AuthorizerFilter {

private static final Logger LOG = LoggerFactory.getLogger(CognitoUserPoolAuthorizerFilter.class);

@Inject
public CognitoUserPoolAuthorizerFilter(GatewayRequest gatewayRequest) {
super(gatewayRequest);
private GatewayRequest gatewayRequest;

@Context
void setGatewayRequest(GatewayRequest gatewayRequest) {
this.gatewayRequest = gatewayRequest;
}

@Override
protected GatewayRequest getGatewayRequest() {
return gatewayRequest;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import javax.annotation.Nonnull;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.ws.rs.Priorities;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;

import org.slf4j.Logger;
Expand Down Expand Up @@ -49,9 +49,16 @@ public class CustomAuthorizerFilter extends AuthorizerFilter {

private static final Logger LOG = LoggerFactory.getLogger(CognitoUserPoolAuthorizerFilter.class);

@Inject
public CustomAuthorizerFilter(GatewayRequest gatewayRequest) {
super(gatewayRequest);
private GatewayRequest gatewayRequest;

@Context
void setGatewayRequest(GatewayRequest gatewayRequest) {
this.gatewayRequest = gatewayRequest;
}

@Override
protected GatewayRequest getGatewayRequest() {
return gatewayRequest;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ private ContainerRequestContext createContainerRequest(String baseUri, String re
}

private DynamicProxyBasePathFilter createFilter(String resource) {
return new DynamicProxyBasePathFilter(createGatewayRequest(resource));
DynamicProxyBasePathFilter filter = new DynamicProxyBasePathFilter();
filter.setGatewayRequest(createGatewayRequest(resource));
return filter;

}

private static GatewayRequest createGatewayRequest(String resource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ private static GatewayBinaryResponseFilter createFilter(Boolean binaryCompressio
Configuration config = mock(Configuration.class);
when(config.getProperty(GatewayBinaryResponseFilter.BINARY_COMPRESSION_ONLY_PROPERTY))
.thenReturn(binaryCompressionOnly);
return new GatewayBinaryResponseFilter(config);
GatewayBinaryResponseFilter filter = new GatewayBinaryResponseFilter();
filter.setConfiguration(config);
return filter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class CognitoUserPoolAuthorizerFilterTest extends AuthorizerFilterTest {

@Override
AuthorizerFilter createCognitoAuthorizerFilter(GatewayRequest gatewayRequest) {
return new CognitoUserPoolAuthorizerFilter(gatewayRequest);
CognitoUserPoolAuthorizerFilter filter = new CognitoUserPoolAuthorizerFilter();
filter.setGatewayRequest(gatewayRequest);
return filter;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class CustomAuthorizerFilterTest extends AuthorizerFilterTest {

@Override
AuthorizerFilter createCognitoAuthorizerFilter(GatewayRequest gatewayRequest) {
return new CustomAuthorizerFilter(gatewayRequest);
CustomAuthorizerFilter filter = new CustomAuthorizerFilter();
filter.setGatewayRequest(gatewayRequest);
return filter;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
import java.io.IOException;
import java.net.URI;

import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

Expand All @@ -39,15 +39,16 @@
@PreMatching
public final class ApplicationPathFilter implements ContainerRequestFilter {

private final String applicationPath;
private Application applicationConfig;

@Inject
ApplicationPathFilter(Application applicationConfig) {
this.applicationPath = getApplicationPath(applicationConfig);
@Context
void setApplication(Application applicationConfig) {
this.applicationConfig = applicationConfig;
}

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
String applicationPath = getApplicationPath();
if (applicationPath != null) {
UriInfo requestUriInfo = requestContext.getUriInfo();
UriBuilder baseUriBuilder = requestUriInfo.getBaseUriBuilder();
Expand All @@ -60,6 +61,10 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
}
}

private String getApplicationPath() {
return getApplicationPath(applicationConfig);
}

private static String getApplicationPath(Application applicationConfig) {
if (applicationConfig == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;

import org.junit.Test;

import com.jrestless.core.filter.ApplicationPathFilter;

public class ApplicationPathFilterTest {

private static final URI EXISTING_BASE_URI = URI.create("existing-base-uri/");
Expand All @@ -28,74 +27,74 @@ public class ApplicationPathFilterTest {

@Test
public void filter_NoAppPathGiven_ShouldAccessRequestContext() throws IOException {
new ApplicationPathFilter(new ApplicationWithoutPath()).filter(null);
createApplicationPathFilter(new ApplicationWithoutPath()).filter(null);
}

@Test(expected = NullPointerException.class)
public void filter_AppPathGiven_ShouldAccessRequestContext() throws IOException {
new ApplicationPathFilter(new ApplicationPathTest()).filter(null);
createApplicationPathFilter(new ApplicationPathTest()).filter(null);
}

@Test
public void filter_EmptyAppPathGiven_ShouldNotUpdateBaseUri() throws IOException {
ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI);
new ApplicationPathFilter(new ApplicationPathEmpty()).filter(reqContext);
createApplicationPathFilter(new ApplicationPathEmpty()).filter(reqContext);
verify(reqContext, never()).setRequestUri(any(), any());
verify(reqContext, never()).setRequestUri(any());
}

@Test
public void filter_NoAppPathGiven_ShouldNotUpdateBaseUri() throws IOException {
ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI);
new ApplicationPathFilter(new ApplicationWithoutPath()).filter(reqContext);
createApplicationPathFilter(new ApplicationWithoutPath()).filter(reqContext);
verify(reqContext, never()).setRequestUri(any(), any());
verify(reqContext, never()).setRequestUri(any());
}

@Test
public void filter_NoAppPathRootGiven_ShouldNotUpdateBaseUri() throws IOException {
ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI);
new ApplicationPathFilter(new ApplicationPathRoot()).filter(reqContext);
createApplicationPathFilter(new ApplicationPathRoot()).filter(reqContext);
verify(reqContext, never()).setRequestUri(any(), any());
verify(reqContext, never()).setRequestUri(any());
}

@Test
public void filter_NullAppConfigGiven_ShouldNotUpdateBaseUri() throws IOException {
ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI);
new ApplicationPathFilter(null).filter(reqContext);
createApplicationPathFilter(null).filter(reqContext);
verify(reqContext, never()).setRequestUri(any(), any());
verify(reqContext, never()).setRequestUri(any());
}

@Test
public void filter_AppPathGiven_ShouldUpdateBaseUri() throws IOException {
ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI);
new ApplicationPathFilter(new ApplicationPathTest()).filter(reqContext);
createApplicationPathFilter(new ApplicationPathTest()).filter(reqContext);
verify(reqContext, times(1)).setRequestUri(TEST_UPDATED_BASE_URI, REQUEST_URI);
verify(reqContext, never()).setRequestUri(any());
}

@Test
public void filter_AppPathWithSlashGiven_ShouldUpdateBaseUriWithoutSlashes() throws IOException {
ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI);
new ApplicationPathFilter(new ApplicationPathTestWithLeadingAndTrailingSlashes()).filter(reqContext);
createApplicationPathFilter(new ApplicationPathTestWithLeadingAndTrailingSlashes()).filter(reqContext);
verify(reqContext, times(1)).setRequestUri(TEST_UPDATED_BASE_URI, REQUEST_URI);
verify(reqContext, never()).setRequestUri(any());
}

@Test
public void filter_AppPathWithSlashAsteriskGiven_ShouldUpdateBaseUriWithoutSlashesAsterisk() throws IOException {
ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI);
new ApplicationPathFilter(new ApplicationPathTestSlashAsterisk()).filter(reqContext);
createApplicationPathFilter(new ApplicationPathTestSlashAsterisk()).filter(reqContext);
verify(reqContext, times(1)).setRequestUri(TEST_UPDATED_BASE_URI, REQUEST_URI);
verify(reqContext, never()).setRequestUri(any());
}

@Test
public void filter_AppPathWithMultiplePathsGiven_ShouldUpdateBaseUriWithAllPaths() throws IOException {
ContainerRequestContext reqContext = mockRequestContext(EXISTING_BASE_URI, REQUEST_URI);
new ApplicationPathFilter(new ApplicationPath1AndPath2()).filter(reqContext);
createApplicationPathFilter(new ApplicationPath1AndPath2()).filter(reqContext);
verify(reqContext, times(1)).setRequestUri(URI.create("existing-base-uri/path1/path2/"), REQUEST_URI);
verify(reqContext, never()).setRequestUri(any());
}
Expand All @@ -111,6 +110,12 @@ private static ContainerRequestContext mockRequestContext(URI baseUri, URI reque
return reqContext;
}

private static ContainerRequestFilter createApplicationPathFilter(Application app) {
ApplicationPathFilter filter = new ApplicationPathFilter();
filter.setApplication(app);
return filter;
}

private static class ApplicationWithoutPath extends Application {
}

Expand Down

0 comments on commit 5fa552d

Please sign in to comment.