You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by dk...@apache.org on 2013/05/31 23:05:07 UTC

svn commit: r1488399 - in /cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs: ./ src/main/java/org/apache/cxf/jaxrs/ src/main/java/org/apache/cxf/jaxrs/interceptor/

Author: dkulp
Date: Fri May 31 21:05:06 2013
New Revision: 1488399

URL: http://svn.apache.org/r1488399
Log:
Make jaxrs independent of xml binding

Added:
    cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBinding.java
    cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXBDefaultFaultOutInterceptor.java
Modified:
    cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/pom.xml
    cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBindingFactory.java

Modified: cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/pom.xml
URL: http://svn.apache.org/viewvc/cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/pom.xml?rev=1488399&r1=1488398&r2=1488399&view=diff
==============================================================================
--- cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/pom.xml (original)
+++ cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/pom.xml Fri May 31 21:05:06 2013
@@ -87,17 +87,6 @@
                 <groupId>javax.annotation</groupId>
                 <artifactId>javax.annotation-api</artifactId>
         </dependency>
-        <dependency>
-            <groupId>${project.groupId}</groupId>
-            <artifactId>cxf-rt-bindings-xml</artifactId>
-            <version>${project.version}</version>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.apache.cxf</groupId>
-                    <artifactId>cxf-rt-databinding-jaxb</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
 
         <dependency>
             <groupId>org.apache.cxf</groupId>

