You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by ro...@apache.org on 2009/11/09 18:22:49 UTC

svn commit: r834150 - in /incubator/wink/trunk/wink-providers/wink-jettison-provider/src: main/java/org/apache/wink/providers/jettison/ test/java/org/apache/wink/providers/jettison/internal/ test/java/org/apache/wink/providers/jettison/internal/jaxb2/

Author: rott
Date: Mon Nov  9 17:22:43 2009
New Revision: 834150

URL: http://svn.apache.org/viewvc?rev=834150&view=rev
Log:
WINK-229: tolerate XmlRootElement in JAXB objects, but JAXBContext created with package string, causing it to use xjc-generated ObjectFactory

Added:
    incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/JettisonJAXBContextResolverTest.java
    incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/MyJAXBResolver.java
    incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/
    incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/AddNumbers.java
    incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/ObjectFactory.java
Modified:
    incubator/wink/trunk/wink-providers/wink-jettison-provider/src/main/java/org/apache/wink/providers/jettison/JettisonJAXBProvider.java

Modified: incubator/wink/trunk/wink-providers/wink-jettison-provider/src/main/java/org/apache/wink/providers/jettison/JettisonJAXBProvider.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jettison-provider/src/main/java/org/apache/wink/providers/jettison/JettisonJAXBProvider.java?rev=834150&r1=834149&r2=834150&view=diff
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jettison-provider/src/main/java/org/apache/wink/providers/jettison/JettisonJAXBProvider.java (original)
+++ incubator/wink/trunk/wink-providers/wink-jettison-provider/src/main/java/org/apache/wink/providers/jettison/JettisonJAXBProvider.java Mon Nov  9 17:22:43 2009
@@ -38,6 +38,7 @@
 import javax.ws.rs.ext.MessageBodyWriter;
 import javax.ws.rs.ext.Provider;
 import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBElement;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
@@ -132,23 +133,27 @@
             unmarshaller = getJAXBUnmarshaller(context);
 
             XMLStreamReader xsr = null;
