Skip to content

Proof-of-concept of Spring Boot, Servlet API, and Spring MVC support #65

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

Merged
merged 1 commit into from
Oct 12, 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 @@ -15,6 +15,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -31,7 +32,7 @@ public class AwsProxyRequest {
private String resource;
private ApiGatewayRequestContext requestContext;
private Map<String, String> queryStringParameters;
private Map<String, String> headers;
private Map<String, String> headers = new HashMap<>(); // avoid NPE
private Map<String, String> pathParameters;
private String httpMethod;
private Map<String, String> stageVariables;
Expand Down Expand Up @@ -105,7 +106,11 @@ public Map<String, String> getHeaders() {


public void setHeaders(Map<String, String> headers) {
this.headers = headers;
if (null != headers) {
this.headers = headers;
} else {
this.headers.clear();
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
package com.amazonaws.serverless.proxy.internal.servlet;

import com.amazonaws.serverless.proxy.internal.RequestReader;
import com.amazonaws.serverless.proxy.internal.model.ApiGatewayRequestContext;
import com.amazonaws.serverless.proxy.internal.model.ContainerConfig;
import com.amazonaws.services.lambda.runtime.Context;

Expand All @@ -24,6 +26,7 @@
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
Expand Down Expand Up @@ -68,6 +71,7 @@ public abstract class AwsHttpServletRequest implements HttpServletRequest {
private Context lambdaContext;
private Map<String, Object> attributes;
private ServletContext servletContext;
private AwsHttpSession session;

protected DispatcherType dispatcherType;

Expand Down Expand Up @@ -101,13 +105,17 @@ public String getRequestedSessionId() {

@Override
public HttpSession getSession(boolean b) {
return null;
if (b && null == this.session) {
ApiGatewayRequestContext requestContext = (ApiGatewayRequestContext) getAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY);
this.session = new AwsHttpSession(requestContext.getRequestId());
}
return this.session;
}


@Override
public HttpSession getSession() {
return null;
return this.session;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class AwsHttpServletResponse
private int statusCode;
private String statusMessage;
private String responseBody;
private PrintWriter writer;
private ByteArrayOutputStream bodyOutputStream = new ByteArrayOutputStream();
private CountDownLatch writersCountDownLatch;
private AwsHttpServletRequest request;
Expand Down Expand Up @@ -316,7 +317,10 @@ public void close()

@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(bodyOutputStream);
if (null == writer) {
writer = new PrintWriter(bodyOutputStream);
}
return writer;
}


Expand Down Expand Up @@ -358,7 +362,11 @@ public int getBufferSize() {

@Override
public void flushBuffer() throws IOException {
if (null != writer) {
writer.flush();
}
responseBody = new String(bodyOutputStream.toByteArray());
log.debug("Response buffer flushed with {} bytes, latch={}", responseBody.length(), writersCountDownLatch.getCount());
isCommitted = true;
writersCountDownLatch.countDown();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.amazonaws.serverless.proxy.internal.servlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;

public class AwsHttpSession implements HttpSession {

private static final Logger log = LoggerFactory.getLogger(AwsHttpSession.class);
private String id;

/**
* @param id API gateway request ID.
*/
public AwsHttpSession(String id) {
if (null == id) {
throw new RuntimeException("HTTP session id (from request ID) cannot be null");
}
log.debug("Creating session " + id);
this.id = id;
}

@Override
public long getCreationTime() {
return 0;
}

@Override
public String getId() {
return id;
}

@Override
public long getLastAccessedTime() {
return 0;
}

@Override
public ServletContext getServletContext() {
return null;
}

@Override
public void setMaxInactiveInterval(int interval) {

}

@Override
public int getMaxInactiveInterval() {
return 0;
}

@Override
public HttpSessionContext getSessionContext() {
return null;
}

@Override
public Object getAttribute(String name) {
return null;
}

@Override
public Object getValue(String name) {
return null;
}

@Override
public Enumeration<String> getAttributeNames() {
return null;
}

@Override
public String[] getValueNames() {
return new String[0];
}

@Override
public void setAttribute(String name, Object value) {

}

@Override
public void putValue(String name, Object value) {

}

@Override
public void removeAttribute(String name) {

}

@Override
public void removeValue(String name) {

}

@Override
public void invalidate() {

}

@Override
public boolean isNew() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -176,11 +177,13 @@ protected void setServletContext(final ServletContext context) {
* Applies the filter chain in the request lifecycle
* @param request The Request object. This must be an implementation of HttpServletRequest
* @param response The response object. This must be an implementation of HttpServletResponse
* @param servlet Servlet at the end of the chain (optional).
* @throws IOException
* @throws ServletException
*/
protected void doFilter(ContainerRequestType request, ContainerResponseType response) throws IOException, ServletException {
FilterChainHolder chain = filterChainManager.getFilterChain(request);
protected void doFilter(ContainerRequestType request, ContainerResponseType response, Servlet servlet) throws IOException, ServletException {
FilterChainHolder chain = filterChainManager.getFilterChain(request, servlet);
log.debug("FilterChainHolder.doFilter {}", chain);
chain.doFilter(request, response);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,13 @@ public String getPathTranslated() {
}


/**
* In AWS API Gateway, stage is never given as part of the path.
* @return
*/
@Override
public String getContextPath() {
return request.getRequestContext().getStage();
return "";
}


Expand Down Expand Up @@ -228,7 +232,7 @@ public Principal getUserPrincipal() {

@Override
public String getRequestURI() {
return request.getPath();
return (getContextPath().isEmpty() ? "" : "/" + getContextPath()) + request.getPath();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
Expand Down Expand Up @@ -141,9 +142,16 @@ public int getEffectiveMinorVersion() {
@Override
public String getMimeType(String s) {
try {
return Files.probeContentType(Paths.get(s));

if (s.startsWith("file:")) { // Support paths such as file:/D:/something/hello.txt
return Files.probeContentType(Paths.get(URI.create(s)));
} else if (s.startsWith("/")) { // Support paths such as file:/D:/something/hello.txt
return Files.probeContentType(Paths.get(URI.create("file://" + s)));
} else {
return Files.probeContentType(Paths.get(s));
}
} catch (IOException e) {
log.warn("Could not find content type for filter", e);
log.warn("Could not find content type for file {}", s, e);
return null;
}
}
Expand Down Expand Up @@ -364,6 +372,8 @@ public FilterRegistration.Dynamic addFilter(String name, Filter filter) {
// filter already exists, we do nothing
if (filters.containsKey(name)) {
return null;
} else {
log.debug("Adding filter '{}' from {}", name, filter);
}

FilterHolder newFilter = new FilterHolder(name, filter, this);
Expand All @@ -376,6 +386,7 @@ public FilterRegistration.Dynamic addFilter(String name, Filter filter) {
@Override
public FilterRegistration.Dynamic addFilter(String name, Class<? extends Filter> filterClass) {
try {
log.debug("Adding filter '{}' from {}", name, filterClass.getName());
Filter newFilter = createFilter(filterClass);
return addFilter(name, newFilter);
} catch (ServletException e) {
Expand Down
Loading