This is an automated email from the ASF dual-hosted git repository. reta pushed a commit to branch 3.3.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit f35bf79d7c44286281e2c1fb857052255da55419 Author: Andriy Redko <[email protected]> AuthorDate: Wed Sep 9 20:02:12 2020 -0400 CXF-8227: Failure to comply with JAX-RS spec with ContainerRequestContext and WriterInterceptorContext (#692) * CXF-8227: Failure to comply with JAX-RS spec with ContainerRequestContext and WriterInterceptorContext. Addressing review comments (cherry picked from commit 6d5fddfa467cb7d9a54c066029b2a6d055fb5546) --- .../cxf/jaxrs/impl/PropertyHolderFactory.java | 59 ++++++++++++++++++ .../jaxrs/impl/ServletRequestPropertyHolder.java | 70 ---------------------- .../apache/cxf/systest/jaxrs/BookApplication.java | 12 +++- .../jaxrs/JAXRSClientServerNonSpringBookTest.java | 19 +++++- 4 files changed, 88 insertions(+), 72 deletions(-) diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PropertyHolderFactory.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PropertyHolderFactory.java index 23be014..052eb48 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PropertyHolderFactory.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PropertyHolderFactory.java @@ -20,8 +20,13 @@ package org.apache.cxf.jaxrs.impl; import java.util.Collection; import java.util.Collections; +import java.util.Enumeration; import java.util.HashMap; +import java.util.LinkedHashSet; import java.util.Map; +import java.util.Set; + +import javax.servlet.http.HttpServletRequest; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.message.Message; @@ -41,6 +46,60 @@ public final class PropertyHolderFactory { void setProperty(String name, Object value); Collection<String> getPropertyNames(); } + + private static class ServletRequestPropertyHolder extends MessagePropertyHolder { + private static final String ENDPOINT_ADDRESS_PROPERTY = "org.apache.cxf.transport.endpoint.address"; + private final HttpServletRequest request; + + ServletRequestPropertyHolder(Message m) { + super(m); + request = (HttpServletRequest)m.get("HTTP.REQUEST"); + } + + @Override + public Object getProperty(String name) { + final Object value = request.getAttribute(name); + + if (value != null) { + return value; + } + + return super.getProperty(name); + } + + @Override + public void removeProperty(String name) { + super.removeProperty(name); + request.removeAttribute(name); + } + + @Override + public void setProperty(String name, Object value) { + if (value == null) { + removeProperty(name); + } else { + super.setProperty(name, value); + request.setAttribute(name, value); + } + } + + @Override + public Collection<String> getPropertyNames() { + final Set<String> list = new LinkedHashSet<>(); + + Enumeration<String> attrNames = request.getAttributeNames(); + while (attrNames.hasMoreElements()) { + String name = attrNames.nextElement(); + if (!ENDPOINT_ADDRESS_PROPERTY.equals(name)) { + list.add(name); + } + } + + list.addAll(super.getPropertyNames()); + return list; + } + + } private static class MessagePropertyHolder implements PropertyHolder { private static final String PROPERTY_KEY = "jaxrs.filter.properties"; diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ServletRequestPropertyHolder.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ServletRequestPropertyHolder.java deleted file mode 100644 index 88b2ba2..0000000 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ServletRequestPropertyHolder.java +++ /dev/null @@ -1,70 +0,0 @@ -/** - * 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.util.Collection; -import java.util.Enumeration; -import java.util.LinkedList; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; - -import org.apache.cxf.jaxrs.impl.PropertyHolderFactory.PropertyHolder; -import org.apache.cxf.message.Message; - -public class ServletRequestPropertyHolder implements PropertyHolder { - private static final String ENDPOINT_ADDRESS_PROPERTY = "org.apache.cxf.transport.endpoint.address"; - private HttpServletRequest request; - public ServletRequestPropertyHolder(Message m) { - request = (HttpServletRequest)m.get("HTTP.REQUEST"); - } - - @Override - public Object getProperty(String name) { - return request.getAttribute(name); - } - - @Override - public void removeProperty(String name) { - request.removeAttribute(name); - } - - @Override - public void setProperty(String name, Object value) { - if (value == null) { - removeProperty(name); - } else { - request.setAttribute(name, value); - } - } - - @Override - public Collection<String> getPropertyNames() { - List<String> list = new LinkedList<>(); - Enumeration<String> attrNames = request.getAttributeNames(); - while (attrNames.hasMoreElements()) { - String name = attrNames.nextElement(); - if (!ENDPOINT_ADDRESS_PROPERTY.equals(name)) { - list.add(name); - } - } - return list; - } - -} diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java index 605d5a6..0465916 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookApplication.java @@ -106,6 +106,12 @@ public class BookApplication extends Application { public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException { context.getHeaders().putSingle("BookWriter", "TheBook"); + + final Object property = context.getProperty("property"); + if (property != null) { + context.getHeaders().putSingle("X-Property-WriterInterceptor", property); + } + context.proceed(); } @@ -131,7 +137,11 @@ public class BookApplication extends Application { || uri.contains("/application6")) { context.getHeaders().put("BOOK", Arrays.asList("1", "2")); } - + + final String value = context.getUriInfo().getQueryParameters().getFirst("property"); + if (value != null) { + context.setProperty("property", value); + } } } diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java index c3ff4dc..6192000 100644 --- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java +++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerNonSpringBookTest.java @@ -20,8 +20,10 @@ package org.apache.cxf.systest.jaxrs; import java.io.InputStream; +import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; import java.util.List; +import java.util.Map; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.WebApplicationException; @@ -186,6 +188,15 @@ public class JAXRSClientServerNonSpringBookTest extends AbstractBusClientServerT doTestPerRequest("http://localhost:" + PORT + "/application11/thebooks/bookstore2/bookheaders"); assertEquals("TheBook", r.getHeaderString("BookWriter")); } + + @Test + public void testGetBook123PropagatingContextPropertyToWriterInterceptor() throws Exception { + Response r = + doTestPerRequest("http://localhost:" + PORT + "/application6/thebooks/bookstore2/bookheaders", + new SimpleEntry<>("property", "PropValue")); + assertEquals("PropValue", r.getHeaderString("X-Property-WriterInterceptor")); + } + @Test public void testGetBook123TwoApplications() throws Exception { @@ -193,10 +204,16 @@ public class JAXRSClientServerNonSpringBookTest extends AbstractBusClientServerT doTestPerRequest("http://localhost:" + PORT + "/application6/the%20books2/bookstore2/book%20headers"); } - private Response doTestPerRequest(String address) throws Exception { + @SafeVarargs + private final Response doTestPerRequest(String address, Map.Entry<String, String> ... params) throws Exception { WebClient wc = WebClient.create(address); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L); wc.accept("application/xml"); + + for (Map.Entry<String, String> param: params) { + wc.query(param.getKey(), param.getValue()); + } + Response r = wc.get(); Book book = r.readEntity(Book.class); assertEquals("CXF in Action", book.getName());
