From ba14d86bcb3b895822c747b4fd06668bd9e4aa56 Mon Sep 17 00:00:00 2001 From: Miguel Serra Date: Thu, 2 Jul 2020 16:31:33 +0100 Subject: [PATCH 1/2] Regexp on MP RestClient @Path Signed-off-by: Miguel Serra --- .../restclient/InterfaceUtil.java | 40 ++++++++++++++++++- .../microprofile/restclient/MethodModel.java | 6 +-- .../restclient/InterfaceUtilTest.java | 35 ++++++++++++++++ .../restclient/ApplicationResource.java | 12 +++++- .../restclient/ApplicationResourceImpl.java | 11 ++++- .../restclient/ConsumesAndProducesTest.java | 28 +++++++++++++ 6 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtilTest.java diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java index 83aa97a34c..17c9eb14a9 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java @@ -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 @@ -59,6 +59,44 @@ static List 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 getAllMatchingParams(String expr) { + List 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}. * diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java index a3c52738cc..af59419800 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java @@ -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 @@ -655,7 +655,7 @@ MethodModel build() { private void validateParameters() { UriBuilder uriBuilder = UriBuilder.fromUri(interfaceModel.getPath()).path(pathValue); - List parameters = InterfaceUtil.parseParameters(uriBuilder.toTemplate()); + List parameters = InterfaceUtil.getAllMatchingParams(uriBuilder.toTemplate()); List methodPathParameters = new ArrayList<>(); List pathHandlingParams = parameterModels.stream() .filter(parameterModel -> parameterModel.handles(PathParam.class)) diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtilTest.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtilTest.java new file mode 100644 index 0000000000..fb40a459bd --- /dev/null +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtilTest.java @@ -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")); + } +} diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java index 5b8ef7c1a5..cafa94cc82 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java @@ -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 @@ -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; @@ -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); } diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java index f14e027d1c..e52d1aa20c 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java @@ -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 @@ -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; + } } diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java index 2e518bc640..fffffc29b1 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java @@ -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; @@ -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; From 2c0099fd4fd490401d753da1f15b675b1680bfe9 Mon Sep 17 00:00:00 2001 From: Maxim Nesen Date: Wed, 8 Jul 2020 09:42:18 +0200 Subject: [PATCH 2/2] Copyright header Signed-off-by: Maxim Nesen --- .../microprofile/restclient/InterfaceUtil.java | 2 +- .../microprofile/restclient/MethodModel.java | 4 ++-- .../jersey/restclient/ApplicationResource.java | 2 +- .../restclient/ApplicationResourceImpl.java | 2 +- .../restclient/ConsumesAndProducesTest.java | 16 ++++++++++++++++ 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java index 17c9eb14a9..0501712a32 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/InterfaceUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 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 diff --git a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java index af59419800..388be43cd7 100644 --- a/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java +++ b/ext/microprofile/mp-rest-client/src/main/java/org/glassfish/jersey/microprofile/restclient/MethodModel.java @@ -1,6 +1,6 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. - * Copyright (c) 2020 Payara Foundation and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019 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 diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java index cafa94cc82..ede06ddfa9 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResource.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 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 diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java index e52d1aa20c..bb58e0577d 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ApplicationResourceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2019, 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 diff --git a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java index fffffc29b1..24a1c89cf1 100644 --- a/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java +++ b/tests/integration/microprofile/rest-client/src/test/java/org/glassfish/jersey/restclient/ConsumesAndProducesTest.java @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2019, 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.restclient; import java.net.URI;