HTTPCLIENT-1351: added method to HttpClientContext to obtain redirect locations

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@1486956 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2013-05-28 15:08:31 +00:00
parent 6fad24aef2
commit 26ee6689f3
7 changed files with 114 additions and 22 deletions

View File

@ -0,0 +1,51 @@
/*
* ====================================================================
* 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
* <http://www.apache.org/>.
*
*/
package org.apache.http.client;
import java.net.URI;
import java.util.List;
import java.util.Set;
/**
* This class represents an iterable collection of {@link java.net.URI} locations.
*
* @since 4.3
*/
public interface URICollection extends Iterable<URI> {
int getCount();
boolean isEmpty();
boolean contains(URI uri);
Set<URI> getUnique();
List<URI> getAll();
}

View File

@ -27,14 +27,13 @@
package org.apache.http.client.protocol;
import java.net.URI;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.auth.AuthState;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.URICollection;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.Lookup;
import org.apache.http.conn.routing.HttpRoute;
@ -56,18 +55,19 @@ import org.apache.http.protocol.HttpCoreContext;
@NotThreadSafe
public class HttpClientContext extends HttpCoreContext {
/**
* Attribute name of a {@link URI} object that represents
* request URI location (relative or absolute) of the last request target.
*/
public static final String HTTP_LOCATION = "http.location";
/**
* Attribute name of a {@link org.apache.http.conn.routing.RouteInfo}
* object that represents the actual connection route.
*/
public static final String HTTP_ROUTE = "http.route";
/**
* Attribute name of a {@link org.apache.http.client.URICollection} object that
* represents a collection of all redirect locations received in the process
* of request execution.
*/
public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
/**
* Attribute name of a {@link org.apache.http.config.Lookup} object that represents
* the actual {@link CookieSpecProvider} registry.
@ -160,14 +160,14 @@ public class HttpClientContext extends HttpCoreContext {
super();
}
public URI getHttpLocation() {
return getAttribute(HTTP_LOCATION, URI.class);
}
public RouteInfo getHttpRoute() {
return getAttribute(HTTP_ROUTE, HttpRoute.class);
}
public URICollection getRedirectLocations() {
return getAttribute(REDIRECT_LOCATIONS, URICollection.class);
}
public CookieStore getCookieStore() {
return getAttribute(COOKIE_STORE, CookieStore.class);
}

View File

@ -74,6 +74,10 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
private final Log log = LogFactory.getLog(getClass());
/**
* @deprecated (4.3) use {@link org.apache.http.client.protocol.HttpClientContext#REDIRECT_LOCATIONS}.
*/
@Deprecated
public static final String REDIRECT_LOCATIONS = "http.protocol.redirect-locations";
public static final DefaultRedirectStrategy INSTANCE = new DefaultRedirectStrategy();
@ -160,10 +164,10 @@ public class DefaultRedirectStrategy implements RedirectStrategy {
}
RedirectLocations redirectLocations = (RedirectLocations) clientContext.getAttribute(
REDIRECT_LOCATIONS);
HttpClientContext.REDIRECT_LOCATIONS);
if (redirectLocations == null) {
redirectLocations = new RedirectLocations();
context.setAttribute(REDIRECT_LOCATIONS, redirectLocations);
context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);
}
if (!config.isCircularRedirectsAllowed()) {
if (redirectLocations.contains(uri)) {

View File

@ -35,6 +35,7 @@ import java.util.List;
import java.util.Set;
import org.apache.http.annotation.NotThreadSafe;
import org.apache.http.client.URICollection;
/**
* This class represents a collection of {@link URI}s used as redirect locations.
@ -42,7 +43,7 @@ import org.apache.http.annotation.NotThreadSafe;
* @since 4.0
*/
@NotThreadSafe // HashSet is not synch.
public class RedirectLocations {
public class RedirectLocations implements URICollection {
private final Set<URI> unique;
private final List<URI> all;
@ -96,4 +97,32 @@ public class RedirectLocations {
return new ArrayList<URI>(this.all);
}
/**
* @since 4.3
*/
public Set<URI> getUnique() {
return new HashSet<URI>(this.unique);
}
/**
* @since 4.3
*/
public Iterator<URI> iterator() {
return getAll().iterator();
}
/**
* @since 4.3
*/
public int getCount() {
return this.all.size();
}
/**
* @since 4.3
*/
public boolean isEmpty() {
return this.all.isEmpty();
}
}

View File

@ -74,12 +74,10 @@ public class ProtocolExec implements ClientExecChain {
private void rewriteRequestURI(
final HttpRequestWrapper request,
final HttpRoute route,
final HttpClientContext context) throws ProtocolException {
final HttpRoute route) throws ProtocolException {
try {
URI uri = request.getURI();
if (uri != null) {
context.setAttribute(HttpClientContext.HTTP_LOCATION, uri);
if (route.getProxyHost() != null && !route.isTunnelled()) {
// Make sure the request URI is absolute
if (!uri.isAbsolute()) {
@ -129,7 +127,7 @@ public class ProtocolExec implements ClientExecChain {
request.setURI(uri);
// Re-write request URI if needed
rewriteRequestURI(request, route, context);
rewriteRequestURI(request, route);
final HttpParams params = request.getParams();
HttpHost virtualHost = (HttpHost) params.getParameter(ClientPNames.VIRTUAL_HOST);

View File

@ -258,9 +258,6 @@ public class TestClientRequestExecution extends IntegrationTestBase {
final HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
Assert.assertEquals("/stuff", request.getRequestLine().getUri());
final URI location = (URI) context.getAttribute(HttpClientContext.HTTP_LOCATION);
Assert.assertEquals(uri, location);
}
}

View File

@ -27,6 +27,7 @@
package org.apache.http.impl.client.integration;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import org.apache.http.Header;
@ -41,10 +42,12 @@ import org.apache.http.client.CircularRedirectException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.RedirectException;
import org.apache.http.client.URICollection;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.cookie.SM;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
@ -222,6 +225,9 @@ public class TestRedirects extends IntegrationTestBase {
Assert.assertEquals(HttpStatus.SC_MULTIPLE_CHOICES, response.getStatusLine().getStatusCode());
Assert.assertEquals("/oldlocation/", reqWrapper.getRequestLine().getUri());
URICollection redirects = context.getRedirectLocations();
Assert.assertNull(redirects);
}
@Test
@ -243,6 +249,13 @@ public class TestRedirects extends IntegrationTestBase {
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertEquals("/newlocation/", reqWrapper.getRequestLine().getUri());
Assert.assertEquals(target, host);
URICollection redirects = context.getRedirectLocations();
Assert.assertNotNull(redirects);
Assert.assertEquals(1, redirects.getCount());
URI redirect = URIUtils.rewriteURI(new URI("/newlocation/"), target);
Assert.assertTrue(redirects.contains(redirect));
}
@Test