Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regexp on MP RestClient @Path #4526

Merged
merged 2 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down 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
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down 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,35 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down 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