Skip to content

[CXF-9209] UriInfoImpl#getMatchedResourceTemplate support variable Paths#2983

Merged
reta merged 4 commits intoapache:mainfrom
jungm:issue/CXF-9209
Apr 5, 2026
Merged

[CXF-9209] UriInfoImpl#getMatchedResourceTemplate support variable Paths#2983
reta merged 4 commits intoapache:mainfrom
jungm:issue/CXF-9209

Conversation

@jungm
Copy link
Copy Markdown
Member

@jungm jungm commented Mar 22, 2026

See CXF-9209

TCK run is passing in TomEE with these changes

final List<URITemplate> templates = new LinkedList<>();
String matchedResourceTemplate = getBaseUri().getPath();

if (HttpUtils.isHttpRequest(message)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jungm I believe this is not the correct implementation since it captures the part of the URI which is outside of the JAX-RS scope. The test case from below is confirming that:

Message m = mockMessage("http://localhost:8080/app", "/foo/one/abc");
assertEquals("/app/foo/one/{name:[a-zA-Z][a-zA-Z_0-9]*}", u.getMatchedResourceTemplate());

I believe the /app should not be included in the resource template (it is managed externally, could be set by servlet container / application server / ...), it is reasonable to assume the correct template in this case should be /foo/one/{name:[a-zA-Z][a-zA-Z_0-9]*}.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at https://jakarta.ee/specifications/restful-ws/4.0/apidocs/jakarta.ws.rs/jakarta/ws/rs/core/uriinfo#getMatchedResourceTemplate() - I think @reta is right. The Javadoc lists an example on what to expect (it doesn't mention the servlet context path).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, pushed a change but that is still WIP - don't think reading the ApplicationPath annotation on every getMatchedResourceTemplate invocation is neither smart nor right considering jaxrs.application.address.ignore so no need to really review this yet

@jungm jungm marked this pull request as draft March 24, 2026 20:06
@jungm jungm marked this pull request as ready for review April 3, 2026 18:20
@jungm jungm requested a review from reta April 3, 2026 18:20
public String getMatchedResourceTemplate() {
if (stack != null) {
final List<URITemplate> templates = new LinkedList<>();
String matchedResourceTemplate = (String) message.getExchange().getEndpoint()
Copy link
Copy Markdown
Member

@reta reta Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jungm I still have the same concern #2983 (comment), I really don't see what has changed (please correct me if I am wrong). JAX-RS has to be aware of the URL parts that are being configured either on JAX-RS Application or JAX-RS resources - this is what its scope is. The server address (which matchedResourceTemplate represents in this case) does include the "external" that are outside of the JAX-RS scope and should not be part of the matcher logic.

My apologies, I was confused the server address with ResourceUtils address, this part seems to be fine, thank you.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reta it is being set in ResourceUtils to address, which is being constructed by:

        String address = "/";
        if (!ignoreAppPath) {
            ApplicationPath appPath = locateApplicationPath(app.getClass());
            if (appPath != null) {
                address = appPath.value();
            }
        }
        if (!address.startsWith("/")) {
            address = "/" + address;
        }

So ideally, this is just the annotated @ApplicationPath (with normalization and respect for jaxrs.application.address.ignore) plus the paths of the resources being appended in getMatchedResourceTemplate

Though I do admit this might be hacky, maybe theres a better way to do this I didn't find? I don't really know the CXF code, always happy for feedback :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I figured this one out a bit later :D Thank you @jungm , I will look into it as well,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are two problems (which we could tackle separately):

  • the getMatchedResourceTemplate fix (which fails with exception)
  • the application path component (which seems to be not taken into account all the time)

For getMatchedResourceTemplate , I think I have a clean fix, @jungm do you mind if I push a change into your pull request? Thank you

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reta of course, edits for maintainers should be allowed so you can just push to this branch (I think)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jungm pushed a small change to fix the immediate issue, the sub-resources seem to have a gap here, looking into it (thanks a lot for the test cases).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@reta just let this run through the TCK in TomEE again now, these tests look good (ignoring app path prefix):

04-05-2026 09:34:07:[WebValidatorBase] Unable to find the following search string in the server's response: '/app/resource/three/{x:[a-z]}/{x:[a-z]}' at index: 0
[WebValidatorBase] Server's response:
-------------------------------------------
/resource/three/{x:[a-z]}/{x:[a-z]}
-------------------------------------------
04-05-2026 09:34:07:[WebValidatorBase] Unable to find the following search string in the server's response: '/app/resource/one/{name:[a-zA-Z][a-zA-Z_0-9]*}' at index: 0
[WebValidatorBase] Server's response:
-------------------------------------------
/resource/one/{name:[a-zA-Z][a-zA-Z_0-9]*}
-------------------------------------------

However in these there seems to be a whitespace in the @Path annotations that is being lost, probably another issue that is out of scope for now?

04-05-2026 09:34:07:[WebValidatorBase] Unable to find the following search string in the server's response: '/app/resource/two/{Prefix}{p:/?}{id: ((\d+)?)}/abc/{yeah}' at index: 0
[WebValidatorBase] Server's response:
-------------------------------------------
/resource/two/{Prefix}{p:/?}{id:((\d+)?)}/abc/{yeah}
-------------------------------------------
04-05-2026 09:34:07:[WebValidatorBase] Unable to find the following search string in the server's response: '/app/resource/two/{Prefix}{p:/?}{id: ((\d+)?)}/abc{p2:/?}{number: (([A-Za-z0-9]*)?)}' at index: 0
[WebValidatorBase] Server's response:
-------------------------------------------
/resource/two/{Prefix}{p:/?}{id:((\d+)?)}/abc{p2:/?}{number:(([A-Za-z0-9]*)?)}
-------------------------------------------

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for checking @jungm

However in these there seems to be a whitespace in the @path annotations that is being lost, probably another issue that is out of scope for now?

So CXF does trim whitespaces upon pattern extraction (seems on purpose), I will into it shortly as well, thank you

…lementantion

Signed-off-by: Andriy Redko <drreta@gmail.com>
@reta reta force-pushed the issue/CXF-9209 branch from 9305af2 to b8125be Compare April 4, 2026 15:59
@reta reta merged commit eed5de6 into apache:main Apr 5, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants