From 671666f51ccf084c7915134846183a188b274a6c Mon Sep 17 00:00:00 2001 From: Jim Ma Date: Fri, 22 Apr 2016 11:28:33 +0800 Subject: [PATCH] CXF-6877:Have @SchemaValidation working on service endpoint implementation class method --- .../AnnotationsFactoryBeanListener.java | 18 +++++++++++-- .../ValidationClientServerTest.java | 25 +++++++++++++++++++ .../schema_validation/ValidationServer.java | 14 +++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java b/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java index 0fcb91479db..8eb7d0e3d0b 100644 --- a/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java +++ b/core/src/main/java/org/apache/cxf/service/factory/AnnotationsFactoryBeanListener.java @@ -123,7 +123,22 @@ public void handleEvent(Event ev, AbstractServiceFactoryBean factory, Object... docs.toArray(new WSDLDocumentation[docs.size()])); } addBindingOperationDocs(ep); - + for (Method method : implCls.getMethods()) { + if (method.getAnnotation(SchemaValidation.class) != null) { + try { + Method interfaceMethod = cls.getMethod(method.getName(), method.getParameterTypes()); + for (BindingOperationInfo bopInfo : ep.getBinding().getBindingInfo().getOperations()) { + if (interfaceMethod.equals(bopInfo.getOperationInfo() + .getProperty("operation.method"))) { + addSchemaValidationSupport(bopInfo.getOperationInfo(), + method.getAnnotation(SchemaValidation.class)); + } + } + } catch (Exception e) { + // ignore this + } + } + } break; } case SERVER_CREATED: { @@ -156,7 +171,6 @@ public void handleEvent(Event ev, AbstractServiceFactoryBean factory, Object... if (col != null) { addDocumentation(inf, WSDLDocumentation.Placement.PORT_TYPE_OPERATION, col.value()); } - SchemaValidation methodValidation = m.getAnnotation(SchemaValidation.class); if (methodValidation != null) { addSchemaValidationSupport(inf, methodValidation); diff --git a/systests/uncategorized/src/test/java/org/apache/cxf/systest/schema_validation/ValidationClientServerTest.java b/systests/uncategorized/src/test/java/org/apache/cxf/systest/schema_validation/ValidationClientServerTest.java index 3dfb0a9aeb8..8fe92bb6b30 100644 --- a/systests/uncategorized/src/test/java/org/apache/cxf/systest/schema_validation/ValidationClientServerTest.java +++ b/systests/uncategorized/src/test/java/org/apache/cxf/systest/schema_validation/ValidationClientServerTest.java @@ -98,6 +98,31 @@ public void testSchemaValidationServer() throws Exception { runSchemaValidationTest(validation); ((java.io.Closeable)validation).close(); } + + @Test + public void testSchemaValidationServerForMethod() throws Exception { + SchemaValidation validation = createService(Boolean.FALSE, "SoapPortMethodValidate"); + ComplexStruct complexStruct = new ComplexStruct(); + complexStruct.setElem1("one"); + complexStruct.setElem3(3); + try { + validation.setComplexStruct(complexStruct); + fail("Set ComplexStruct should have thrown ProtocolException"); + } catch (WebServiceException e) { + String expected = "'{\"http://apache.org/schema_validation/types\":elem2}' is expected."; + assertTrue(e.getMessage(), e.getMessage().indexOf(expected) != -1); + } + + SchemaValidation novlidation = createService(Boolean.FALSE, "SoapPort"); + try { + novlidation.setComplexStruct(complexStruct); + + } catch (WebServiceException e) { + fail("Exception is not expected :" + e); + } + + } + @Test public void testSchemaValidationClient() throws Exception { SchemaValidation validation = createService(Boolean.TRUE, "SoapPort"); diff --git a/systests/uncategorized/src/test/java/org/apache/cxf/systest/schema_validation/ValidationServer.java b/systests/uncategorized/src/test/java/org/apache/cxf/systest/schema_validation/ValidationServer.java index 5f2404dd00e..dd6d3a8fa33 100644 --- a/systests/uncategorized/src/test/java/org/apache/cxf/systest/schema_validation/ValidationServer.java +++ b/systests/uncategorized/src/test/java/org/apache/cxf/systest/schema_validation/ValidationServer.java @@ -42,6 +42,7 @@ import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.staxutils.StaxUtils; import org.apache.cxf.testutil.common.AbstractBusTestServerBase; +import org.apache.schema_validation.types.ComplexStruct; public class ValidationServer extends AbstractBusTestServerBase { public static final String PORT = allocatePort(ValidationServer.class); @@ -58,6 +59,7 @@ protected void run() { eps.add(Endpoint.publish(address + "/SoapPortValidate", new ValidatingSchemaValidationImpl())); eps.add(Endpoint.publish(address + "/PProvider", new PayloadProvider())); eps.add(Endpoint.publish(address + "/MProvider", new MessageProvider())); + eps.add(Endpoint.publish(address + "/SoapPortMethodValidate", new ValidatingSchemaValidationMethodImpl())); } public void tearDown() throws Exception { @@ -76,6 +78,18 @@ static class ValidatingSchemaValidationImpl extends SchemaValidationImpl { } + @WebService(serviceName = "SchemaValidationService", + portName = "SoapPort", + endpointInterface = "org.apache.schema_validation.SchemaValidation", + targetNamespace = "http://apache.org/schema_validation", + wsdlLocation = "classpath:/wsdl/schema_validation.wsdl") + static class ValidatingSchemaValidationMethodImpl extends SchemaValidationImpl { + @SchemaValidation + public boolean setComplexStruct(ComplexStruct in) { + return true; + } + } + private static String getResponse(String v) { if ("9999999999".equals(v)) { return ""