Return-Path: X-Original-To: apmail-hc-commits-archive@www.apache.org Delivered-To: apmail-hc-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8FCC0F3E7 for ; Mon, 26 May 2014 10:31:43 +0000 (UTC) Received: (qmail 18847 invoked by uid 500); 26 May 2014 10:31:43 -0000 Delivered-To: apmail-hc-commits-archive@hc.apache.org Received: (qmail 18802 invoked by uid 500); 26 May 2014 10:31:43 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 18795 invoked by uid 99); 26 May 2014 10:31:43 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 26 May 2014 10:31:43 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.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; Mon, 26 May 2014 10:31:42 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 8B361238890B for ; Mon, 26 May 2014 10:31:21 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1597552 - in /httpcomponents/httpclient/trunk: fluent-hc/src/main/java/org/apache/http/client/fluent/ httpclient/src/main/java/org/apache/http/impl/client/ httpclient/src/test/java/org/apache/http/impl/client/ Date: Mon, 26 May 2014 10:31:21 -0000 To: commits@hc.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140526103121.8B361238890B@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Mon May 26 10:31:20 2014 New Revision: 1597552 URL: http://svn.apache.org/r1597552 Log: HTTPCLIENT-1511: Added abstract response handler Contributed by Jochen Kemnade Added: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractResponseHandler.java - copied, changed from r1597551, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractResponseHandler.java (with props) Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java Modified: httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java?rev=1597552&r1=1597551&r2=1597552&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java (original) +++ httpcomponents/httpclient/trunk/fluent-hc/src/main/java/org/apache/http/client/fluent/Content.java Mon May 26 10:31:20 2014 @@ -62,6 +62,9 @@ public class Content { return asString(charset); } + /** + * @since 4.4 + */ public String asString(final Charset charset) { return new String(this.raw, charset); } Copied: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractResponseHandler.java (from r1597551, httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java) URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractResponseHandler.java?p2=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractResponseHandler.java&p1=httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java&r1=1597551&r2=1597552&rev=1597552&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/AbstractResponseHandler.java Mon May 26 10:31:20 2014 @@ -38,7 +38,7 @@ import org.apache.http.client.ResponseHa import org.apache.http.util.EntityUtils; /** - * A {@link ResponseHandler} that returns the response body as a String + * A generic {@link ResponseHandler} that works with the response entity * for successful (2xx) responses. If the response code was >= 300, the response * body is consumed and an {@link HttpResponseException} is thrown. *

