You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2009/12/17 14:19:35 UTC

svn commit: r891691 - in /camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util: JAXBContextResolver.java JAXBMarshallerResolver.java

Author: jstrachan
Date: Thu Dec 17 13:19:33 2009
New Revision: 891691

URL: http://svn.apache.org/viewvc?rev=891691&view=rev
Log:
fix for CAMEL-2280 to use better namespace prefixes (using no prefix for the default Camel spring namespace)

Added:
    camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBMarshallerResolver.java   (with props)
Modified:
    camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBContextResolver.java

Modified: camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBContextResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBContextResolver.java?rev=891691&r1=891690&r2=891691&view=diff
==============================================================================
--- camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBContextResolver.java (original)
+++ camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBContextResolver.java Thu Dec 17 13:19:33 2009
@@ -16,12 +16,13 @@
  */
 package org.apache.camel.web.util;
 
+import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
+
 import javax.ws.rs.ext.ContextResolver;
 import javax.ws.rs.ext.Provider;
 import javax.xml.bind.JAXBContext;
-
-import com.sun.jersey.api.json.JSONConfiguration;
-import com.sun.jersey.api.json.JSONJAXBContext;
+import java.util.HashMap;
+import java.util.Map;
 
 /**
  * A resolver of the JAXB context primed for the Camel XML languages
@@ -35,13 +36,8 @@
     private String packages;
 
     public JAXBContextResolver() throws Exception {
-
-        // TODO we can't use natural with JAXB 2.1.6 or 2.1 for some reason?
-        JSONConfiguration.Builder builder = JSONConfiguration.mapped();
-        //JSONConfiguration.Builder builder = JSONConfiguration.natural();
-
         this.packages = org.apache.camel.web.resources.Constants.JAXB_PACKAGES;
-        this.context = new JSONJAXBContext(builder.build(), packages);
+        this.context = JAXBContext.newInstance(packages);
     }
 
     public JAXBContext getContext(Class objectType) {

Added: camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBMarshallerResolver.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBMarshallerResolver.java?rev=891691&view=auto
==============================================================================
--- camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBMarshallerResolver.java (added)
+++ camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBMarshallerResolver.java Thu Dec 17 13:19:33 2009
@@ -0,0 +1,101 @@
+/**
+ *
+ * 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.camel.web.util;
+
+import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
+
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.Provider;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+@Provider
+public class JAXBMarshallerResolver implements ContextResolver<Marshaller> {
+    private JAXBContextResolver contextResolver;
+
+    public JAXBMarshallerResolver() throws Exception {
+        contextResolver = new JAXBContextResolver();
+    }
+
+    public Marshaller getContext(Class<?> aClass) {
+        try {
+            JAXBContext context = contextResolver.getContext();
+            Marshaller marshaller = context.createMarshaller();
+            NamespacePrefixMapper namespaceMapper = new NamespacePrefixMapper() {
+
+                /**
+                 * Returns a preferred prefix for the given namespace URI.
+                 *
+                 * This method is intended to be overrided by a derived class.
+                 *
+                 * @param namespaceUri
+                 *      The namespace URI for which the prefix needs to be found.
+                 *      Never be null. "" is used to denote the default namespace.
+                 * @param suggestion
+                 *      When the content tree has a suggestion for the prefix
+                 *      to the given namespaceUri, that suggestion is passed as a
+                 *      parameter. Typically this value comes from QName.getPrefix()
+                 *      to show the preference of the content tree. This parameter
+                 *      may be null, and this parameter may represent an already
+                 *      occupied prefix.
+                 * @param requirePrefix
+                 *      If this method is expected to return non-empty prefix.
+                 *      When this flag is true, it means that the given namespace URI
+                 *      cannot be set as the default namespace.
+                 *
+                 * @return
+                 *      null if there's no preferred prefix for the namespace URI.
+                 *      In this case, the system will generate a prefix for you.
+                 *
+                 *      Otherwise the system will try to use the returned prefix,
+                 *      but generally there's no guarantee if the prefix will be
+                 *      actually used or not.
+                 *
+                 *      return "" to map this namespace URI to the default namespace.
+                 *      Again, there's no guarantee that this preference will be
+                 *      honored.
+                 *
+                 *      If this method returns "" when requirePrefix=true, the return
+                 *      value will be ignored and the system will generate one.
+                 */
+                @Override
+                public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
+                    if (namespaceUri.equals("http://camel.apache.org/schema/web")) {
+                        return "w";
+                    } else if (namespaceUri.equals("http://camel.apache.org/schema/spring")) {
+                        if (requirePrefix) {
+                            return "c";
+                        }
+                        return "";
+                    } else {
+                        return suggestion;
+                    }
+                }
+
+            };
+            marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", namespaceMapper);
+            return marshaller;
+        } catch (JAXBException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

Propchange: camel/trunk/components/camel-web/src/main/java/org/apache/camel/web/util/JAXBMarshallerResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native