Return-Path: Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: (qmail 76129 invoked from network); 16 Jan 2011 08:15:54 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 16 Jan 2011 08:15:54 -0000 Received: (qmail 66683 invoked by uid 500); 16 Jan 2011 08:15:54 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 66560 invoked by uid 500); 16 Jan 2011 08:15:51 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 66553 invoked by uid 99); 16 Jan 2011 08:15:50 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 16 Jan 2011 08:15:50 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 16 Jan 2011 08:15:48 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 25A9C2388A40; Sun, 16 Jan 2011 08:15:19 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@cxf.apache.org From: cschneider@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110116081519.25A9C2388A40@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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()); } }