Skip to content

Commit

Permalink
Regexp on MP RestClient @path
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Serra <Miguel.Serra@criticaltechworks.com>
  • Loading branch information
mcserra committed Jul 2, 2020
1 parent 0f60874 commit 7174bdd
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,44 @@ static List<String> parseParameters(String template) {
return allMatches;
}

/**
* Parses all required parameters from expr string.
* Parameters encapsulated by {} or parameters with regexp expressions like {param: (regex_here)}
*
* @param expr string expression
* @return path params
*/
static List<String> getAllMatchingParams(String expr) {
List<String> allMatches = new ArrayList<>();
if (expr == null || expr.isEmpty() || expr.indexOf('{') == -1) {
return allMatches;
}

boolean matching = false;
int parenthesisMatched = 0;
StringBuilder matchingParameter = new StringBuilder();
for (int i = 0; i < expr.length(); i++) {
char x = expr.charAt(i);

if (!matching && x == '{' && parenthesisMatched == 0) {
matching = true;
} else if (matching && x != ':' && x != '}') {
matchingParameter.append(x);
} else if (matching) {
allMatches.add(matchingParameter.toString());
matchingParameter.setLength(0);
matching = false;
}

if (x == '}') {
parenthesisMatched--;
} else if (x == '{') {
parenthesisMatched++;
}
}
return allMatches;
}

/**
* Validates and returns proper compute method defined in {@link ClientHeaderParam}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ MethodModel build() {

private void validateParameters() {
UriBuilder uriBuilder = UriBuilder.fromUri(interfaceModel.getPath()).path(pathValue);
List<String> parameters = InterfaceUtil.parseParameters(uriBuilder.toTemplate());
List<String> parameters = InterfaceUtil.getAllMatchingParams(uriBuilder.toTemplate());
List<String> methodPathParameters = new ArrayList<>();
List<ParamModel> pathHandlingParams = parameterModels.stream()
.filter(parameterModel -> parameterModel.handles(PathParam.class))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.glassfish.jersey.microprofile.restclient;

import java.util.Arrays;

import org.testng.annotations.Test;

import static org.testng.Assert.assertEquals;

public class InterfaceUtilTest {

@Test
public void testGetAllParams() {
assertEquals(InterfaceUtil.getAllMatchingParams("{abc}/{xyzId: [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}}"), Arrays.asList("abc", "xyzId"));
assertEquals(InterfaceUtil.getAllMatchingParams("{xyzId: [a-zA-Z]+}/{abc}"), Arrays.asList("xyzId", "abc"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
Expand Down Expand Up @@ -66,4 +67,13 @@ default String sayHi() {
@Path("methodContent")
String methodContentType(@HeaderParam(HttpHeaders.CONTENT_TYPE) MediaType contentType, String entity);

@GET
@Path("{content: [a-zA-Z]+}")
@Produces(MediaType.TEXT_PLAIN)
String regex(@PathParam("content") String content);

@GET
@Path("content1/{content1}/content0/{content0: [0-9]{4}}")
@Produces(MediaType.TEXT_PLAIN)
String regex0(@PathParam("content1") String context0, @PathParam("content0") String context1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@ public String methodContentType(MediaType contentType, String entity) {
return null;
}

@Override
public String regex(String content) {
return content;
}

@Override
public String regex0(String context0, String context1) {
return context0 + "_" + context1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import javax.json.Json;
import javax.json.JsonValue;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.core.HttpHeaders;
Expand Down Expand Up @@ -75,6 +76,33 @@ public void testMethodContentType() throws URISyntaxException {
app.methodContentType(MediaType.TEXT_XML_TYPE, "something");
}

@Test
public void testMethodWithRegexPathParam() throws URISyntaxException {
ApplicationResource app = RestClientBuilder.newBuilder()
.baseUri(new URI("http://localhost:9998"))
.build(ApplicationResource.class);

assertEquals(app.regex("bar"), "bar");
}

@Test
public void testMethodWithRegexPathParam0() throws URISyntaxException {
ApplicationResource app = RestClientBuilder.newBuilder()
.baseUri(new URI("http://localhost:9998"))
.build(ApplicationResource.class);

assertEquals(app.regex0("foo", "1234"), "foo_1234");
}

@Test(expected = WebApplicationException.class)
public void testMethodWithRegexPathParam0Failure() throws URISyntaxException {
ApplicationResource app = RestClientBuilder.newBuilder()
.baseUri(new URI("http://localhost:9998"))
.build(ApplicationResource.class);

app.regex0("foo", "12345");
}

private static class TestClientRequestFilter implements ClientRequestFilter {

private final String expectedAccept;
Expand Down

0 comments on commit 7174bdd

Please sign in to comment.