Skip to content

Commit

Permalink
Merge pull request #3354 from jamezp/RESTEASY-2845-5.x
Browse files Browse the repository at this point in the history
[RESTEASY-2845] ResteasyWebTarget.proxy(Class) problem with questionmark in @path with Regex
  • Loading branch information
jamezp committed Dec 12, 2022
2 parents d7ba86e + 7a8f44f commit d675dc8
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.jboss.resteasy.util.MediaTypeHelper;

import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.RxInvoker;
import javax.ws.rs.client.RxInvokerProvider;
Expand All @@ -26,9 +27,12 @@
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.Set;

Expand Down Expand Up @@ -157,13 +161,34 @@ protected Object invokeSync(Object[] args)
protected ClientInvocation createRequest(Object[] args)
{
WebTarget target = this.webTarget;
for (int i = 0; i < processors.length; i++)

final Map<String, Object> pathParamsMap = new HashMap<>();

for (int i = 0; i < method.getParameterTypes().length; i++)
{
if (processors != null && processors[i] instanceof WebTargetProcessor)
Annotation[] paramAnnotations = method.getParameterAnnotations()[i];
for (Annotation annotation : paramAnnotations)
{
WebTargetProcessor processor = (WebTargetProcessor)processors[i];
target = processor.build(target, args[i]);
if (annotation instanceof PathParam)
{
pathParamsMap.put(((PathParam) annotation).value(), args[i]);
break;
}
}
}

if (pathParamsMap.size() > 1) {
target = target.resolveTemplates(pathParamsMap);
}

if (processors != null) {
for (int i = 0; i < processors.length; i++)
{
if (processors[i] instanceof WebTargetProcessor)
{
WebTargetProcessor processor = (WebTargetProcessor) processors[i];
target = processor.build(target, args[i]);
}
}
}
ClientInvocationBuilder builder = (ClientInvocationBuilder) target.request();
Expand All @@ -177,13 +202,14 @@ protected ClientInvocation createRequest(Object[] args)
{
clientInvocation.getHeaders().accept(accepts);
}
for (int i = 0; i < processors.length; i++)
{
if (processors != null && processors[i] instanceof InvocationProcessor)
if (processors != null) {
for (int i = 0; i < processors.length; i++)
{
InvocationProcessor processor = (InvocationProcessor)processors[i];
processor.process(clientInvocation, args[i]);

if (processors[i] instanceof InvocationProcessor)
{
InvocationProcessor processor = (InvocationProcessor)processors[i];
processor.process(clientInvocation, args[i]);
}
}
}
return clientInvocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2022 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.resteasy.test.client.proxy;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;

import org.jboss.resteasy.test.client.proxy.resource.ProxyPathParamRegexResource;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.ws.rs.Encoded;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.MediaType;


/**
* @tpSubChapter Resteasy-client
* @tpChapter Integration tests
* @tpTestCaseDetails Regression test for RESTEASY-2845
* @tpSince RESTEasy 5.0.5
*/
@RunWith(Arquillian.class)
@RunAsClient
public class ProxyPathParamRegexTest {

@Path("")
public interface RegexInterface {
@GET
@Path("/{path}/{string}")
@Produces(MediaType.TEXT_PLAIN)
String getPath(@PathParam("path") String path, @PathParam("string") @Encoded String string);
}

static ResteasyClient client;

@Before
public void setUp(){
client = (ResteasyClient) ClientBuilder.newClient();
}

@After
public void after() throws Exception{
client.close();
}

@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(ProxyPathParamRegexTest.class.getSimpleName());
return TestUtil.finishContainerPrepare(war, null, ProxyPathParamRegexResource.class);
}

private String generateURL() {
return PortProviderUtil.generateBaseUrl(ProxyPathParamRegexTest.class.getSimpleName());
}

/**
* @tpTestDetails Checks whether question mark in regular expression in second path param is correctly evaluated.
* @tpPassCrit Expected string is returned
* @tpSince RESTEasy 5.0.5
*/
@Test
public void testQuestionMarkInMultiplePathParamRegex() {

ResteasyWebTarget target = client.target(generateURL());

ProxyPathParamRegexTest.RegexInterface proxy = target.proxy(ProxyPathParamRegexTest.RegexInterface.class);
String responseString = proxy.getPath("path", "a");

Assert.assertEquals("Wrong string returned by proxy interface", "patha", responseString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2022 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.resteasy.test.client.proxy.resource;

import org.jboss.resteasy.test.client.proxy.ProxyPathParamRegexTest;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Encoded;
import javax.ws.rs.core.MediaType;

@Path("")
public class ProxyPathParamRegexResource implements ProxyPathParamRegexTest.RegexInterface {
@GET
@Path("/{path}/{string:[a-z]?}")
@Produces(MediaType.TEXT_PLAIN)
public String getPath(@PathParam("path") String path, @PathParam("string") @Encoded String string) {
return path + string;
}
}

0 comments on commit d675dc8

Please sign in to comment.