This is an automated email from the ASF dual-hosted git repository.
ffang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new 795aa4d0c56 CXF-7457: Remove the query in
ContainerRequestContext.setRequestUri when the new URI has none (#3665)
795aa4d0c56 is described below
commit 795aa4d0c56a0d0ec94f886676cc0ed699e5943d
Author: Freeman(Yue) Fang <[email protected]>
AuthorDate: Mon Nov 2 16:22:16 2026 -0500
CXF-7457: Remove the query in ContainerRequestContext.setRequestUri when
the new URI has none (#3665)
doSetRequestUri() only replaced Message.QUERY_STRING when the new request
URI
had a query. So a pre-matching filter that removed every query parameter, by
setting a request URI without a query, left the original query in place, and
the resource still saw the original query parameters.
Remove Message.QUERY_STRING when the new URI has no query. An empty query
(as in "/path?") is treated the same as no query.
---
.../jaxrs/impl/ContainerRequestContextImpl.java | 5 +-
.../impl/ContainerRequestContextImplTest.java | 74 ++++++++++++++++++++++
2 files changed, 78 insertions(+), 1 deletion(-)
diff --git
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
index 13fdd4a80c5..c14cc10c085 100644
---
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
+++
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImpl.java
@@ -138,8 +138,11 @@ public class ContainerRequestContextImpl extends
AbstractRequestContextImpl
// changing to toString() seems to have adverse effects downstream.
Needs more investigation.
HttpUtils.resetRequestURI(m, requestUri.getRawPath());
String query = requestUri.getRawQuery();
- if (query != null) {
+ if (!StringUtils.isEmpty(query)) {
m.put(Message.QUERY_STRING, query);
+ } else {
+ // the new URI has no or an empty query, so the query of the old
one must not stay
+ m.remove(Message.QUERY_STRING);
}
}
diff --git
a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImplTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImplTest.java
new file mode 100644
index 00000000000..a621f3d9c17
--- /dev/null
+++
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ContainerRequestContextImplTest.java
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.cxf.jaxrs.impl;
+
+import java.net.URI;
+
+import jakarta.ws.rs.core.MultivaluedMap;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.ExchangeImpl;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+public class ContainerRequestContextImplTest {
+
+ @Test
+ public void testSetRequestUriReplacesQuery() {
+ Message m = createMessage("/echo", "a=1&b=2");
+ new ContainerRequestContextImpl(m, true,
false).setRequestUri(URI.create("/echo?a=3"));
+
+ MultivaluedMap<String, String> query = new
UriInfoImpl(m).getQueryParameters();
+ assertEquals(1, query.size());
+ assertEquals("3", query.getFirst("a"));
+ }
+
+ @Test
+ public void testSetRequestUriWithoutQueryRemovesQuery() {
+ Message m = createMessage("/echo", "a=1&b=2");
+ new ContainerRequestContextImpl(m, true,
false).setRequestUri(URI.create("/echo"));
+
+ assertNull(m.get(Message.QUERY_STRING));
+ assertTrue(new UriInfoImpl(m).getQueryParameters().isEmpty());
+ }
+
+ @Test
+ public void testSetRequestUriWithEmptyQueryRemovesQuery() {
+ Message m = createMessage("/echo", "a=1&b=2");
+ new ContainerRequestContextImpl(m, true,
false).setRequestUri(URI.create("/echo?"));
+
+ assertNull(m.get(Message.QUERY_STRING));
+ assertTrue(new UriInfoImpl(m).getQueryParameters().isEmpty());
+ }
+
+ private static Message createMessage(String requestUri, String query) {
+ Message m = new MessageImpl();
+ Exchange e = new ExchangeImpl();
+ m.setExchange(e);
+ e.setInMessage(m);
+ m.put(Message.REQUEST_URI, requestUri);
+ m.put(Message.QUERY_STRING, query);
+ return m;
+ }
+}