Added: cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBinding.java
URL: http://svn.apache.org/viewvc/cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBinding.java?rev=1488399&view=auto
==============================================================================
--- cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBinding.java (added)
+++ cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBinding.java Fri May 31 21:05:06 2013
@@ -0,0 +1,62 @@
+/**
+ * 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.jaxrs;
+
+import org.apache.cxf.binding.Binding;
+import org.apache.cxf.interceptor.AbstractBasicInterceptorProvider;
+import org.apache.cxf.message.Exchange;
+import org.apache.cxf.message.MessageImpl;
+import org.apache.cxf.service.model.BindingInfo;
+
+public class JAXRSBinding extends AbstractBasicInterceptorProvider implements Binding {
+    
+    private BindingInfo bindingInfo;
+    
+    public JAXRSBinding(BindingInfo bindingInfo) {
+        super();
+        this.bindingInfo = bindingInfo;
+    }
+
+    public BindingInfo getBindingInfo() {
+        return bindingInfo;
+    }
+
+    public org.apache.cxf.message.Message createMessage() {
+        return createMessage(new MessageImpl());
+    }
+
+    public org.apache.cxf.message.Message createMessage(org.apache.cxf.message.Message m) {
+        if (!m.containsKey(org.apache.cxf.message.Message.CONTENT_TYPE)) {
+            
+            String ct = null;
+            
+            // Should this be done in ServiceInvokerInterceptor to support a case where the 
+            // response content type is detected early on the inbound chain for all the bindings ?
+            Exchange exchange = m.getExchange();
+            if (exchange != null) {
+                ct = (String)exchange.get(org.apache.cxf.message.Message.CONTENT_TYPE);
+            }
+            if (ct == null) {
+                ct = "text/xml";
+            }
+            m.put(org.apache.cxf.message.Message.CONTENT_TYPE, ct);
+        }
+        return m;
+    }
+}
\ No newline at end of file

Modified: cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBindingFactory.java
URL: http://svn.apache.org/viewvc/cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBindingFactory.java?rev=1488399&r1=1488398&r2=1488399&view=diff
==============================================================================
--- cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBindingFactory.java (original)
+++ cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSBindingFactory.java Fri May 31 21:05:06 2013
@@ -26,13 +26,12 @@ import javax.xml.namespace.QName;
 import org.apache.cxf.Bus;
 import org.apache.cxf.binding.AbstractBindingFactory;
 import org.apache.cxf.binding.Binding;
-import org.apache.cxf.binding.xml.XMLBinding;
-import org.apache.cxf.binding.xml.interceptor.XMLFaultOutInterceptor;
 import org.apache.cxf.common.i18n.Message;
 import org.apache.cxf.common.injection.NoJSR250Annotations;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.endpoint.Endpoint;
 import org.apache.cxf.interceptor.StaxOutInterceptor;
+import org.apache.cxf.jaxrs.interceptor.JAXBDefaultFaultOutInterceptor;
 import org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor;
 import org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor;
 import org.apache.cxf.service.Service;
@@ -58,17 +57,19 @@ public class JAXRSBindingFactory extends
     }
 
     public Binding createBinding(BindingInfo bi) {
-        XMLBinding binding = new XMLBinding(bi);
+        JAXRSBinding binding = new JAXRSBinding(bi);
 
         binding.getInInterceptors().add(new JAXRSInInterceptor());
         
         binding.getOutInterceptors().add(new JAXRSOutInterceptor());
         
-        binding.getOutFaultInterceptors().add(new XMLFaultOutInterceptor());
+        binding.getOutFaultInterceptors().add(new JAXBDefaultFaultOutInterceptor());
         binding.getOutFaultInterceptors().add(new StaxOutInterceptor());
 
         return binding;
     }
+    
+    
 
     /*
      * The concept of Binding can not be applied to JAX-RS. Here we use

Added: cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXBDefaultFaultOutInterceptor.java
URL: http://svn.apache.org/viewvc/cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXBDefaultFaultOutInterceptor.java?rev=1488399&view=auto
==============================================================================
--- cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXBDefaultFaultOutInterceptor.java (added)
+++ cxf/branches/dkulp-nowsdl4j/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/interceptor/JAXBDefaultFaultOutInterceptor.java Fri May 31 21:05:06 2013
@@ -0,0 +1,97 @@
+/**
+ * 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.jaxrs.interceptor;
+
+import java.util.ResourceBundle;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.w3c.dom.Node;
+
+import org.apache.cxf.common.i18n.BundleUtils;
+import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.helpers.NSStack;
+import org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor;
+import org.apache.cxf.interceptor.Fault;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.Phase;
+import org.apache.cxf.staxutils.StaxUtils;
+
+public class JAXBDefaultFaultOutInterceptor extends AbstractOutDatabindingInterceptor {
+
+    private static final ResourceBundle BUNDLE = BundleUtils.getBundle(JAXBDefaultFaultOutInterceptor.class);
+
+    public JAXBDefaultFaultOutInterceptor() {
+        super(Phase.MARSHAL);
+    }
+    public JAXBDefaultFaultOutInterceptor(String phase) {
+        super(phase);
+    }
+
+    public void handleMessage(Message message) throws Fault {
+        
+        if (mustPropogateException(message)) {
+            throw (Fault) message.getContent(Exception.class);
+        }
+        
+        Fault f = (Fault) message.getContent(Exception.class);
+        message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
+        NSStack nsStack = new NSStack();
+        nsStack.push();
+
+        XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
+        try {
+            nsStack.add("http://cxf.apache.org/bindings/xformat");
+            String prefix = nsStack.getPrefix("http://cxf.apache.org/bindings/xformat");
+            StaxUtils.writeStartElement(writer, prefix, "XMLFault", 
+                                        "http://cxf.apache.org/bindings/xformat");
+            StaxUtils.writeStartElement(writer, prefix, "faultstring", 
+                                        "http://cxf.apache.org/bindings/xformat");
+            Throwable t = f.getCause();
+            writer.writeCharacters(t == null ? f.getMessage() : t.toString());
+            // fault string
+            writer.writeEndElement();
+            // call StaxUtils to write Fault detail.
+            
+            if (f.getDetail() != null) {
+                StaxUtils.writeStartElement(writer, prefix, "detail", "http://cxf.apache.org/bindings/xformat");
+                StaxUtils.writeNode(DOMUtils.getChild(f.getDetail(), Node.ELEMENT_NODE), 
+                                    writer, false);
+                writer.writeEndElement();
+            }
+            // fault root
+            writer.writeEndElement();
+            writer.flush();
+        } catch (XMLStreamException xe) {
+            throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
+        }
+    }
+    
+    @Override
+    public void handleFault(Message message) throws Fault {
+        if (mustPropogateException(message)) {
+            throw (Fault) message.getContent(Exception.class);
+        }
+    }
+    
+    protected boolean mustPropogateException(Message m) {
+        return Boolean.TRUE.equals(m.getExchange().get(Message.PROPOGATE_EXCEPTION));
+    }
+}