You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2019/05/03 10:55:21 UTC
[cxf] branch master updated: If there is no inMessage use the
inFaultMessage. Patch applied thanks to Hugo Trippaers
This is an automated email from the ASF dual-hosted git repository.
coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/master by this push:
new 0bd8eae If there is no inMessage use the inFaultMessage. Patch applied thanks to Hugo Trippaers
0bd8eae is described below
commit 0bd8eaef0a1b6ea87aa07f605e7ea8ace82698a2
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Fri May 3 11:54:23 2019 +0100
If there is no inMessage use the inFaultMessage. Patch applied thanks to Hugo Trippaers
---
.../cxf/jaxrs/impl/ReaderInterceptorMBR.java | 3 +
.../apache/cxf/jaxrs/provider/ProviderFactory.java | 45 ++++++-----
.../cxf/jaxrs/impl/ReaderInterceptorMBRTest.java | 32 ++++++++
.../cxf/jaxrs/provider/ProviderFactoryTest.java | 86 ++++++++++++++++++++--
.../systest/jaxrs/JAXRSClientServerBookTest.java | 32 ++++++++
5 files changed, 175 insertions(+), 23 deletions(-)
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ReaderInterceptorMBR.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ReaderInterceptorMBR.java
index d39dffd..86a3803 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ReaderInterceptorMBR.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ReaderInterceptorMBR.java
@@ -43,6 +43,9 @@ public class ReaderInterceptorMBR implements ReaderInterceptor {
public ReaderInterceptorMBR(MessageBodyReader<?> reader,
Message m) {
+ if (null == m) {
+ throw new IllegalArgumentException("Message not allowed to be null");
+ }
this.reader = reader;
this.m = m;
}
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
index 82fe620..cf5b7e8 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
@@ -93,8 +93,8 @@ public abstract class ProviderFactory {
private static final String BUS_PROVIDERS_ALL = "org.apache.cxf.jaxrs.bus.providers";
private static final String PROVIDER_CACHE_ALLOWED = "org.apache.cxf.jaxrs.provider.cache.allowed";
private static final String PROVIDER_CACHE_CHECK_ALL = "org.apache.cxf.jaxrs.provider.cache.checkAllCandidates";
-
-
+
+
static class LazyProviderClass {
// class to Lazily call the ClassLoaderUtil.loadClass, but do it once
// and cache the result. Then use the class to create instances as needed.
@@ -106,11 +106,11 @@ public abstract class ProviderFactory {
final String className;
volatile boolean initialized;
Class<?> cls;
-
+
LazyProviderClass(String cn) {
className = cn;
}
-
+
synchronized void loadClass() {
if (!initialized) {
try {
@@ -121,7 +121,7 @@ public abstract class ProviderFactory {
initialized = true;
}
}
-
+
public Object tryCreateInstance(Bus bus) {
if (!initialized) {
loadClass();
@@ -147,21 +147,21 @@ public abstract class ProviderFactory {
return null;
}
};
-
- private static final LazyProviderClass DATA_SOURCE_PROVIDER_CLASS =
+
+ private static final LazyProviderClass DATA_SOURCE_PROVIDER_CLASS =
new LazyProviderClass("org.apache.cxf.jaxrs.provider.DataSourceProvider");
- private static final LazyProviderClass JAXB_PROVIDER_CLASS =
+ private static final LazyProviderClass JAXB_PROVIDER_CLASS =
new LazyProviderClass(JAXB_PROVIDER_NAME);
- private static final LazyProviderClass JAXB_ELEMENT_PROVIDER_CLASS =
+ private static final LazyProviderClass JAXB_ELEMENT_PROVIDER_CLASS =
new LazyProviderClass("org.apache.cxf.jaxrs.provider.JAXBElementTypedProvider");
- private static final LazyProviderClass MULTIPART_PROVIDER_CLASS =
+ private static final LazyProviderClass MULTIPART_PROVIDER_CLASS =
new LazyProviderClass("org.apache.cxf.jaxrs.provider.MultipartProvider");
-
+
protected Map<NameKey, ProviderInfo<ReaderInterceptor>> readerInterceptors =
new NameKeyMap<>(true);
protected Map<NameKey, ProviderInfo<WriterInterceptor>> writerInterceptors =
new NameKeyMap<>(true);
-
+
private List<ProviderInfo<MessageBodyReader<?>>> messageReaders =
new ArrayList<>();
private List<ProviderInfo<MessageBodyWriter<?>>> messageWriters =
@@ -183,7 +183,7 @@ public abstract class ProviderFactory {
private Comparator<?> providerComparator;
private ProviderCache providerCache;
-
+
protected ProviderFactory(Bus bus) {
this.bus = bus;
@@ -356,7 +356,7 @@ public abstract class ProviderFactory {
boolean injectContext) {
return handleMapper(em, expectedType, m, providerClass, null, injectContext);
}
-
+
protected <T> boolean handleMapper(ProviderInfo<T> em,
Class<?> expectedType,
Message m,
@@ -434,7 +434,7 @@ public abstract class ProviderFactory {
m);
int size = readerInterceptors.size();
if (mr != null || size > 0) {
- ReaderInterceptor mbrReader = new ReaderInterceptorMBR(mr, m.getExchange().getInMessage());
+ ReaderInterceptor mbrReader = new ReaderInterceptorMBR(mr, getResponseMessage(m));
List<ReaderInterceptor> interceptors = null;
if (size > 0) {
@@ -907,7 +907,7 @@ public abstract class ProviderFactory {
if (result != 0) {
return result;
}
-
+
return comparePriorityStatus(p1.getProvider().getClass(), p2.getProvider().getClass());
}
}
@@ -1154,7 +1154,7 @@ public abstract class ProviderFactory {
Class<?> actualType = InjectionUtils.getActualType(genericSuperType);
if (actualType != null && actualType.isAssignableFrom(expectedClass)) {
return new Type[]{genericSuperType};
- } else if (commonBaseCls != null && commonBaseCls != Object.class
+ } else if (commonBaseCls != null && commonBaseCls != Object.class
&& commonBaseCls.isAssignableFrom(expectedClass)
&& commonBaseCls.isAssignableFrom(actualType)
|| expectedClass.isAssignableFrom(actualType)) {
@@ -1265,6 +1265,15 @@ public abstract class ProviderFactory {
return new ProviderInfo<Object>(instance, proxies, theBus, checkContexts, custom);
}
+ private Message getResponseMessage(Message message) {
+ Message responseMessage = message.getExchange().getInMessage();
+ if (responseMessage == null) {
+ responseMessage = message.getExchange().getInFaultMessage();
+ }
+
+ return responseMessage;
+ }
+
protected static class NameKey {
private String name;
private Integer priority;
@@ -1342,7 +1351,7 @@ public abstract class ProviderFactory {
} else {
return getFilterNameBindings(p.getBus(), p.getProvider());
}
-
+
}
protected static Set<String> getFilterNameBindings(Bus bus, Object provider) {
Class<?> pClass = ClassHelper.getRealClass(bus, provider);
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ReaderInterceptorMBRTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ReaderInterceptorMBRTest.java
new file mode 100644
index 0000000..19c771a
--- /dev/null
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/ReaderInterceptorMBRTest.java
@@ -0,0 +1,32 @@
+/**
+ * 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 org.junit.Test;
+
+
+public class ReaderInterceptorMBRTest {
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testReaderInterceptorMBRWithNullMessage() {
+ new ReaderInterceptorMBR(null, null);
+ }
+
+}
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
index 8516ce0..cb0435a 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
@@ -52,6 +52,7 @@ import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;
+import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
import javax.xml.bind.JAXBContext;
@@ -115,7 +116,7 @@ public class ProviderFactoryTest {
pf.createExceptionMapper(WebApplicationException.class, new MessageImpl());
assertSame(provider, em);
}
-
+
@Test
public void testRegisterFeatureInFeature() {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
@@ -131,7 +132,7 @@ public class ProviderFactoryTest {
pf.createExceptionMapper(WebApplicationException.class, new MessageImpl());
assertSame(provider, em);
}
-
+
@Test
public void testRegisterMbrMbwProviderAsMbrOnly() {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
@@ -149,7 +150,7 @@ public class ProviderFactoryTest {
assertTrue(writer instanceof JAXBElementProvider);
assertNotSame(writer, customProvider);
}
-
+
@Test
public void testRegisterMbrMbwProviderAsMbwOnly() {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
@@ -167,7 +168,7 @@ public class ProviderFactoryTest {
assertTrue(reader instanceof JAXBElementProvider);
assertNotSame(reader, customProvider);
}
-
+
@Test
public void testOrderOfProvidersWithSameProperties() {
ProviderFactory pf = ServerProviderFactory.getInstance();
@@ -830,6 +831,24 @@ public class ProviderFactoryTest {
return message;
}
+ private Message prepareFaultMessage(String contentType, String acceptType) {
+ Message message = new MessageImpl();
+ Map<String, List<String>> headers = new MetadataMap<String, String>();
+ message.put(Message.PROTOCOL_HEADERS, headers);
+ Exchange exchange = new ExchangeImpl();
+ exchange.setInMessage(null);
+ exchange.setInFaultMessage(message);
+ if (acceptType != null) {
+ headers.put("Accept", Collections.singletonList(acceptType));
+ exchange.setOutMessage(new MessageImpl());
+ } else {
+ headers.put("Content-Type", Collections.singletonList(contentType));
+ }
+ message.put("Content-Type", contentType);
+ message.setExchange(exchange);
+ return message;
+ }
+
@Test
public void testRegisterCustomEntityProvider() throws Exception {
ProviderFactory pf = ServerProviderFactory.getInstance();
@@ -839,6 +858,63 @@ public class ProviderFactoryTest {
"application/widget");
}
+ @Test
+ public void testCreateMessageBodyReaderInterceptor() {
+ ServerProviderFactory spf = ServerProviderFactory.getInstance();
+ final Message message = prepareMessage(MediaType.APPLICATION_XML, MediaType.APPLICATION_XML);
+
+ List<ReaderInterceptor> interceptors =
+ spf.createMessageBodyReaderInterceptor(Book.class, Book.class,
+ new Annotation[0], MediaType.APPLICATION_XML_TYPE,
+ message, true, null);
+ assertSame(1, interceptors.size());
+ }
+
+ @Test
+ public void testCreateMessageBodyReaderInterceptorWithFaultMessage() throws Exception {
+ ServerProviderFactory spf = ServerProviderFactory.getInstance();
+ final Message message = prepareFaultMessage(MediaType.APPLICATION_XML, MediaType.APPLICATION_XML);
+
+ List<ReaderInterceptor> interceptors =
+ spf.createMessageBodyReaderInterceptor(Book.class, Book.class,
+ new Annotation[0], MediaType.APPLICATION_XML_TYPE,
+ message, true, null);
+ assertSame(1, interceptors.size());
+ }
+
+ @Test
+ public void testCreateMessageBodyReaderInterceptorWithReaderInterceptor() throws Exception {
+ ReaderInterceptor ri = readerInterceptorContext -> readerInterceptorContext.proceed();
+ ProviderInfo<ReaderInterceptor> pi = new ProviderInfo<>(ri, null, true);
+
+ ServerProviderFactory spf = ServerProviderFactory.getInstance();
+ spf.readerInterceptors.put(new ProviderFactory.NameKey("org.apache.cxf.filter.binding", 1, ri.getClass()), pi);
+
+ final Message message = prepareMessage(MediaType.APPLICATION_XML, MediaType.APPLICATION_XML);
+
+ List<ReaderInterceptor> interceptors =
+ spf.createMessageBodyReaderInterceptor(Book.class, Book.class,
+ new Annotation[0], MediaType.APPLICATION_XML_TYPE,
+ message, true, null);
+ assertSame(2, interceptors.size());
+ }
+
+ @Test
+ public void testCreateMessageBodyReaderInterceptorWithFaultMessageAndReaderInterceptor() throws Exception {
+ ReaderInterceptor ri = readerInterceptorContext -> readerInterceptorContext.proceed();
+ ProviderInfo<ReaderInterceptor> pi = new ProviderInfo<>(ri, null, true);
+
+ ServerProviderFactory spf = ServerProviderFactory.getInstance();
+ spf.readerInterceptors.put(new ProviderFactory.NameKey("org.apache.cxf.filter.binding", 1, ri.getClass()), pi);
+
+ final Message message = prepareFaultMessage(MediaType.APPLICATION_XML, MediaType.APPLICATION_XML);
+ List<ReaderInterceptor> interceptors =
+ spf.createMessageBodyReaderInterceptor(Book.class, Book.class,
+ new Annotation[0], MediaType.APPLICATION_XML_TYPE,
+ message, true, null);
+ assertSame(2, interceptors.size());
+ }
+
private int indexOf(List<? extends Object> providerInfos, Class<?> providerType) {
int index = 0;
for (Object pi : providerInfos) {
@@ -1141,7 +1217,7 @@ public class ProviderFactoryTest {
}
}
-
+
@Produces("application/xml")
@Consumes("application/xml")
private static class BookReaderWriter
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
index 0582cbb..a7b1f27 100644
--- a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
@@ -19,6 +19,7 @@
package org.apache.cxf.systest.jaxrs;
+import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
@@ -50,6 +51,7 @@ import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
+import javax.ws.rs.ext.ReaderInterceptor;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
@@ -2747,6 +2749,36 @@ public class JAXRSClientServerBookTest extends AbstractBusClientServerTestBase {
}
+ @Test
+ public void testGetBookWithReaderInterceptor() throws Exception {
+ BookStore client = JAXRSClientFactory
+ .create("http://localhost:" + PORT, BookStore.class, Collections.singletonList(getReaderInterceptor()));
+ Book book = client.getBook(0);
+ assertEquals(123, book.getId());
+ }
+
+ @Test
+ public void testGetBookWithServerWebApplicationExceptionAndReaderInterceptor() throws Exception {
+ BookStore client = JAXRSClientFactory
+ .create("http://localhost:" + PORT, BookStore.class, Collections.singletonList(getReaderInterceptor()));
+ try {
+ client.throwException();
+ fail("Exception expected");
+ } catch (ServerErrorException ex) {
+ assertEquals(500, ex.getResponse().getStatus());
+ assertEquals("This is a WebApplicationException", ex.getResponse().readEntity(String.class));
+ }
+ }
+
+
+ private ReaderInterceptor getReaderInterceptor() {
+ return readerInterceptorContext -> {
+ InputStream is = new BufferedInputStream(readerInterceptorContext.getInputStream());
+ readerInterceptorContext.setInputStream(is);
+ return readerInterceptorContext.proceed();
+ };
+ }
+
private void getAndCompareAsStrings(String address,
String resourcePath,