Author: klape
Date: 2013-08-02 19:57:58 -0400 (Fri, 02 Aug 2013)
New Revision: 17851
Added:
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java
Modified:
thirdparty/cxf/branches/cxf-2.2.12/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java
Log:
[JBPAPP-10834] Support marshalling exception classes with @XmlAccessorOrder in
JAXBEncoderDecoder
Modified:
thirdparty/cxf/branches/cxf-2.2.12/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java
===================================================================
---
thirdparty/cxf/branches/cxf-2.2.12/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java 2013-08-02
00:34:53 UTC (rev 17850)
+++
thirdparty/cxf/branches/cxf-2.2.12/common/common/src/main/java/org/apache/cxf/staxutils/StaxUtils.java 2013-08-02
23:57:58 UTC (rev 17851)
@@ -361,6 +361,9 @@
}
return false;
}
+ public static void writeTo(Node node, OutputStream os) throws XMLStreamException {
+ copy(new DOMSource(node), os);
+ }
public static void copy(Source source, OutputStream os) throws XMLStreamException {
XMLStreamWriter writer = createXMLStreamWriter(os);
try {
Modified:
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java
===================================================================
---
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java 2013-08-02
00:34:53 UTC (rev 17850)
+++
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/main/java/org/apache/cxf/jaxb/JAXBEncoderDecoder.java 2013-08-02
23:57:58 UTC (rev 17851)
@@ -26,6 +26,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
+import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
@@ -34,6 +35,8 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
@@ -47,7 +50,9 @@
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
+import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import javax.xml.bind.attachment.AttachmentMarshaller;
@@ -346,10 +351,31 @@
} else {
LOG.warning("Schema associated with " + namespace + " is
null");
}
-
+
+ List<Member> combinedMembers = new ArrayList<Member>();
+
for (Field f : Utils.getFields(cls, accessType)) {
XmlAttribute at = f.getAnnotation(XmlAttribute.class);
if (at == null) {
+ combinedMembers.add(f);
+ }
+ }
+ for (Method m : Utils.getGetters(cls, accessType)) {
+ combinedMembers.add(m);
+ }
+
+ XmlAccessorOrder xmlAccessorOrder =
cls.getAnnotation(XmlAccessorOrder.class);
+ if (xmlAccessorOrder != null &&
xmlAccessorOrder.value().equals(XmlAccessOrder.ALPHABETICAL)) {
+ Collections.sort(combinedMembers, new Comparator<Member>() {
+ public int compare(Member m1, Member m2) {
+ return m1.getName().compareTo(m2.getName());
+ }
+ });
+ }
+
+ for (Member member : combinedMembers) {
+ if (member instanceof Field) {
+ Field f = (Field)member;
QName fname = new QName(namespace, f.getName());
f.setAccessible(true);
if (JAXBSchemaInitializer.isArray(f.getGenericType())) {
@@ -358,20 +384,20 @@
Object o = Utils.getFieldValue(f, elValue);
writeObject(marshaller, writer, new JAXBElement(fname,
String.class, o));
}
+ } else { // it's a Method
+ Method m = (Method)member;
+ int idx = m.getName().startsWith("get") ? 3 : 2;
+ String name = m.getName().substring(idx);
+ name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
+ QName mname = new QName(namespace, name);
+ if (JAXBSchemaInitializer.isArray(m.getGenericReturnType())) {
+ writeArrayObject(marshaller, writer, mname, m.invoke(elValue));
+ } else {
+ Object o = Utils.getMethodValue(m, elValue);
+ writeObject(marshaller, writer, new JAXBElement(mname,
String.class, o));
+ }
}
}
- for (Method m : Utils.getGetters(cls, accessType)) {
- int idx = m.getName().startsWith("get") ? 3 : 2;
- String name = m.getName().substring(idx);
- name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
- QName mname = new QName(namespace, name);
- if (JAXBSchemaInitializer.isArray(m.getGenericReturnType())) {
- writeArrayObject(marshaller, writer, mname, m.invoke(elValue));
- } else {
- Object o = Utils.getMethodValue(m, elValue);
- writeObject(marshaller, writer, new JAXBElement(mname, String.class,
o));
- }
- }
writer.writeEndElement();
writer.flush();
} catch (Exception e) {
Modified:
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java
===================================================================
---
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java 2013-08-02
00:34:53 UTC (rev 17850)
+++
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/JAXBEncoderDecoderTest.java 2013-08-02
23:57:58 UTC (rev 17851)
@@ -53,8 +53,13 @@
import org.apache.cxf.jaxb_form.ObjectWithQualifiedElementElement;
import org.apache.cxf.jaxb_misc.Base64WithDefaultValueType;
import org.apache.cxf.jaxb_misc.ObjectFactory;
+import org.apache.cxf.service.model.InterfaceInfo;
+import org.apache.cxf.service.model.MessageInfo;
import org.apache.cxf.service.model.MessagePartInfo;
+import org.apache.cxf.service.model.OperationInfo;
+import org.apache.cxf.service.model.ServiceInfo;
import org.apache.cxf.staxutils.StaxStreamFilter;
+import org.apache.cxf.staxutils.StaxUtils;
import org.apache.cxf.testutil.common.TestUtil;
import org.apache.hello_world_soap_http.Greeter;
import org.apache.hello_world_soap_http.types.GreetMe;
@@ -389,6 +394,47 @@
}
@Test
+ public void testMarshallExceptionWithOrder() throws Exception {
+ Document doc = DOMUtils.createDocument();
+ Element elNode = doc.createElementNS("http://cxf.apache.org",
"ExceptionRoot");
+
+ OrderException exception = new OrderException("Mymessage");
+ exception.setAValue("avalue");
+ exception.setDetail("detail");
+ exception.setInfo1("info1");
+ exception.setInfo2("info2");
+ exception.setIntVal(10000);
+
+ QName elName = new QName("http://cxf.apache.org",
"OrderException");
+ ServiceInfo serviceInfo = new ServiceInfo();
+ InterfaceInfo interfaceInfo = new InterfaceInfo(serviceInfo, null);
+ OperationInfo op = interfaceInfo.addOperation(new
QName("http://cxf.apache.org", "operation"));
+ MessageInfo message = new MessageInfo(op, null, null);
+ MessagePartInfo part = new MessagePartInfo(elName, message);
+ part.setElement(true);
+ part.setElementQName(elName);
+ part.setTypeClass(OrderException.class);
+
+ //just need a simple generic context to handle the exceptions internal
primitives
+ JAXBContext exceptionContext = JAXBContext.newInstance(new Class[] {
+ String.class,
+ });
+ JAXBEncoderDecoder.marshallException(exceptionContext.createMarshaller(),
exception, part, elNode);
+
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ StaxUtils.writeTo(elNode, bout);
+ int a = bout.toString().lastIndexOf("aValue");
+ int b = bout.toString().lastIndexOf("detail");
+ int c = bout.toString().lastIndexOf("info1");
+ int d = bout.toString().lastIndexOf("info2");
+ int e = bout.toString().lastIndexOf("intVal");
+ assertTrue(a < b);
+ assertTrue(b < c);
+ assertTrue(c < d);
+ assertTrue(d < e);
+ }
+
+ @Test
public void testMarshallWithoutQNameInfo() throws Exception {
GreetMe obj = new GreetMe();
obj.setRequestType("Hello");
Added:
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java
===================================================================
---
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java
(rev 0)
+++
thirdparty/cxf/branches/cxf-2.2.12/rt/databinding/jaxb/src/test/java/org/apache/cxf/jaxb/OrderException.java 2013-08-02
23:57:58 UTC (rev 17851)
@@ -0,0 +1,84 @@
+/**
+ * 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;
+
+import javax.xml.bind.annotation.XmlAccessOrder;
+import javax.xml.bind.annotation.XmlAccessorOrder;
+
+(a)XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
+public class OrderException extends Exception {
+ private static final long serialVersionUID = 1L;
+
+ private String info1;
+
+ private String info2;
+
+ private String aValue;
+
+ private int intVal;
+
+ private String detail;
+
+ public OrderException(String message) {
+ super(message);
+ }
+
+
+ public String getAValue() {
+ return aValue;
+ }
+
+ public void setAValue(String value) {
+ this.aValue = value;
+ }
+
+ public String getInfo1() {
+ return info1;
+ }
+
+ public void setInfo1(String info1) {
+ this.info1 = info1;
+ }
+
+ public String getInfo2() {
+ return info2;
+ }
+
+ public void setInfo2(String info2) {
+ this.info2 = info2;
+ }
+
+
+ public int getIntVal() {
+ return intVal;
+ }
+
+ public void setIntVal(int intVal) {
+ this.intVal = intVal;
+ }
+
+ public String getDetail() {
+ return detail;
+ }
+
+ public void setDetail(String detail) {
+ this.detail = detail;
+ }
+
+}