Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit e8c77c5

Browse files
committed
Request.queryString
* fix jooby-project#518 * fix jooby-project#514
1 parent 4b4189e commit e8c77c5

File tree

10 files changed

+119
-0
lines changed

10 files changed

+119
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.jooby.issues;
2+
3+
import java.util.Map;
4+
import java.util.stream.Collectors;
5+
6+
import org.jooby.Mutant;
7+
import org.jooby.test.ServerFeature;
8+
import org.junit.Test;
9+
10+
public class Issue514 extends ServerFeature {
11+
12+
{
13+
get("/514", req -> {
14+
Map<String, Mutant> params = req.params().toMap();
15+
return req.path() + "?" + params.entrySet().stream()
16+
.map(e -> e.getKey() + "=" + e.getValue().value()).collect(Collectors.joining("&"));
17+
});
18+
}
19+
20+
@Test
21+
public void shouldReCreateQueryString() throws Exception {
22+
request()
23+
.get("/514?foo=1&bar=2&baz=3")
24+
.expect(v -> {
25+
System.out.println(v);
26+
});
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.jooby.issues;
2+
3+
import org.jooby.test.ServerFeature;
4+
import org.junit.Test;
5+
6+
public class Issue518 extends ServerFeature {
7+
8+
{
9+
get("/518", req -> {
10+
return req.path() + req.queryString().map(it -> "?" + it).orElse("");
11+
});
12+
}
13+
14+
@Test
15+
public void shouldGetQueryString() throws Exception {
16+
request()
17+
.get("/518?foo=1&bar=2&baz=3")
18+
.expect("/518?foo=1&bar=2&baz=3");
19+
20+
request()
21+
.get("/518")
22+
.expect("/518");
23+
24+
request()
25+
.get("/518?")
26+
.expect("/518");
27+
}
28+
29+
}

jooby-netty/src/main/java/org/jooby/internal/netty/NettyRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ public NettyRequest(final ChannelHandlerContext ctx,
109109
channel.attr(ASYNC).set(false);
110110
}
111111

112+
@Override
113+
public Optional<String> queryString() {
114+
String uri = req.uri();
115+
int at = uri.indexOf('?') + 1;
116+
return at > 0 && at < uri.length() ? Optional.of(uri.substring(at)) : Optional.empty();
117+
}
118+
112119
@Override
113120
public String method() {
114121
return req.method().name();

jooby-servlet/src/main/java/org/jooby/servlet/ServletServletRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.jooby.spi.NativeRequest;
4040
import org.jooby.spi.NativeUpload;
4141

42+
import com.google.common.base.Strings;
4243
import com.google.common.collect.ImmutableList;
4344
import com.google.common.collect.ImmutableList.Builder;
4445

@@ -81,6 +82,11 @@ public String method() {
8182
return req.getMethod();
8283
}
8384

85+
@Override
86+
public Optional<String> queryString() {
87+
return Optional.ofNullable(Strings.emptyToNull(req.getQueryString()));
88+
}
89+
8490
@Override
8591
public String path() {
8692
return path;

jooby-undertow/src/main/java/org/jooby/internal/undertow/UndertowRequest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ public UndertowRequest(final HttpServerExchange exchange, final Config conf)
8181
this.path = URLDecoder.decode(exchange.getRequestPath(), "UTF-8");
8282
}
8383

84+
@Override
85+
public Optional<String> queryString() {
86+
String q = exchange.getQueryString();
87+
return q.length() == 0 ? Optional.empty() : Optional.of(q);
88+
}
89+
8490
@Override
8591
public String method() {
8692
return exchange.getRequestMethod().toString();

jooby/src/main/java/org/jooby/Request.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ public String path() {
126126
return req.path();
127127
}
128128

129+
@Override
130+
public Optional<String> queryString() {
131+
return req.queryString();
132+
}
133+
129134
@Override
130135
public String path(final boolean escape) {
131136
return req.path(escape);
@@ -479,6 +484,13 @@ default String path() {
479484
return path(false);
480485
}
481486

487+
/**
488+
* The query string, without the leading <code>?</code>.
489+
*
490+
* @return The query string, without the leading <code>?</code>.
491+
*/
492+
Optional<String> queryString();
493+
482494
/**
483495
* Escape the path using {@link UrlEscapers#urlFragmentEscaper()}.
484496
*

jooby/src/main/java/org/jooby/internal/RequestImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ public String contextPath() {
127127
return contextPath;
128128
}
129129

130+
@Override
131+
public Optional<String> queryString() {
132+
return req.queryString();
133+
}
134+
130135
@SuppressWarnings("unchecked")
131136
@Override
132137
public <T> Optional<T> ifGet(final String name) {

jooby/src/main/java/org/jooby/spi/NativeRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ public interface NativeRequest {
4747
*/
4848
String path();
4949

50+
/**
51+
* Returns the query string that is contained in the request URL after the path.
52+
*
53+
* @return Query string or <code>empty</code>
54+
*/
55+
Optional<String> queryString();
56+
5057
/**
5158
* @return List with all the parameter names from query string plus any other form/multipart param
5259
* names (excluding file uploads). This method should NOT returns null, absence of params

jooby/src/test/java/org/jooby/RequestForwardingTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ public void verb() throws Exception {
111111
});
112112
}
113113

114+
@Test
115+
public void queryString() throws Exception {
116+
new MockUnit(Request.class)
117+
.expect(unit -> {
118+
Request req = unit.get(Request.class);
119+
expect(req.queryString()).andReturn(Optional.empty());
120+
})
121+
.run(unit -> {
122+
assertEquals(Optional.empty(),
123+
new Request.Forwarding(unit.get(Request.class)).queryString());
124+
});
125+
}
126+
114127
@Test
115128
public void type() throws Exception {
116129
new MockUnit(Request.class)

jooby/src/test/java/org/jooby/RequestTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public MediaType type() {
2828
throw new UnsupportedOperationException();
2929
}
3030

31+
@Override
32+
public Optional<String> queryString() {
33+
throw new UnsupportedOperationException();
34+
}
35+
3136
@Override
3237
public boolean matches(final String pattern) {
3338
throw new UnsupportedOperationException();

0 commit comments

Comments
 (0)