Skip to content

Commit

Permalink
Merge pull request #676 from andrenpaes/servlet-request-attributes
Browse files Browse the repository at this point in the history
Exposes Servlet Request attributes
  • Loading branch information
jknack authored Mar 20, 2017
2 parents 7c9f59b + 402753e commit 7071898
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.Executor;
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
Expand Down Expand Up @@ -119,6 +116,16 @@ public List<String> params(final String name) throws Exception {
return Arrays.asList(values);
}

@Override
public Map<String, Object> attributes() {
final Enumeration<String> attributeNames = req.getAttributeNames();
if (!attributeNames.hasMoreElements()) {
return Collections.emptyMap();
}
return Collections.list(attributeNames).stream()
.collect(Collectors.toMap(Function.identity(), name -> req.getAttribute(name)));
}

@Override
public List<String> headers(final String name) {
return toList(req.getHeaders(name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.google.common.collect.ImmutableMap;
import org.jooby.MediaType;
import org.jooby.test.MockUnit;
import org.junit.Test;
Expand Down Expand Up @@ -148,6 +152,45 @@ public void noparams() throws IOException, Exception {

}

@Test
public void attributes() throws Exception {
String tmpdir = System.getProperty("java.io.tmpdir");
final UUID serverAttribute = UUID.randomUUID();
new MockUnit(HttpServletRequest.class)
.expect(unit -> {
HttpServletRequest req = unit.get(HttpServletRequest.class);
expect(req.getContentType()).andReturn("text/html");
expect(req.getPathInfo()).andReturn("/");
expect(req.getAttributeNames()).andReturn(
Collections.enumeration(Collections.singletonList("server.attribute")));
expect(req.getAttribute("server.attribute")).andReturn(serverAttribute);
})
.run(unit -> {
assertEquals(ImmutableMap.of("server.attribute", serverAttribute),
new ServletServletRequest(unit.get(HttpServletRequest.class), tmpdir)
.attributes());
});

}

@Test
public void emptyAttributes() throws Exception {
String tmpdir = System.getProperty("java.io.tmpdir");
new MockUnit(HttpServletRequest.class)
.expect(unit -> {
HttpServletRequest req = unit.get(HttpServletRequest.class);
expect(req.getContentType()).andReturn("text/html");
expect(req.getPathInfo()).andReturn("/");
expect(req.getAttributeNames()).andReturn(Collections.emptyEnumeration());
})
.run(unit -> {
assertEquals(Collections.emptyMap(),
new ServletServletRequest(unit.get(HttpServletRequest.class), tmpdir)
.attributes());
});

}

@Test(expected = IOException.class)
public void filesFailure() throws IOException, Exception {
String tmpdir = System.getProperty("java.io.tmpdir");
Expand Down
2 changes: 2 additions & 0 deletions jooby/src/main/java/org/jooby/internal/HttpHandlerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ public void handle(final NativeRequest request, final NativeResponse response) t
requestPath = rpath.apply(requestPath);
}

// put request attributes first to make sure we don't override defaults
locals.putAll(request.attributes());
// default locals
locals.put(CONTEXT_PATH, contextPath);
locals.put(PATH, requestPath);
Expand Down
8 changes: 8 additions & 0 deletions jooby/src/main/java/org/jooby/spi/NativeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -71,6 +72,13 @@ public interface NativeRequest {
*/
List<String> params(String name) throws Exception;

/**
* @return Map containing all request attributes
*/
default Map<String, Object> attributes() {
return Collections.emptyMap();
}

/**
* Get all the headers for the provided name or a empty list.
*
Expand Down

0 comments on commit 7071898

Please sign in to comment.