Skip to content
Permalink
Browse files
[CXF-5392] Ability to set validation handlers on the writers
Modified patch from Joe Wertz applied

git-svn-id: https://svn.apache.org/repos/asf/cxf/trunk@1543519 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dkulp committed Nov 19, 2013
1 parent 106b942 commit 9c4675b7977e08fd9d6391313d13f0f8e08062cb
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 3 deletions.
@@ -85,7 +85,10 @@ public void setProperty(String prop, Object value) {
unwrapJAXBElement = Boolean.TRUE.equals(value);
} else if (prop.equals(org.apache.cxf.message.Message.class.getName())) {
org.apache.cxf.message.Message m = (org.apache.cxf.message.Message)value;
veventHandler = (ValidationEventHandler)m.getContextualProperty("jaxb-validation-event-handler");
veventHandler = (ValidationEventHandler)m.getContextualProperty("jaxb-reader-validation-event-handler");
if (veventHandler == null) {
veventHandler = (ValidationEventHandler)m.getContextualProperty("jaxb-validation-event-handler");
}
if (veventHandler == null) {
veventHandler = databinding.getValidationEventHandler();
}
@@ -46,12 +46,15 @@
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.jaxb.JAXBEncoderDecoder;
import org.apache.cxf.jaxb.attachment.JAXBAttachmentMarshaller;
import org.apache.cxf.message.MessageUtils;
import org.apache.cxf.service.model.MessagePartInfo;
import org.apache.ws.commons.schema.XmlSchemaElement;

public class DataWriterImpl<T> extends JAXBDataBase implements DataWriter<T> {
private static final Logger LOG = LogUtils.getLogger(JAXBDataBinding.class);

ValidationEventHandler veventHandler;
boolean setEventHandler = true;
private JAXBDataBinding databinding;

public DataWriterImpl(JAXBDataBinding binding) {
@@ -62,6 +65,23 @@ public DataWriterImpl(JAXBDataBinding binding) {
public void write(Object obj, T output) {
write(obj, null, output);
}

public void setProperty(String prop, Object value) {
if (prop.equals(org.apache.cxf.message.Message.class.getName())) {
org.apache.cxf.message.Message m = (org.apache.cxf.message.Message)value;
veventHandler = (ValidationEventHandler)m.getContextualProperty(
"jaxb-writer-validation-event-handler");
if (veventHandler == null) {
veventHandler = (ValidationEventHandler)m.getContextualProperty(
"jaxb-validation-event-handler");
}
if (veventHandler == null) {
veventHandler = databinding.getValidationEventHandler();
}
setEventHandler = MessageUtils.getContextualBoolean(m, "set-jaxb-validation-event-handler", true);
}
}

private static class MtomValidationHandler implements ValidationEventHandler {
ValidationEventHandler origHandler;
JAXBAttachmentMarshaller marshaller;
@@ -106,8 +126,8 @@ public Marshaller createMarshaller(Object elValue, MessagePartInfo part) {
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
marshaller.setListener(databinding.getMarshallerListener());
if (databinding.getValidationEventHandler() != null) {
marshaller.setEventHandler(databinding.getValidationEventHandler());
if (setEventHandler) {
marshaller.setEventHandler(veventHandler);
}

final Map<String, String> nspref = databinding.getDeclaredNamespaceMappings();
@@ -0,0 +1,37 @@
/**
* 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.jaxb.io;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

public class MyCustomHandler implements ValidationEventHandler {

private boolean used;
public boolean getUsed() {
return used;
}

public boolean handleEvent(ValidationEvent event) {
used = true;
return true;
}

}
@@ -56,6 +56,32 @@ public void tearDown() throws IOException {
is.close();
}

@Test
public void testSetProperty() throws Exception {
JAXBDataBinding db = getDataBinding(GreetMe.class);

reader = getTestReader("../resources/SetPropertyValidationFailureReq.xml");
assertNotNull(reader);

DataReaderImpl<XMLStreamReader> dr = (DataReaderImpl)db.createReader(XMLStreamReader.class);
assertNotNull(dr);

// Build message to set custom event handler
org.apache.cxf.message.Message message = new org.apache.cxf.message.MessageImpl();
message.put("jaxb-validation-event-handler", new MyCustomHandler());
message.put("unwrap.jaxb.element", true);

dr.setProperty("org.apache.cxf.message.Message", message);

// Should fail if custom handler doesn't skip formatting error
Object val = dr.read(reader);
assertTrue(val instanceof GreetMe);
assertEquals("TestSOAPInputPMessage", ((GreetMe)val).getRequestType());

// Check handler used
assertTrue(((MyCustomHandler)dr.veventHandler).getUsed());
}

@Test
public void testReadWrapper() throws Exception {
JAXBDataBinding db = getDataBinding(GreetMe.class);
@@ -66,6 +66,35 @@ public void setUp() throws Exception {
public void tearDown() throws Exception {
baos.close();
}

@Test
public void testSetProperty() throws Exception {
JAXBDataBinding db = getTestWriterFactory();

DataWriterImpl<XMLStreamWriter> dw = (DataWriterImpl)db.createWriter(XMLStreamWriter.class);
assertNotNull(dw);

// Build message to set custom event handler
org.apache.cxf.message.Message message = new org.apache.cxf.message.MessageImpl();
message.put("jaxb-writer-validation-event-handler", new MyCustomHandler());

dw.setProperty("org.apache.cxf.message.Message", message);

// Write Stuff
TradePriceData val = new TradePriceData();
val.setTickerSymbol("This is a symbol");
val.setTickerPrice(1.0f);

QName elName = new QName("http://apache.org/hello_world_doc_lit_bare/types", "inout");
MessagePartInfo part = new MessagePartInfo(elName, null);
part.setElement(true);
part.setElementQName(elName);
dw.write(val, part, streamWriter);
streamWriter.flush();

// Test MyCustomHandler
assertTrue(((MyCustomHandler)dw.veventHandler).getUsed());
}

@Test
public void testWriteRPCLit1() throws Exception {
@@ -0,0 +1,22 @@
<!--
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.
-->
<ns4:greetMe xmlns:ns4="http://apache.org/hello_world_soap_http/types">
<ns4:requestType>TestSOAPInputPMessage</ns4:requestType>
<ns4:somethingElse>bla bla bla</ns4:somethingElse>
</ns4:greetMe>
@@ -0,0 +1,67 @@
/**
* 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.jaxb.validators;

import org.apache.cxf.testutil.common.TestUtil;
import org.junit.Assert;
import org.junit.Test;

import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
/**
*
*/
@ContextConfiguration(locations = { "classpath:jaxbCustomValidators.xml" })
public class CustomValidatorJAXBTest extends AbstractJUnit4SpringContextTests {
static final String PORT = TestUtil.getPortNumber(CustomValidatorJAXBTest.class);

@Test
public void cleanTest() {
HelloWorld client = (HelloWorld)applicationContext
.getBean("testClient", HelloWorld.class);

PassedObject hi = client.sayHi(new PassedObject("John", "Doe"));
Assert.assertTrue("Expected: 'Hello John Doe' Actual: " + hi.getName(),
"Hello John Doe".equals(hi.getName()));

}

@Test
public void sendNullTest() {
HelloWorld client = (HelloWorld)applicationContext
.getBean("testClient", HelloWorld.class);

PassedObject hi = client.sayHi(new PassedObject());
Assert.assertTrue("Expected: 'Hello null null' Actual: '" + hi.getName() + "'",
"Hello null null".equals(hi.getName()));

}

@Test
public void returnNullTest() {
HelloWorld client = (HelloWorld)applicationContext
.getBean("testClient", HelloWorld.class);

PassedObject hi = client.returnNull(new PassedObject("John", "Doe"));
Assert.assertTrue("Expected: 'Hello null' Actual: '" + hi.getName() + "'",
"Hello null".equals(hi.getName()));

}
}
@@ -0,0 +1,33 @@
/**
* 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.jaxb.validators;

import javax.jws.WebMethod;
import javax.jws.WebParam;

@javax.jws.WebService(targetNamespace = "aNamespace")
public interface HelloWorld {

@WebMethod(operationName = "sayHi")
PassedObject sayHi(@WebParam(name = "text") PassedObject name);

@WebMethod(operationName = "returnNull")
PassedObject returnNull(@WebParam(name = "text") PassedObject name);
}

@@ -0,0 +1,33 @@
/**
* 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.jaxb.validators;

@javax.jws.WebService(endpointInterface = "org.apache.cxf.systest.jaxb.validators.HelloWorld")
public class HelloWorldImpl implements HelloWorld {

public PassedObject sayHi(PassedObject name) {
return new PassedObject("Hello", name.getName());
}

public PassedObject returnNull(PassedObject name) {
return new PassedObject("Hello", null);
}

}

@@ -0,0 +1,37 @@
/**
* 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.jaxb.validators;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

public class MyCustomHandler implements ValidationEventHandler {

private boolean used;
public boolean getUsed() {
return used;
}

public boolean handleEvent(ValidationEvent event) {
used = true;
return true;
}

}

0 comments on commit 9c4675b

Please sign in to comment.