Skip to content

Conversation

@sapessi
Copy link
Contributor

@sapessi sapessi commented Mar 28, 2017

Merging support for servlet filters into master branch. This should address issue #15. For containers that support the servlet specs you can use a StartupHandler to register filters in the servlet context:

handler.setStartupHandler(c -> {
    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
});

The container does NOT load filter implementations annotated with @WebFilter automatically, they still have to be registered in the onStartup method. This is to avoid impacting cold starts.

sapessi added 3 commits March 23, 2017 16:46
Still open
1. Support filter annotations
2. Add support for filters in spark
Bug fixes and additional unit tests. Annotation support completes the
feature required to address #15. The service does not automatically
load annotated filter classes, these will have to be added manually
with the new startup event.
More unit tests for #15 and added a unit test to verify #16.
Updated readme to include servlet filter section
@sapessi sapessi requested a review from jt0 March 28, 2017 18:37
@sapessi sapessi self-assigned this Mar 28, 2017
Copy link
Contributor

@jt0 jt0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial review.

protected Cookie[] parseCookies(String headerValue) {
List<Cookie> output = new ArrayList<>();

for (AbstractMap.SimpleEntry<String, String> entry : this.parseHeaderValue(headerValue)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace AbstractMap.SimpleEntry w/ Map.Entry.

Also, consider replacing w/ Java8 streams:

List<Map.Entry<String, String>> parsedHeaders = this.parseHeaderValue(headerValue);

return parsedHeaders.stream()
                    .filter(e -> e.getKey() != null)
                    .map(e -> new Cookie(e.getKey(), e.getValue()))
                    .collect(Collectors.toList())
                    .toArray(new Cookie[entrySet.size()]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat trick!

* @param headerValue The string value of the HTTP Cookie header
* @return An array of Cookie objects from the header
*/
protected Cookie[] parseCookies(String headerValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to cookieHeaderValue or similar.



protected String readPathInfo(String path, String resource) {
// TODO: Implement
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed?



protected String readPathTranslated(String path) {
// TODO: Implement
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed?

attributes = new HashMap<>();

// TODO: We are setting this to request by default
dispatcherType = DispatcherType.REQUEST;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anyway to change this value? If not, remove class variable and hard code response in getDispatcherType() method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to static return. Cannot see where we'll use the other dispatcher types any time soon: http://docs.oracle.com/javaee/6/api/javax/servlet/DispatcherType.html

*/
package com.amazonaws.serverless.proxy.internal.servlet;

import javax.servlet.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use '*' imports.

DispatcherType type = request.getDispatcherType();

if (filtersSize == -1) {
getFilterHolders().size();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, bug. forgot to remove.

if (!key.getClass().isAssignableFrom(TargetCacheKey.class)) {
return false;
} else {
return hashCode() == key.hashCode();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't valid (e.g. o1.targetPath could be null and o2.dispatcherType could be null, both resulting in a hashcode of -1).


@Override
public void init(FilterConfig filterConfig) throws ServletException {
if (filterConfig.getInitParameter("invalid_status_code") != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make "invalid_status_code" a constant.

try {
invalidStatusCode = Integer.parseInt(statusCode);
} catch (NumberFormatException e) {
invalidStatusCode = DEFAULT_ERROR_CODE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to find a better log strategy. Tired of just calling the lambdaContext.getLogger from places I can't reach it. I'll add this as an issue for the next release.

@sapessi sapessi merged commit 3bcca84 into master Apr 13, 2017
@sapessi sapessi deleted the servlet-improvements branch April 13, 2017 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants