Skip to content

Commit b01276e

Browse files
mcserrasenivam
authored andcommitted
Regexp on MP RestClient @path
Signed-off-by: Miguel Serra <[email protected]>
1 parent dfe0415 commit b01276e

File tree

6 files changed

+126
-6
lines changed

6 files changed

+126
-6
lines changed

ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -59,6 +59,44 @@ static List<String> parseParameters(String template) {
5959
return allMatches;
6060
}
6161

62+
/**
63+
* Parses all required parameters from expr string.
64+
* Parameters encapsulated by {} or parameters with regexp expressions like {param: (regex_here)}
65+
*
66+
* @param expr string expression
67+
* @return path params
68+
*/
69+
static List<String> getAllMatchingParams(String expr) {
70+
List<String> allMatches = new ArrayList<>();
71+
if (expr == null || expr.isEmpty() || expr.indexOf('{') == -1) {
72+
return allMatches;
73+
}
74+
75+
boolean matching = false;
76+
int parenthesisMatched = 0;
77+
StringBuilder matchingParameter = new StringBuilder();
78+
for (int i = 0; i < expr.length(); i++) {
79+
char x = expr.charAt(i);
80+
81+
if (!matching && x == '{' && parenthesisMatched == 0) {
82+
matching = true;
83+
} else if (matching && x != ':' && x != '}') {
84+
matchingParameter.append(x);
85+
} else if (matching) {
86+
allMatches.add(matchingParameter.toString());
87+
matchingParameter.setLength(0);
88+
matching = false;
89+
}
90+
91+
if (x == '}') {
92+
parenthesisMatched--;
93+
} else if (x == '{') {
94+
parenthesisMatched++;
95+
}
96+
}
97+
return allMatches;
98+
}
99+
62100
/**
63101
* Validates and returns proper compute method defined in {@link ClientHeaderParam}.
64102
*

ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
3-
* Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2020 Payara Foundation and/or its affiliates. All rights reserved.
44
*
55
* This program and the accompanying materials are made available under the
66
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -655,7 +655,7 @@ MethodModel build() {
655655

656656
private void validateParameters() {
657657
UriBuilder uriBuilder = UriBuilder.fromUri(interfaceModel.getPath()).path(pathValue);
658-
List<String> parameters = InterfaceUtil.parseParameters(uriBuilder.toTemplate());
658+
List<String> parameters = InterfaceUtil.getAllMatchingParams(uriBuilder.toTemplate());
659659
List<String> methodPathParameters = new ArrayList<>();
660660
List<ParamModel> pathHandlingParams = parameterModels.stream()
661661
.filter(parameterModel -> parameterModel.handles(PathParam.class))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.microprofile.restclient;
18+
19+
import java.util.Arrays;
20+
21+
import org.testng.annotations.Test;
22+
23+
import static org.testng.Assert.assertEquals;
24+
25+
public class InterfaceUtilTest {
26+
27+
@Test
28+
public void testGetAllParams() {
29+
assertEquals(InterfaceUtil.getAllMatchingParams(
30+
"{abc}/{xyzId: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}"),
31+
Arrays.asList("abc", "xyzId"));
32+
assertEquals(InterfaceUtil.getAllMatchingParams(
33+
"{xyzId: [a-zA-Z]+}/{abc}"), Arrays.asList("xyzId", "abc"));
34+
}
35+
}

tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -26,6 +26,7 @@
2626
import javax.ws.rs.POST;
2727
import javax.ws.rs.PUT;
2828
import javax.ws.rs.Path;
29+
import javax.ws.rs.PathParam;
2930
import javax.ws.rs.Produces;
3031
import javax.ws.rs.core.HttpHeaders;
3132
import javax.ws.rs.core.MediaType;
@@ -66,4 +67,13 @@ default String sayHi() {
6667
@Path("methodContent")
6768
String methodContentType(@HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType contentType, String entity);
6869

70+
@GET
71+
@Path("{content: [a-zA-Z]+}")
72+
@Produces(MediaType.TEXT_PLAIN)
73+
String regex(@PathParam("content") String content);
74+
75+
@GET
76+
@Path("content1/{content1}/content0/{content0: [0-9]{4}}")
77+
@Produces(MediaType.TEXT_PLAIN)
78+
String regex0(@PathParam("content1") String context0, @PathParam("content0") String context1);
6979
}

tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -62,4 +62,13 @@ public String methodContentType(MediaType contentType, String entity) {
6262
return null;
6363
}
6464

65+
@Override
66+
public String regex(String content) {
67+
return content;
68+
}
69+
70+
@Override
71+
public String regex0(String context0, String context1) {
72+
return context0 + "_" + context1;
73+
}
6574
}

tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import javax.json.Json;
88
import javax.json.JsonValue;
9+
import javax.ws.rs.WebApplicationException;
910
import javax.ws.rs.client.ClientRequestContext;
1011
import javax.ws.rs.client.ClientRequestFilter;
1112
import javax.ws.rs.core.HttpHeaders;
@@ -75,6 +76,33 @@ public void testMethodContentType() throws URISyntaxException {
7576
app.methodContentType(MediaType.TEXT_XML_TYPE, "something");
7677
}
7778

79+
@Test
80+
public void testMethodWithRegexPathParam() throws URISyntaxException {
81+
ApplicationResource app = RestClientBuilder.newBuilder()
82+
.baseUri(new URI("http://localhost:9998"))
83+
.build(ApplicationResource.class);
84+
85+
assertEquals(app.regex("bar"), "bar");
86+
}
87+
88+
@Test
89+
public void testMethodWithRegexPathParam0() throws URISyntaxException {
90+
ApplicationResource app = RestClientBuilder.newBuilder()
91+
.baseUri(new URI("http://localhost:9998"))
92+
.build(ApplicationResource.class);
93+
94+
assertEquals(app.regex0("foo", "1234"), "foo_1234");
95+
}
96+
97+
@Test(expected = WebApplicationException.class)
98+
public void testMethodWithRegexPathParam0Failure() throws URISyntaxException {
99+
ApplicationResource app = RestClientBuilder.newBuilder()
100+
.baseUri(new URI("http://localhost:9998"))
101+
.build(ApplicationResource.class);
102+
103+
app.regex0("foo", "12345");
104+
}
105+
78106
private static class TestClientRequestFilter implements ClientRequestFilter {
79107

80108
private final String expectedAccept;

0 commit comments

Comments
 (0)