You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2014/06/16 19:09:04 UTC

svn commit: r1602929 - in /jena/trunk/jena-arq/src/main/java/org/apache/jena: atlas/web/HttpException.java atlas/web/auth/FormsAuthenticator.java riot/web/HttpOp.java

Author: andy
Date: Mon Jun 16 17:09:04 2014
New Revision: 1602929

URL: http://svn.apache.org/r1602929
Log:
JENA-715 : put error response body in the error exception.

Modified:
    jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/HttpException.java
    jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/FormsAuthenticator.java
    jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java

Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/HttpException.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/HttpException.java?rev=1602929&r1=1602928&r2=1602929&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/HttpException.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/HttpException.java Mon Jun 16 17:09:04 2014
@@ -26,12 +26,15 @@ public class HttpException extends Runti
     private static final long serialVersionUID = -7224224620679594095L;
     private int responseCode = -1;
     private String statusLine = null ;
+	private String response;
+
+	public HttpException(int responseCode, String statusLine, String response) {
+		super(responseCode + " - " + statusLine);
+		this.responseCode = responseCode;
+		this.statusLine = statusLine ;
+		this.response = response;
+	}
 
-    public HttpException(int responseCode, String statusLine) {
-        super(responseCode + " - " + statusLine);
-        this.responseCode = responseCode;
-        this.statusLine = statusLine ;
-    }
     
     public HttpException(String message) {
         super(message);
@@ -61,4 +64,11 @@ public class HttpException extends Runti
         return this.statusLine;
     }
 
+	/**
+	 * The response payload from the remote.
+	 * @return The payload, or null if no payload
+	 */
+	public String getResponse() {
+		return response;
+	}
 }

Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/FormsAuthenticator.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/FormsAuthenticator.java?rev=1602929&r1=1602928&r2=1602929&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/FormsAuthenticator.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/atlas/web/auth/FormsAuthenticator.java Mon Jun 16 17:09:04 2014
@@ -31,8 +31,8 @@ import org.apache.http.client.methods.Ht
 import org.apache.http.impl.client.AbstractHttpClient;
 import org.apache.http.impl.client.BasicCookieStore;
 import org.apache.http.protocol.HttpContext;
-import org.apache.http.util.EntityUtils;
 import org.apache.jena.atlas.web.HttpException;
+import org.apache.jena.riot.web.HttpOp;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -132,12 +132,15 @@ public class FormsAuthenticator extends 
                 post.setEntity(login.getLoginEntity());
                 HttpResponse response = client.execute(post, httpContext);
 
-                // Check for successful login
+				// Always read the payload to ensure reusable connections
+				final String payload = HttpOp.readPayload(response.getEntity());
+
+				// Check for successful login
                 if (response.getStatusLine().getStatusCode() >= 400) {
                     LOG.warn("Failed to login against " + login.getLoginFormURL() + " to obtain authentication for access to "
                             + target.toString());
                     throw new HttpException(response.getStatusLine().getStatusCode(), "Login attempt failed - "
-                            + response.getStatusLine().getReasonPhrase());
+                            + response.getStatusLine().getReasonPhrase(), payload);
                 }
 
                 // Otherwise assume a successful login
@@ -145,8 +148,6 @@ public class FormsAuthenticator extends 
                         + " and obtained authentication for access to " + target.toString());
                 login.setCookies(client.getCookieStore());
 
-                // Consume the response to free up the connection
-                EntityUtils.consumeQuietly(response.getEntity());
             } catch (UnsupportedEncodingException e) {
                 throw new HttpException("UTF-8 encoding not supported on your platform", e);
             } catch (IOException e) {

Modified: jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java
URL: http://svn.apache.org/viewvc/jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java?rev=1602929&r1=1602928&r2=1602929&view=diff
==============================================================================
--- jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java (original)
+++ jena/trunk/jena-arq/src/main/java/org/apache/jena/riot/web/HttpOp.java Mon Jun 16 17:09:04 2014
@@ -39,6 +39,7 @@ import org.apache.http.client.HttpClient
 import org.apache.http.client.entity.UrlEncodedFormEntity;
 import org.apache.http.client.methods.*;
 import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.entity.ContentType;
 import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.AbstractHttpClient;
@@ -1115,8 +1116,8 @@ public class HttpOp {
                 log.debug(format("[%d] %s %s", id, statusLine.getStatusCode(), statusLine.getReasonPhrase()));
                 // Error responses can have bodies so it is important to clear
                 // up.
-                EntityUtils.consume(response.getEntity());
-                throw new HttpException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
+				final String contentPayload = readPayload(response.getEntity());
+				throw new HttpException(statusLine.getStatusCode(), statusLine.getReasonPhrase(), contentPayload);
             }
             // Redirects are followed by HttpClient.
             if (handler != null)
@@ -1126,6 +1127,14 @@ public class HttpOp {
         }
     }
 
+
+	public static String readPayload(HttpEntity entity) throws IOException {
+		if (entity == null) {
+			return null;
+		}
+		return EntityUtils.toString(entity, ContentType.getOrDefault(entity).getCharset());
+	}
+
     /**
      * Ensures that a HTTP Client is non-null
      * <p>