mirror of https://github.com/apache/jclouds.git
Issue 177: support scheme-only redirects
This commit is contained in:
parent
55c8ce03ad
commit
a72efaea6d
|
@ -66,7 +66,8 @@ public class AWSRedirectionRetryHandler extends RedirectionRetryHandler {
|
|||
// http://developer.amazonwebservices.com/connect/thread.jspa?messageID=72287𑩟
|
||||
return backoffHandler.shouldRetryRequest(command, response);
|
||||
} else {
|
||||
command.changeHostAndPortTo(host, command.getRequest().getEndpoint().getPort());
|
||||
command.changeSchemeHostAndPortTo(command.getRequest().getEndpoint()
|
||||
.getScheme(), host, command.getRequest().getEndpoint().getPort());
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
|
|
|
@ -94,7 +94,7 @@ public class S3UtilsTest {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void changeHostAndPortTo(String host, int port) {
|
||||
public void changeSchemeHostAndPortTo(String scheme, String host, int port) {
|
||||
}
|
||||
|
||||
public void changeToGETRequest() {
|
||||
|
|
|
@ -441,7 +441,7 @@ public class StubAsyncBlobStore extends BaseAsyncBlobStore {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void changeHostAndPortTo(String host, int port) {
|
||||
public void changeSchemeHostAndPortTo(String scheme, String host, int port) {
|
||||
}
|
||||
|
||||
public void changeToGETRequest() {
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
*/
|
||||
package org.jclouds.http;
|
||||
|
||||
|
||||
/**
|
||||
* Command whose endpoint is an http service.
|
||||
*
|
||||
|
@ -48,8 +47,10 @@ public interface HttpCommand {
|
|||
|
||||
/**
|
||||
* change the destination of the current http command. typically used in handling redirects.
|
||||
*
|
||||
* @param string
|
||||
*/
|
||||
void changeHostAndPortTo(String host, int port);
|
||||
void changeSchemeHostAndPortTo(String scheme, String host, int port);
|
||||
|
||||
/**
|
||||
* change method from GET to HEAD. typically used in handling redirects.
|
||||
|
|
|
@ -84,8 +84,9 @@ public class TransformingHttpCommandImpl<T> implements TransformingHttpCommand<T
|
|||
* This also removes the Host header in order to avoid ssl problems.
|
||||
*/
|
||||
@Override
|
||||
public void changeHostAndPortTo(String host, int port) {
|
||||
public void changeSchemeHostAndPortTo(String scheme, String host, int port) {
|
||||
UriBuilder builder = UriBuilder.fromUri(request.getEndpoint());
|
||||
builder.scheme(scheme);
|
||||
builder.host(host);
|
||||
builder.port(port);
|
||||
request.setEndpoint(builder.build());
|
||||
|
|
|
@ -64,7 +64,8 @@ public class RedirectionRetryHandler implements HttpRetryHandler {
|
|||
String hostHeader = response.getFirstHeaderOrNull(HttpHeaders.LOCATION);
|
||||
if (hostHeader != null && command.incrementRedirectCount() < retryCountLimit) {
|
||||
URI redirectionUrl = UriBuilder.fromUri(hostHeader).build();
|
||||
if (redirectionUrl.getHost().equals(command.getRequest().getEndpoint().getHost())
|
||||
if (redirectionUrl.getScheme().equals(command.getRequest().getEndpoint().getScheme())
|
||||
&& redirectionUrl.getHost().equals(command.getRequest().getEndpoint().getHost())
|
||||
&& redirectionUrl.getPort() == command.getRequest().getEndpoint().getPort()) {
|
||||
if (!redirectionUrl.getPath().equals(command.getRequest().getEndpoint().getPath())) {
|
||||
command.changePathTo(redirectionUrl.getPath());
|
||||
|
@ -72,7 +73,8 @@ public class RedirectionRetryHandler implements HttpRetryHandler {
|
|||
return backoffHandler.shouldRetryRequest(command, response);
|
||||
}
|
||||
} else {
|
||||
command.changeHostAndPortTo(redirectionUrl.getHost(), redirectionUrl.getPort());
|
||||
command.changeSchemeHostAndPortTo(redirectionUrl.getScheme(), redirectionUrl.getHost(),
|
||||
redirectionUrl.getPort());
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
|
|
|
@ -18,17 +18,46 @@
|
|||
*/
|
||||
package org.jclouds.http;
|
||||
|
||||
import static org.easymock.EasyMock.expect;
|
||||
import static org.easymock.classextension.EasyMock.createMock;
|
||||
import static org.easymock.classextension.EasyMock.replay;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.ext.RuntimeDelegate;
|
||||
|
||||
import org.jclouds.rest.internal.GeneratedHttpRequest;
|
||||
import org.jclouds.rest.internal.RuntimeDelegateImpl;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Adrian Cole
|
||||
*/
|
||||
public class TransformingHttpCommandImplTest {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testToString() {
|
||||
// TODO
|
||||
public void testChangeSchemeHostAndPortTo() {
|
||||
RuntimeDelegate.setInstance(new RuntimeDelegateImpl());
|
||||
|
||||
GeneratedHttpRequest<?> request = createMock(GeneratedHttpRequest.class);
|
||||
TransformingHttpCommandImpl<?> command = new TransformingHttpCommandImpl(null, request,
|
||||
null);
|
||||
expect(request.getEndpoint()).andReturn(URI.create("http://localhost/mypath"));
|
||||
request.setEndpoint(URI.create("https://remotehost:443/mypath"));
|
||||
Multimap<String, String> headers = HashMultimap.create();
|
||||
expect(request.getHeaders()).andReturn(headers);
|
||||
replay(request);
|
||||
command.changeSchemeHostAndPortTo("https", "remotehost", 443);
|
||||
assertEquals(headers.get(HttpHeaders.HOST), Collections.singletonList("remotehost"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -160,7 +160,8 @@ public class GaeHttpCommandExecutorService extends BaseHttpCommandExecutorServic
|
|||
HttpRequest request = command.getRequest();
|
||||
String hostHeader = request.getFirstHeaderOrNull(HttpHeaders.HOST);
|
||||
if (hostHeader != null) {
|
||||
command.changeHostAndPortTo(hostHeader, request.getEndpoint().getPort());
|
||||
command.changeSchemeHostAndPortTo(request.getEndpoint().getScheme(), hostHeader, request
|
||||
.getEndpoint().getPort());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue