Repository: cxf Updated Branches: refs/heads/master 0f2a9fa0a -> 9ef7c5eba
[CXF-5765] Fix several issues with RPC/Lit with schema validation turned on. Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/9ef7c5eb Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/9ef7c5eb Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/9ef7c5eb Branch: refs/heads/master Commit: 9ef7c5ebad8e5b26de79705056d20cb8bc92a107 Parents: 0f2a9fa Author: Daniel Kulp <[email protected]> Authored: Fri Jun 13 10:37:01 2014 -0400 Committer: Daniel Kulp <[email protected]> Committed: Fri Jun 13 10:37:50 2014 -0400 ---------------------------------------------------------------------- .../AbstractInDatabindingInterceptor.java | 2 +- .../AbstractOutDatabindingInterceptor.java | 52 ++++++++++++++------ .../cxf/staxutils/CachingXmlEventWriter.java | 2 +- .../soap/interceptor/RPCInInterceptor.java | 3 ++ .../soap/interceptor/RPCOutInterceptor.java | 34 ++++++++++++- .../JavaFirstSchemaValidationTest.java | 35 +++++++++++-- .../schemavalidation/PersonServiceRPC.java | 45 +++++++++++++++++ .../schemavalidation/PersonServiceRPCImpl.java | 47 ++++++++++++++++++ .../jaxws/schemavalidation/package-info.java | 21 ++++++++ 9 files changed, 218 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/core/src/main/java/org/apache/cxf/interceptor/AbstractInDatabindingInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/interceptor/AbstractInDatabindingInterceptor.java b/core/src/main/java/org/apache/cxf/interceptor/AbstractInDatabindingInterceptor.java index db083a1..472a426 100644 --- a/core/src/main/java/org/apache/cxf/interceptor/AbstractInDatabindingInterceptor.java +++ b/core/src/main/java/org/apache/cxf/interceptor/AbstractInDatabindingInterceptor.java @@ -134,7 +134,7 @@ public abstract class AbstractInDatabindingInterceptor extends AbstractPhaseInte * @param reader * @see #setDataReaderValidation(Service, Message, DataReader) */ - private void setOperationSchemaValidation(OperationInfo opInfo, Message message) { + protected void setOperationSchemaValidation(OperationInfo opInfo, Message message) { if (opInfo != null) { SchemaValidationType validationType = (SchemaValidationType) opInfo.getProperty(Message.SCHEMA_VALIDATION_ENABLED); http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/core/src/main/java/org/apache/cxf/interceptor/AbstractOutDatabindingInterceptor.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/interceptor/AbstractOutDatabindingInterceptor.java b/core/src/main/java/org/apache/cxf/interceptor/AbstractOutDatabindingInterceptor.java index c713f44..4ba54e5 100644 --- a/core/src/main/java/org/apache/cxf/interceptor/AbstractOutDatabindingInterceptor.java +++ b/core/src/main/java/org/apache/cxf/interceptor/AbstractOutDatabindingInterceptor.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.xml.namespace.NamespaceContext; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import javax.xml.stream.events.XMLEvent; @@ -62,6 +63,17 @@ public abstract class AbstractOutDatabindingInterceptor extends AbstractPhaseInt protected boolean isRequestor(Message message) { return Boolean.TRUE.equals(message.containsKey(Message.REQUESTOR_ROLE)); } + protected boolean shouldBuffer(Message message) { + Object en = message.getContextualProperty(OUT_BUFFERING); + boolean allowBuffer = true; + boolean buffer = false; + if (en != null) { + buffer = Boolean.TRUE.equals(en) || "true".equals(en); + allowBuffer = !(Boolean.FALSE.equals(en) || "false".equals(en)); + } + // need to cache the events in case validation fails or buffering is enabled + return buffer || (allowBuffer && shouldValidate(message) && !isRequestor(message)); + } protected void writeParts(Message message, Exchange exchange, BindingOperationInfo operation, MessageContentsList objs, @@ -75,22 +87,17 @@ public abstract class AbstractOutDatabindingInterceptor extends AbstractPhaseInt // configure endpoint and operation level schema validation setOperationSchemaValidation(operation.getOperationInfo(), message); - Object en = message.getContextualProperty(OUT_BUFFERING); - boolean allowBuffer = true; - boolean buffer = false; - if (en != null) { - buffer = Boolean.TRUE.equals(en) || "true".equals(en); - allowBuffer = !(Boolean.FALSE.equals(en) || "false".equals(en)); - } // need to cache the events in case validation fails or buffering is enabled - if (buffer || (allowBuffer && shouldValidate(message) && !isRequestor(message))) { - cache = new CachingXmlEventWriter(); - try { - cache.setNamespaceContext(origXmlWriter.getNamespaceContext()); - } catch (XMLStreamException e) { - //ignorable, will just get extra namespace decls + if (shouldBuffer(message)) { + if (!(xmlWriter instanceof CachingXmlEventWriter)) { + cache = new CachingXmlEventWriter(); + try { + cache.setNamespaceContext(origXmlWriter.getNamespaceContext()); + } catch (XMLStreamException e) { + //ignorable, will just get extra namespace decls + } + xmlWriter = cache; } - xmlWriter = cache; out = null; } @@ -119,8 +126,25 @@ public abstract class AbstractOutDatabindingInterceptor extends AbstractPhaseInt for (MessagePartInfo part : parts) { if (objs.hasValue(part)) { + NamespaceContext c = null; + if (!part.isElement() + && xmlWriter instanceof CachingXmlEventWriter) { + try { + c = xmlWriter.getNamespaceContext(); + xmlWriter.setNamespaceContext(new CachingXmlEventWriter.NSContext(null)); + } catch (XMLStreamException e) { + //ignore + } + } Object o = objs.get(part); dataWriter.write(o, part, xmlWriter); + if (c != null) { + try { + xmlWriter.setNamespaceContext(c); + } catch (XMLStreamException e) { + //ignore + } + } } } } http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/core/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java b/core/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java index 47c48cb..266b6a8 100644 --- a/core/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java +++ b/core/src/main/java/org/apache/cxf/staxutils/CachingXmlEventWriter.java @@ -233,7 +233,7 @@ public class CachingXmlEventWriter implements XMLStreamWriter { Collections.EMPTY_SET.iterator())); } - private static class NSContext implements NamespaceContext { + public static class NSContext implements NamespaceContext { NamespaceContext parent; Map<String, String> map = new HashMap<String, String>(); http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCInInterceptor.java ---------------------------------------------------------------------- diff --git a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCInInterceptor.java b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCInInterceptor.java index 181e983..cd16a9c 100644 --- a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCInInterceptor.java +++ b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCInInterceptor.java @@ -221,5 +221,8 @@ public class RPCInInterceptor extends AbstractInDatabindingInterceptor { endpointInfo.setProperty("URI", wsdlDescription); } message.put(Message.WSDL_DESCRIPTION, wsdlDescription); + + // configure endpoint and operation level schema validation + setOperationSchemaValidation(operation.getOperationInfo(), message); } } http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCOutInterceptor.java ---------------------------------------------------------------------- diff --git a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCOutInterceptor.java b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCOutInterceptor.java index 9fa064b..a6fcdcb 100644 --- a/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCOutInterceptor.java +++ b/rt/bindings/soap/src/main/java/org/apache/cxf/binding/soap/interceptor/RPCOutInterceptor.java @@ -23,6 +23,7 @@ import java.util.logging.Logger; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; +import javax.xml.stream.events.XMLEvent; import org.apache.cxf.binding.soap.wsdl.extensions.SoapBody; import org.apache.cxf.common.logging.LogUtils; @@ -35,6 +36,7 @@ import org.apache.cxf.message.MessageContentsList; import org.apache.cxf.phase.Phase; import org.apache.cxf.service.model.BindingOperationInfo; import org.apache.cxf.service.model.MessagePartInfo; +import org.apache.cxf.staxutils.CachingXmlEventWriter; import org.apache.cxf.staxutils.StaxUtils; public class RPCOutInterceptor extends AbstractOutDatabindingInterceptor { @@ -46,6 +48,7 @@ public class RPCOutInterceptor extends AbstractOutDatabindingInterceptor { public void handleMessage(Message message) { + XMLStreamWriter origXmlWriter = null; try { NSStack nsStack = new NSStack(); nsStack.push(); @@ -56,7 +59,19 @@ public class RPCOutInterceptor extends AbstractOutDatabindingInterceptor { assert operation.getName() != null; XMLStreamWriter xmlWriter = getXMLStreamWriter(message); - + CachingXmlEventWriter cache = null; + // need to cache the events in case validation fails or buffering is enabled + if (shouldBuffer(message)) { + origXmlWriter = xmlWriter; + cache = new CachingXmlEventWriter(); + try { + cache.setNamespaceContext(xmlWriter.getNamespaceContext()); + } catch (XMLStreamException e) { + //ignorable, will just get extra namespace decls + } + message.setContent(XMLStreamWriter.class, cache); + xmlWriter = cache; + } List<MessagePartInfo> parts = null; @@ -97,9 +112,24 @@ public class RPCOutInterceptor extends AbstractOutDatabindingInterceptor { writeParts(message, message.getExchange(), operation, objs, parts); // Finishing the writing. - xmlWriter.writeEndElement(); + xmlWriter.writeEndElement(); + + + if (cache != null) { + try { + for (XMLEvent event : cache.getEvents()) { + StaxUtils.writeEvent(event, origXmlWriter); + } + } catch (XMLStreamException e) { + throw new Fault(e); + } + } } catch (XMLStreamException e) { throw new Fault(e); + } finally { + if (origXmlWriter != null) { + message.setContent(XMLStreamWriter.class, origXmlWriter); + } } } http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/JavaFirstSchemaValidationTest.java ---------------------------------------------------------------------- diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/JavaFirstSchemaValidationTest.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/JavaFirstSchemaValidationTest.java index 8eb5c8c..888e5a8 100644 --- a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/JavaFirstSchemaValidationTest.java +++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/JavaFirstSchemaValidationTest.java @@ -21,6 +21,7 @@ package org.apache.cxf.systest.jaxws.schemavalidation; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -31,6 +32,7 @@ import org.apache.cxf.annotations.SchemaValidation.SchemaValidationType; import org.apache.cxf.endpoint.Client; import org.apache.cxf.endpoint.Server; import org.apache.cxf.feature.Feature; +import org.apache.cxf.feature.LoggingFeature; import org.apache.cxf.feature.validation.DefaultSchemaValidationTypeProvider; import org.apache.cxf.feature.validation.SchemaValidationFeature; import org.apache.cxf.frontend.ClientProxy; @@ -39,7 +41,6 @@ import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import org.apache.cxf.message.Message; import org.apache.cxf.service.model.BindingOperationInfo; import org.apache.cxf.testutil.common.TestUtil; - import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; @@ -55,6 +56,7 @@ public class JavaFirstSchemaValidationTest extends Assert { private static List<Server> serverList = new ArrayList<Server>(); private static PersonServiceAnnotated annotatedClient; private static PersonService client; + private static PersonServiceRPC rpcClient; @BeforeClass public static void startServers() throws Exception { @@ -69,10 +71,13 @@ public class JavaFirstSchemaValidationTest extends Assert { createServer(PersonService.class, new PersonServiceImpl(), feature); - createServer(PersonServiceAnnotated.class, new PersonServiceAnnotatedImpl(), null); + createServer(PersonServiceAnnotated.class, new PersonServiceAnnotatedImpl()); + + createServer(PersonServiceRPC.class, new PersonServiceRPCImpl(), feature, new LoggingFeature()); annotatedClient = createClient(PersonServiceAnnotated.class); client = createClient(PersonService.class); + rpcClient = createClient(PersonServiceRPC.class); } @AfterClass @@ -85,7 +90,27 @@ public class JavaFirstSchemaValidationTest extends Assert { static String getAddress(Class<?> sei) { return "http://localhost:" + PORT + "/" + sei.getSimpleName(); } + + + @Test + public void testRPCLit() throws Exception { + Person person = new Person(); + person.setFirstName("Foo"); + person.setLastName("Bar"); + //this should work + rpcClient.saveValidateOut(person); + + try { + person.setFirstName(null); + rpcClient.saveValidateOut(person); + } catch (SOAPFaultException sfe) { + // verify its server side and a schema validation + assertTrue(sfe.getMessage().contains("Marshalling Error")); + assertTrue(sfe.getMessage().contains("lastName")); + } + } + // so this is the default, we are inheriting from the service level SchemaValidation annotation // which is set to BOTH @Test @@ -261,12 +286,12 @@ public class JavaFirstSchemaValidationTest extends Assert { return newClient; } - public static Server createServer(Class<?> serviceInterface, Object serviceImpl, Feature feature) + public static Server createServer(Class<?> serviceInterface, Object serviceImpl, Feature ... features) throws IOException { JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean(); svrFactory.setServiceClass(serviceImpl.getClass()); - if (feature != null) { - svrFactory.getFeatures().add(feature); + if (features != null) { + svrFactory.getFeatures().addAll(Arrays.asList(features)); } svrFactory.setAddress(getAddress(serviceInterface)); svrFactory.setServiceBean(serviceImpl); http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/PersonServiceRPC.java ---------------------------------------------------------------------- diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/PersonServiceRPC.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/PersonServiceRPC.java new file mode 100644 index 0000000..7af971a --- /dev/null +++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/PersonServiceRPC.java @@ -0,0 +1,45 @@ +/** + * 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.systest.jaxws.schemavalidation; + +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebService; +import javax.jws.soap.SOAPBinding; + +import org.apache.cxf.annotations.SchemaValidation; +import org.apache.cxf.annotations.SchemaValidation.SchemaValidationType; + +@WebService(name = "PersonServiceRPC", targetNamespace = "http://org.apache.cxf/service/PersonService") +@SOAPBinding(style = SOAPBinding.Style.RPC) +@SchemaValidation(type = SchemaValidationType.BOTH) +public interface PersonServiceRPC { + @WebMethod(operationName = "saveInheritEndpoint") + Person saveInheritEndpoint(@WebParam(name = "Person") Person data); + + @WebMethod(operationName = "saveNoValidation") + Person saveNoValidation(@WebParam(name = "Person") Person data); + + @WebMethod(operationName = "saveValidateIn") + Person saveValidateIn(@WebParam(name = "Person") Person data); + + @WebMethod(operationName = "saveValidateOut") + Person saveValidateOut(@WebParam(name = "Person") Person data); +} http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/PersonServiceRPCImpl.java ---------------------------------------------------------------------- diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/PersonServiceRPCImpl.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/PersonServiceRPCImpl.java new file mode 100644 index 0000000..d11b3fa --- /dev/null +++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/PersonServiceRPCImpl.java @@ -0,0 +1,47 @@ +/** + * 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.systest.jaxws.schemavalidation; + +import javax.jws.WebService; + +@WebService(endpointInterface = "org.apache.cxf.systest.jaxws.schemavalidation.PersonServiceRPC", + serviceName = "PersonServiceRPC", + targetNamespace = "http://org.apache.cxf/service/PersonService") +public class PersonServiceRPCImpl implements PersonServiceRPC { + @Override + public Person saveNoValidation(Person data) { + return data; + } + + @Override + public Person saveInheritEndpoint(Person data) { + return data; + } + + @Override + public Person saveValidateIn(Person data) { + return data; + } + + @Override + public Person saveValidateOut(Person data) { + return data; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/9ef7c5eb/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/package-info.java ---------------------------------------------------------------------- diff --git a/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/package-info.java b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/package-info.java new file mode 100644 index 0000000..4ae90c0 --- /dev/null +++ b/systests/jaxws/src/test/java/org/apache/cxf/systest/jaxws/schemavalidation/package-info.java @@ -0,0 +1,21 @@ +/** + * 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. + */ [email protected](namespace = "http://org.apache.cxf/service/PersonService", + elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) +package org.apache.cxf.systest.jaxws.schemavalidation;
