Skip to content

Commit

Permalink
Merge pull request eclipse-vertx#433 from EmadAlblueshi/routing-conte…
Browse files Browse the repository at this point in the history
…xt-remove

add RoutingContext#remove
  • Loading branch information
pmlopes committed Aug 30, 2016
2 parents 718cbb7 + 5b91e02 commit 4d2c817
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
10 changes: 10 additions & 0 deletions vertx-web/src/main/java/io/vertx/ext/web/RoutingContext.java
Expand Up @@ -119,6 +119,16 @@ public interface RoutingContext {
*/
<T> T get(String key);

/**
* Remove some data from the context. The data is available in any handlers that receive the context.
*
* @param key the key for the data
* @param <T> the type of the data
* @return the previous data associated with the key
* @throws ClassCastException if the data is not of the expected type
*/
<T> T remove(String key);

/**
* @return all the context data as a map
*/
Expand Down
Expand Up @@ -102,6 +102,11 @@ public <T> T get(String key) {
return decoratedContext.get(key);
}

@Override
public <T> T remove(String key) {
return decoratedContext.remove(key);
}

@Override
public String getAcceptableContentType() {
return decoratedContext.getAcceptableContentType();
Expand Down
Expand Up @@ -145,6 +145,13 @@ public <T> T get(String key) {
return (T)obj;
}

@Override
@SuppressWarnings("unchecked")
public <T> T remove(String key) {
Object obj = getData().remove(key);
return (T)obj;
}

@Override
public Map<String, Object> data() {
return getData();
Expand Down
Expand Up @@ -83,6 +83,11 @@ public <T> T get(String key) {
return inner.get(key);
}

@Override
public <T> T remove(String key) {
return inner.remove(key);
}

@Override
public Map<String, Object> data() {
return inner.data();
Expand Down
Expand Up @@ -83,4 +83,20 @@ public void test_object_as_json_yields_json_object() throws Exception {
}, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null);
}

@Test
public void test_remove_data() throws Exception {
router.route().handler(event -> {
String foo = event.getBodyAsJson().encode();
event.put("foo", foo);
String removedFoo = event.remove("foo");
assertEquals(removedFoo, foo);
assertNull(event.get("foo"));
event.response().end();
});
testRequest(HttpMethod.POST, "/", req -> {
req.setChunked(true);
req.write(Buffer.buffer("{ \"foo\": \"bar\" }"));
}, HttpResponseStatus.OK.code(), HttpResponseStatus.OK.reasonPhrase(), null);
}

}

0 comments on commit 4d2c817

Please sign in to comment.