Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Servlet improvements #45

Merged
merged 2 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ public Principal getUserPrincipal() {
if (getAuthenticationScheme().equals(AUTH_SCHEME_CUSTOM)) {
return event.getRequestContext().getAuthorizer().getPrincipalId();
} else if (getAuthenticationScheme().equals(AUTH_SCHEME_AWS_IAM)) {
return event.getRequestContext().getIdentity().getUserArn();
// if we received credentials from Cognito Federated Identities then we return the identity id
if (event.getRequestContext().getIdentity().getCognitoIdentityId() != null) {
return event.getRequestContext().getIdentity().getCognitoIdentityId();
} else { // otherwise the user arn from the credentials
return event.getRequestContext().getIdentity().getUserArn();
}
} else if (getAuthenticationScheme().equals(AUTH_SCHEME_COGNITO_POOL)) {
return event.getRequestContext().getAuthorizer().getClaims().getSubject();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.serverless.proxy.test.jersey;
package com.amazonaws.serverless.proxy.jersey;

import com.amazonaws.serverless.proxy.internal.RequestReader;
import com.amazonaws.serverless.proxy.internal.model.ApiGatewayRequestContext;
import com.amazonaws.serverless.proxy.test.jersey.model.MapResponseModel;
import com.amazonaws.serverless.proxy.test.jersey.model.SingleValueModel;
import com.amazonaws.serverless.proxy.jersey.model.MapResponseModel;
import com.amazonaws.serverless.proxy.jersey.model.SingleValueModel;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.serverless.proxy.test.jersey;
package com.amazonaws.serverless.proxy.jersey;


import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.internal.servlet.AwsServletContext;
import com.amazonaws.serverless.proxy.jersey.factory.AwsProxyServletContextFactory;
import com.amazonaws.serverless.proxy.jersey.factory.AwsProxyServletRequestFactory;
import com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler;
import com.amazonaws.serverless.proxy.test.jersey.model.MapResponseModel;
import com.amazonaws.serverless.proxy.test.jersey.model.SingleValueModel;
import com.amazonaws.serverless.proxy.jersey.model.MapResponseModel;
import com.amazonaws.serverless.proxy.jersey.model.SingleValueModel;
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
import com.amazonaws.services.lambda.runtime.Context;
Expand Down Expand Up @@ -51,7 +50,7 @@ public class JerseyAwsProxyTest {


private static ObjectMapper objectMapper = new ObjectMapper();
private static ResourceConfig app = new ResourceConfig().packages("com.amazonaws.serverless.proxy.test.jersey")
private static ResourceConfig app = new ResourceConfig().packages("com.amazonaws.serverless.proxy.jersey")
.register(new AbstractBinder() {
@Override
protected void configure() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.serverless.proxy.test.jersey.model;
package com.amazonaws.serverless.proxy.jersey.model;

import java.util.HashMap;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package com.amazonaws.serverless.proxy.test.jersey.model;
package com.amazonaws.serverless.proxy.jersey.model;

/**
* Request/response model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public class SparkLambdaContainerHandler<RequestType, ResponseType> extends AwsL


//-------------------------------------------------------------
// Variables - Private - Static
// Variables - Private
//-------------------------------------------------------------

private static LambdaEmbeddedServer embeddedServer;
private LambdaEmbeddedServer embeddedServer;


//-------------------------------------------------------------
Expand Down Expand Up @@ -145,13 +145,15 @@ protected AwsHttpServletResponse getContainerResponse(CountDownLatch latch) {
@Override
protected void handleRequest(AwsProxyHttpServletRequest httpServletRequest, AwsHttpServletResponse httpServletResponse, Context lambdaContext)
throws Exception {
// this method of the AwsLambdaServletContainerHandler sets the request context
super.handleRequest(httpServletRequest, httpServletResponse, lambdaContext);

if (embeddedServer == null) {
embeddedServer = LambdaEmbeddedServerFactory.getServerInstance();
servletContext = httpServletRequest.getServletContext();

// call the onStartup event if set to give developers a chance to set filters in the context
if (startupHandler != null) {
startupHandler.onStartup(this.servletContext);
startupHandler.onStartup(getServletContext());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public int ignite(String host,

public void configureWebSockets(Map<String, WebSocketHandlerWrapper> webSocketHandlers,
Optional<Integer> webSocketIdleTimeoutMillis) {
throw new UnsupportedOperationException();
// Swallowing this exception to prevent Spark from getting stuck
// throw new UnsupportedOperationException();
System.out.println("Spark called configureWebSockets. However, web sockets are not supported");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.amazonaws.serverless.proxy.spark;


import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;

import org.junit.Test;

import static org.junit.Assert.*;
import static spark.Spark.get;


public class HelloWorldSparkTest {
private static final String CUSTOM_HEADER_KEY = "X-Custom-Header";
private static final String CUSTOM_HEADER_VALUE = "My Header Value";
private static final String BODY_TEXT_RESPONSE = "Hello World";

private static SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

@Test
public void basicServer_initialize() {
try {
handler = SparkLambdaContainerHandler.getAwsProxyHandler();

configureRoutes();

} catch (RuntimeException | ContainerInitializationException e) {
e.printStackTrace();
fail();
}
}

@Test
public void basicServer_handleRequest_emptyFilters() {
AwsProxyRequest req = new AwsProxyRequestBuilder().method("GET").path("/hello").build();
AwsProxyResponse response = handler.proxy(req, new MockLambdaContext());

assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().containsKey(CUSTOM_HEADER_KEY));
assertEquals(CUSTOM_HEADER_VALUE, response.getHeaders().get(CUSTOM_HEADER_KEY));
assertEquals(BODY_TEXT_RESPONSE, response.getBody());
}

private void configureRoutes() {
get("/hello", (req, res) -> {
res.status(200);
res.header(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE);
return BODY_TEXT_RESPONSE;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.amazonaws.serverless.proxy.spark;


import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
import com.amazonaws.serverless.proxy.spark.filter.CustomHeaderFilter;

import org.junit.Test;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;

import java.util.EnumSet;

import static org.junit.Assert.*;
import static spark.Spark.get;


public class SparkLambdaContainerHandlerTest {
private static final String RESPONSE_BODY_TEXT = "hello";

@Test
public void filters_onStartupMethod_executeFilters() {

SparkLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = null;
try {
handler = SparkLambdaContainerHandler.getAwsProxyHandler();
} catch (ContainerInitializationException e) {
e.printStackTrace();
fail();
}

handler.onStartup(c -> {
if (c == null) {
System.out.println("Null servlet context");
fail();
}
FilterRegistration.Dynamic registration = c.addFilter("CustomHeaderFilter", CustomHeaderFilter.class);
// update the registration to map to a path
registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
// servlet name mappings are disabled and will throw an exception
});

configureRoutes();

AwsProxyRequest req = new AwsProxyRequestBuilder().method("GET").path("/header-filter").build();
AwsProxyResponse response = handler.proxy(req, new MockLambdaContext());

assertNotNull(response);
assertEquals(200, response.getStatusCode());
assertTrue(response.getHeaders().containsKey(CustomHeaderFilter.HEADER_NAME));
assertEquals(CustomHeaderFilter.HEADER_VALUE, response.getHeaders().get(CustomHeaderFilter.HEADER_NAME));
assertEquals(RESPONSE_BODY_TEXT, response.getBody());

}

private void configureRoutes() {
get("/header-filter", (req, res) -> {
res.status(200);
return RESPONSE_BODY_TEXT;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.amazonaws.serverless.proxy.spark.embeddedserver;


import org.junit.Test;

import java.util.Optional;

import static org.junit.Assert.*;


public class LambdaEmbeddedServerTest {
private static LambdaEmbeddedServer server = new LambdaEmbeddedServer(null, null, false);

@Test
public void webSocket_configureWebSocket_noException() {
try {
server.configureWebSockets(null, Optional.of(0));
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.amazonaws.serverless.proxy.spark.filter;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;


public class CustomHeaderFilter implements Filter {
public static final String HEADER_NAME = "X-Filter-Header";
public static final String HEADER_VALUE = "CustomHeaderFilter";

@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Called init on filter");
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("Called doFilter");
HttpServletResponse resp = (HttpServletResponse)servletResponse;
resp.addHeader(HEADER_NAME, HEADER_VALUE);

filterChain.doFilter(servletRequest, servletResponse);
}


@Override
public void destroy() {
System.out.println("Called destroy");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
import com.amazonaws.serverless.proxy.spring.echoapp.model.ValidatedUserModel;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.ws.rs.core.MediaType;

import java.util.EnumSet;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.amazonaws.serverless.proxy.spring.echoapp;

import javax.servlet.*;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;


Expand All @@ -28,4 +35,4 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
public void destroy() {
System.out.println("Called destroy");
}
}
}