cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1059511 - in /cxf/trunk: rt/transports/http/src/main/java/org/apache/cxf/transport/http/ systests/transports/ systests/transports/src/test/java/org/apache/cxf/systest/http/auth/
Date Sun, 16 Jan 2011 08:15:19 GMT
Author: cschneider
Date: Sun Jan 16 08:15:18 2011
New Revision: 1059511

URL: http://svn.apache.org/viewvc?rev=1059511&view=rev
Log:
CXF-3249 Better handling of HTTP 401 responses and other HTTP error codes

Added:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPException.java
Modified:
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
    cxf/trunk/systests/transports/   (props changed)
    cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http/auth/DigestAuthTest.java

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java?rev=1059511&r1=1059510&r2=1059511&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPConduit.java
Sun Jan 16 08:15:18 2011
@@ -21,6 +21,7 @@ package org.apache.cxf.transport.http;
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -1493,11 +1494,13 @@ public class HTTPConduit 
             
             logResponseInfo(responseCode);
             
-            if (responseCode == HttpURLConnection.HTTP_NOT_FOUND
-                && !MessageUtils.isTrue(outMessage.getContextualProperty(
-                    "org.apache.cxf.http.no_io_exceptions"))) {
-                throw new IOException("HTTP response '" + responseCode + ": " 
-                    + connection.getResponseMessage() + "'");
+            // This property should be set in case the exceptions should not be handled here
+            // For example jax rs uses this
+            boolean noExceptions = MessageUtils.isTrue(outMessage.getContextualProperty(
+                "org.apache.cxf.http.no_io_exceptions"));
+            if (responseCode >= 400 && responseCode != 500 && !noExceptions)
{
+                throw new HTTPException(responseCode, connection.getResponseMessage(), 
+                                        connection.getURL());
             }
 
             InputStream in = null;
@@ -1550,8 +1553,10 @@ public class HTTPConduit 
                     in = connection.getInputStream();
                 }
             }
-            // if (in == null) : it's perfectly ok for non-soap http services
-            // have no response body : those interceptors which do need it will check anyway
       
+            if (in == null) {
+                // Create an empty stream to avoid NullPointerExceptions
+                in = new ByteArrayInputStream(new byte[] {});
+            }
             inMessage.setContent(InputStream.class, in);
             
             

Added: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPException.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPException.java?rev=1059511&view=auto
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPException.java
(added)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/http/HTTPException.java
Sun Jan 16 08:15:18 2011
@@ -0,0 +1,49 @@
+/**
+ * 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.transport.http;
+
+import java.io.IOException;
+import java.net.URL;
+
+public class HTTPException extends IOException {
+    private int responseCode;
+    private String responseMessage;
+    private URL url;
+    
+    public HTTPException(int responseCode, String responseMessage, URL url) {
+        super("HTTP response '" + responseCode + ": " 
+              + responseMessage + "' when communicating with " + url.toString());
+        this.responseCode = responseCode;
+        this.responseMessage = responseMessage;
+        this.url = url;
+    }
+
+    public int getResponseCode() {
+        return responseCode;
+    }
+
+    public String getResponseMessage() {
+        return responseMessage;
+    }
+
+    public URL getUrl() {
+        return url;
+    }
+ 
+}

Propchange: cxf/trunk/systests/transports/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sun Jan 16 08:15:18 2011
@@ -8,3 +8,5 @@ eclipse-classes
 .project
 .wtpmodules
 
+
+activemq-data

Modified: cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http/auth/DigestAuthTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http/auth/DigestAuthTest.java?rev=1059511&r1=1059510&r2=1059511&view=diff
==============================================================================
--- cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http/auth/DigestAuthTest.java
(original)
+++ cxf/trunk/systests/transports/src/test/java/org/apache/cxf/systest/http/auth/DigestAuthTest.java
Sun Jan 16 08:15:18 2011
@@ -23,13 +23,13 @@ package org.apache.cxf.systest.http.auth
 import java.net.URL;
 
 import javax.xml.namespace.QName;
-import javax.xml.ws.soap.SOAPFaultException;
 
 import org.apache.cxf.configuration.security.AuthorizationPolicy;
 import org.apache.cxf.endpoint.Client;
 import org.apache.cxf.frontend.ClientProxy;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
 import org.apache.cxf.transport.http.HTTPConduit;
+import org.apache.cxf.transport.http.HTTPException;
 import org.apache.hello_world.Greeter;
 import org.apache.hello_world.services.SOAPService;
 
@@ -97,13 +97,11 @@ public class DigestAuthTest extends Abst
         try {
             String answer = mortimer.sayHi();
             Assert.fail("Unexpected reply (" + answer + "). Should throw exception");
-        } catch (SOAPFaultException e) {
-            // TODO do we really expect Can't find input stream here. I rather would expect
-            // authorization failed with some infos
+        } catch (Exception e) {
             Throwable cause = e.getCause();
-            Assert.assertEquals(RuntimeException.class, cause.getClass());
-            RuntimeException rte = (RuntimeException)cause;
-            Assert.assertTrue(rte.getMessage().startsWith("Can't find input stream"));
+            Assert.assertEquals(HTTPException.class, cause.getClass());
+            HTTPException he = (HTTPException)cause;
+            Assert.assertEquals(401, he.getResponseCode());
         }
     }
 



Mime
View raw message