+
+            if (isBadgerFishConventionUsed) {
+                xsr = new BadgerFishXMLInputFactory().createXMLStreamReader(entityStream);
+            } else {
+                xsr =
+                    new MappedXMLInputFactory(inputConfiguration)
+                .createXMLStreamReader(entityStream);
+            }
+
             if (type.isAnnotationPresent(XmlRootElement.class)) {
-                if (isBadgerFishConventionUsed) {
-                    xsr = new BadgerFishXMLInputFactory().createXMLStreamReader(entityStream);
-                } else {
-                    xsr =
-                        new MappedXMLInputFactory(inputConfiguration)
-                            .createXMLStreamReader(entityStream);
-                }
                 unmarshaledResource = unmarshaller.unmarshal(xsr);
-            } else {
-                if (isBadgerFishConventionUsed) {
-                    xsr = new BadgerFishXMLInputFactory().createXMLStreamReader(entityStream);
-                } else {
-                    xsr =
-                        new MappedXMLInputFactory(inputConfiguration)
-                            .createXMLStreamReader(entityStream);
+                if (unmarshaledResource instanceof JAXBElement) {
+                    // this can happen if the JAXBContext object used to create the unmarshaller
+                    // was created using the package name string instead of a class object and the
+                    // ObjectFactory has a creator method for the desired object that returns
+                    // JAXBElement.  But we know better; the 'type' param passed in here had the
+                    // XmlRootElement on it, so we know the desired return object type is NOT
+                    // JAXBElement, thus:
+                    unmarshaledResource = ((JAXBElement)unmarshaledResource).getValue();
                 }
+            } else {
                 unmarshaledResource = unmarshaller.unmarshal(xsr, type).getValue();
             }
         } catch (JAXBException e) {

Added: incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/JettisonJAXBContextResolverTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/JettisonJAXBContextResolverTest.java?rev=834150&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/JettisonJAXBContextResolverTest.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/JettisonJAXBContextResolverTest.java Mon Nov  9 17:22:43 2009
@@ -0,0 +1,85 @@
+/*******************************************************************************
+ * 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.wink.providers.jettison.internal;
+
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+
+import org.apache.wink.providers.jettison.JettisonJAXBElementProvider;
+import org.apache.wink.providers.jettison.JettisonJAXBProvider;
+import org.apache.wink.providers.jettison.internal.jaxb2.AddNumbers;
+import org.apache.wink.providers.json.JSONUtils;
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+public class JettisonJAXBContextResolverTest extends MockServletInvocationTest {
+
+    @Override
+    protected Class<?>[] getClasses() {
+        return new Class<?>[] {AddNumbersResource.class, MyJAXBResolver.class};
+    }
+    
+    @Override
+    protected Object[] getSingletons() {
+        JettisonJAXBProvider jaxbProvider = new JettisonJAXBProvider(true, null, null);
+        jaxbProvider.setUseAsReader(true);
+
+        JettisonJAXBElementProvider jaxbElementProvider =
+            new JettisonJAXBElementProvider(true, null, null);
+        jaxbElementProvider.setUseAsReader(true);
+
+        return new Object[] {jaxbProvider, jaxbElementProvider};
+    }
+    
+    @Override
+    public String getPropertiesFile() {
+        return "META-INF/wink.properties";
+    }
+    
+    @Path("/test/addnumbers")
+    public static class AddNumbersResource {
+
+        @POST
+        public AddNumbers postAddNumbers(AddNumbers a) {
+            return a;
+        }
+    }
+    
+    
+    @Test
+    public void testJAXBUnmarshallingWithAlternateContext1() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor.constructMockRequest("POST", "/test/addnumbers", "application/json");
+        request.setContentType("application/json");
+
+        request.setContent(" { \"addNumbers\" : { \"arg0\" : { \"$\":\"1\"}, \"arg1\" : { \"$\":\"2\" } } } "
+            .getBytes());
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertTrue(JSONUtils
+            .equals(new JSONObject("{\"addNumbers\":{\"@xmlns\":{\"ns2\":\"http://org/apache/wink/providers/jettison/internal/jaxb2\"},\"arg0\":{\"$\":\"1\"},\"arg1\":{\"$\":\"2\"}}}"),
+                    new JSONObject(response.getContentAsString())));
+    }
+    
+}

Added: incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/MyJAXBResolver.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/MyJAXBResolver.java?rev=834150&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/MyJAXBResolver.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/MyJAXBResolver.java Mon Nov  9 17:22:43 2009
@@ -0,0 +1,38 @@
+/*******************************************************************************
+ * 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.wink.providers.jettison.internal;
+
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+
+    @Provider
+    public class MyJAXBResolver implements ContextResolver<JAXBContext> {
+
+        public JAXBContext getContext(Class arg0) {
+            try {
+                return JAXBContext.newInstance(arg0.getPackage().getName());
+            } catch (JAXBException e) {
+                return null;
+            }
+        }
+
+    }
\ No newline at end of file

Added: incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/AddNumbers.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/AddNumbers.java?rev=834150&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/AddNumbers.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/AddNumbers.java Mon Nov  9 17:22:43 2009
@@ -0,0 +1,103 @@
+/*******************************************************************************
+ * 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.
+ *  
+ *******************************************************************************/
+
+//
+// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-558 
+// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
+// Any modifications to this file will be lost upon recompilation of the source schema. 
+// Generated on: 2009.08.20 at 04:05:47 PM CDT 
+//
+
+
+package org.apache.wink.providers.jettison.internal.jaxb2;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlType;
+
+
+/**
+ * <p>Java class for anonymous complex type.
+ * 
+ * <p>The following schema fragment specifies the expected content contained within this class.
+ * 
+ * <pre>
+ * &lt;complexType>
+ *   &lt;complexContent>
+ *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       &lt;sequence>
+ *         &lt;element name="arg0" type="{http://www.w3.org/2001/XMLSchema}int"/>
+ *         &lt;element name="arg1" type="{http://www.w3.org/2001/XMLSchema}int"/>
+ *       &lt;/sequence>
+ *     &lt;/restriction>
+ *   &lt;/complexContent>
+ * &lt;/complexType>
+ * </pre>
+ * 
+ * 
+ */
+@XmlAccessorType(XmlAccessType.FIELD)
+@XmlType(name = "", propOrder = {
+    "arg0",
+    "arg1"
+})
+// xjc will not generate XmlRootElement annotation; we are simulating
+// what would happen if a user adds this annotation by-hand, and attempting
+// to tolerate it in the provider(s)
+@XmlRootElement(name = "addNumbers")
+public class AddNumbers {
+
+    protected int arg0;
+    protected int arg1;
+
+    /**
+     * Gets the value of the arg0 property.
+     * 
+     */
+    public int getArg0() {
+        return arg0;
+    }
+
+    /**
+     * Sets the value of the arg0 property.
+     * 
+     */
+    public void setArg0(int value) {
+        this.arg0 = value;
+    }
+
+    /**
+     * Gets the value of the arg1 property.
+     * 
+     */
+    public int getArg1() {
+        return arg1;
+    }
+
+    /**
+     * Sets the value of the arg1 property.
+     * 
+     */
+    public void setArg1(int value) {
+        this.arg1 = value;
+    }
+
+}

Added: incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/ObjectFactory.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/ObjectFactory.java?rev=834150&view=auto
==============================================================================
--- incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/ObjectFactory.java (added)
+++ incubator/wink/trunk/wink-providers/wink-jettison-provider/src/test/java/org/apache/wink/providers/jettison/internal/jaxb2/ObjectFactory.java Mon Nov  9 17:22:43 2009
@@ -0,0 +1,81 @@
+/*******************************************************************************
+ * 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.
+ *  
+ *******************************************************************************/
+
+//
+// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-558 
+// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
+// Any modifications to this file will be lost upon recompilation of the source schema. 
+// Generated on: 2009.08.20 at 04:05:47 PM CDT 
+//
+
+
+package org.apache.wink.providers.jettison.internal.jaxb2;
+
+import javax.xml.bind.JAXBElement;
+import javax.xml.bind.annotation.XmlElementDecl;
+import javax.xml.bind.annotation.XmlRegistry;
+import javax.xml.namespace.QName;
+
+
+/**
+ * This object contains factory methods for each 
+ * Java content interface and Java element interface 
+ * generated in the org.apache.wink.common.internal.providers.jaxb.jaxb1 package. 
+ * <p>An ObjectFactory allows you to programatically 
+ * construct new instances of the Java representation 
+ * for XML content. The Java representation of XML 
+ * content can consist of schema derived interfaces 
+ * and classes representing the binding of schema 
+ * type definitions, element declarations and model 
+ * groups.  Factory methods for each of these are 
+ * provided in this class.
+ * 
+ */
+@XmlRegistry
+public class ObjectFactory {
+
+    private final static QName _AddNumbers_QNAME = new QName("http://org/apache/wink/providers/jettison/internal/jaxb2", "addNumbers");
+
+    /**
+     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.apache.wink.providers.jettison.internal.jaxb2
+     * 
+     */
+    public ObjectFactory() {
+    }
+
+    /**
+     * Create an instance of {@link AddNumbers }
+     * 
+     */
+    public AddNumbers createAddNumbers() {
+        return new AddNumbers();
+    }
+    
+    /**
+     * Create an instance of {@link JAXBElement }{@code <}{@link AddNumbers }{@code >}}
+     * 
+     */
+    @XmlElementDecl(namespace = "http://org/apache/wink/providers/jettison/internal/jaxb2", name = "addNumbers")
+    public JAXBElement<AddNumbers> createAddNumbers(AddNumbers value) {
+        return new JAXBElement<AddNumbers>(_AddNumbers_QNAME, AddNumbers.class, null, value);
+    }
+
+
+}