@@ -47,19 +47,19 @@ import org.apache.http.util.EntityUtils; * org.apache.http.client.methods.HttpUriRequest, ResponseHandler)}, * HttpClient may handle redirects (3xx responses) internally. * - * @since 4.0 + * @since 4.4 */ @Immutable -public class BasicResponseHandler implements ResponseHandler { +public abstract class AbstractResponseHandler implements ResponseHandler { /** - * Returns the response body as a String if the response was successful (a - * 2xx status code). If no response body exists, this returns null. If the - * response was unsuccessful (>= 300 status code), throws an - * {@link HttpResponseException}. + * Read the entity from the response body and pass it to the entity handler + * method if the response was successful (a 2xx status code). If no response + * body exists, this returns null. If the response was unsuccessful (>= 300 + * status code), throws an {@link HttpResponseException}. */ @Override - public String handleResponse(final HttpResponse response) + public final T handleResponse(final HttpResponse response) throws HttpResponseException, IOException { final StatusLine statusLine = response.getStatusLine(); final HttpEntity entity = response.getEntity(); @@ -68,7 +68,13 @@ public class BasicResponseHandler implem throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } - return entity == null ? null : EntityUtils.toString(entity); + return entity == null ? null : handleEntity(entity); } + /** + * Handle the response entity and transform it into the actual response + * object. + */ + public abstract T handleEntity(HttpEntity entity) throws IOException; + } Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java?rev=1597552&r1=1597551&r2=1597552&view=diff ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java (original) +++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/BasicResponseHandler.java Mon May 26 10:31:20 2014 @@ -30,11 +30,7 @@ package org.apache.http.impl.client; import java.io.IOException; import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.StatusLine; import org.apache.http.annotation.Immutable; -import org.apache.http.client.HttpResponseException; -import org.apache.http.client.ResponseHandler; import org.apache.http.util.EntityUtils; /** @@ -50,25 +46,14 @@ import org.apache.http.util.EntityUtils; * @since 4.0 */ @Immutable -public class BasicResponseHandler implements ResponseHandler { +public class BasicResponseHandler extends AbstractResponseHandler { /** - * Returns the response body as a String if the response was successful (a - * 2xx status code). If no response body exists, this returns null. If the - * response was unsuccessful (>= 300 status code), throws an - * {@link HttpResponseException}. + * Returns the entity as a body as a String. */ @Override - public String handleResponse(final HttpResponse response) - throws HttpResponseException, IOException { - final StatusLine statusLine = response.getStatusLine(); - final HttpEntity entity = response.getEntity(); - if (statusLine.getStatusCode() >= 300) { - EntityUtils.consume(entity); - throw new HttpResponseException(statusLine.getStatusCode(), - statusLine.getReasonPhrase()); - } - return entity == null ? null : EntityUtils.toString(entity); + public String handleEntity(final HttpEntity entity) throws IOException { + return EntityUtils.toString(entity); } } Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractResponseHandler.java URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractResponseHandler.java?rev=1597552&view=auto ============================================================================== --- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractResponseHandler.java (added) +++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractResponseHandler.java Mon May 26 10:31:20 2014 @@ -0,0 +1,93 @@ +/* + * ==================================================================== + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.impl.client; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.HttpVersion; +import org.apache.http.StatusLine; +import org.apache.http.client.HttpResponseException; +import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicStatusLine; +import org.apache.http.util.EntityUtils; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +/** + * Unit tests for {@link BasicResponseHandler}. + */ +public class TestAbstractResponseHandler { + + @Test + public void testSuccessfulResponse() throws Exception { + final StatusLine sl = new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"); + final HttpResponse response = Mockito.mock(HttpResponse.class); + final HttpEntity entity = new StringEntity("42"); + Mockito.when(response.getStatusLine()).thenReturn(sl); + Mockito.when(response.getEntity()).thenReturn(entity); + + final AbstractResponseHandler handler = new AbstractResponseHandler() { + + @Override + public Integer handleEntity(final HttpEntity entity) throws IOException { + return Integer.valueOf(EntityUtils.toString(entity)); + } + }; + final Integer number = handler.handleResponse(response); + Assert.assertEquals(42, number.intValue()); + } + + @SuppressWarnings("boxing") + @Test + public void testUnsuccessfulResponse() throws Exception { + final InputStream instream = Mockito.mock(InputStream.class); + final HttpEntity entity = Mockito.mock(HttpEntity.class); + Mockito.when(entity.isStreaming()).thenReturn(true); + Mockito.when(entity.getContent()).thenReturn(instream); + final StatusLine sl = new BasicStatusLine(HttpVersion.HTTP_1_1, 404, "Not Found"); + final HttpResponse response = Mockito.mock(HttpResponse.class); + Mockito.when(response.getStatusLine()).thenReturn(sl); + Mockito.when(response.getEntity()).thenReturn(entity); + + final BasicResponseHandler handler = new BasicResponseHandler(); + try { + handler.handleResponse(response); + Assert.fail("HttpResponseException expected"); + } catch (final HttpResponseException ex) { + Assert.assertEquals(404, ex.getStatusCode()); + Assert.assertEquals("Not Found", ex.getMessage()); + } + Mockito.verify(entity).getContent(); + Mockito.verify(instream).close(); + } + +} Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractResponseHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractResponseHandler.java ------------------------------------------------------------------------------ svn:keywords = Date Revision Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAbstractResponseHandler.java ------------------------------------------------------------------------------ svn:mime-type = text/plain