Issue 581:BindAsHostPrefixIfConfigured generates wrong paths

This commit is contained in:
Adrian Cole 2011-05-29 20:08:35 -07:00
parent e9cf646a75
commit ca63b9cfa3
3 changed files with 94 additions and 10 deletions

View File

@ -28,15 +28,15 @@ import javax.inject.Singleton;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriBuilder;
import org.jclouds.s3.S3AsyncClient;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.utils.ModifyRequest;
import org.jclouds.rest.Binder;
import org.jclouds.rest.annotations.SkipEncoding;
import org.jclouds.rest.binders.BindAsHostPrefix;
import org.jclouds.s3.S3AsyncClient;
import org.jclouds.util.Strings2;
import com.google.common.collect.Maps;
import com.google.common.collect.ImmutableMap;
/**
*
@ -70,12 +70,15 @@ public class BindAsHostPrefixIfConfigured implements Binder {
UriBuilder builder = uriBuilderProvider.get().uri(request.getEndpoint());
StringBuilder path = new StringBuilder(Strings2.urlEncode(request.getEndpoint().getPath(), S3AsyncClient.class
.getAnnotation(SkipEncoding.class).value()));
int indexToInsert = path.indexOf(servicePath);
int indexToInsert = 0;
if (!servicePath.equals("/")) {
indexToInsert = path.indexOf(servicePath);
indexToInsert = indexToInsert == -1 ? 0 : indexToInsert;
indexToInsert += servicePath.length();
}
path.insert(indexToInsert, "/" + payload.toString());
builder.replacePath(path.toString());
return (R) request.toBuilder().endpoint(builder.buildFromEncodedMap(Maps.<String, Object> newLinkedHashMap()))
return (R) request.toBuilder().endpoint(builder.buildFromEncodedMap(ImmutableMap.<String, Object> of()))
.build();
}
}

View File

@ -0,0 +1,65 @@
/**
*
* Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed 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.jclouds.s3.binders;
import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Properties;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.jclouds.rest.internal.RestAnnotationProcessor;
import org.jclouds.s3.BaseS3AsyncClientTest;
import org.jclouds.s3.S3AsyncClient;
import org.testng.annotations.Test;
import com.google.inject.TypeLiteral;
/**
* Tests behavior of {@code BindAsHostPrefixIfConfigured}
*
* @author Adrian Cole
*/
// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
@Test(groups = "unit", testName = "BindAsHostPrefixIfConfiguredNoPathTest")
public class BindAsHostPrefixIfConfiguredNoPathTest extends BaseS3AsyncClientTest<S3AsyncClient> {
@Override
protected TypeLiteral<RestAnnotationProcessor<S3AsyncClient>> createTypeLiteral() {
return new TypeLiteral<RestAnnotationProcessor<S3AsyncClient>>() {
};
}
public void testBucketWithHostnameStyle() throws IOException, SecurityException, NoSuchMethodException {
Method method = S3AsyncClient.class.getMethod("deleteObject", String.class, String.class);
GeneratedHttpRequest<S3AsyncClient> request = processor.createRequest(method, "testbucket.example.com", "test.jpg");
assertRequestLineEquals(request, "DELETE https://s3.amazonaws.com/testbucket.example.com/test.jpg HTTP/1.1");
}
@Override
protected Properties getProperties() {
Properties properties = super.getProperties();
properties.setProperty(PROPERTY_S3_VIRTUAL_HOST_BUCKETS, "false");
return properties;
}
}

View File

@ -23,6 +23,7 @@ import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCK
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Properties;
@ -60,6 +61,20 @@ public class BindAsHostPrefixIfConfiguredTest extends BaseS3AsyncClientTest<S3As
}
public void testBucketWithHostnameStyle() throws IOException, SecurityException, NoSuchMethodException {
HttpRequest request = new HttpRequest("GET", URI.create("http://euc/services/Walrus"));
BindAsHostPrefixIfConfigured binder = injector.getInstance(BindAsHostPrefixIfConfigured.class);
request = binder.bindToRequest(request, "testbucket.example.com");
assertEquals(request.getRequestLine(), "GET http://euc/services/Walrus/testbucket.example.com HTTP/1.1");
Method method = S3AsyncClient.class.getMethod("deleteObject", String.class, String.class);
request = processor.createRequest(method, "testbucket.example.com", "test.jpg");
assertRequestLineEquals(request, "DELETE http://euc/services/Walrus/testbucket.example.com/test.jpg HTTP/1.1");
}
@Test(dataProvider = "objects")
public void testObject(String key) throws InterruptedException {
@ -80,6 +95,7 @@ public class BindAsHostPrefixIfConfiguredTest extends BaseS3AsyncClientTest<S3As
@Override
protected Properties getProperties() {
Properties properties = super.getProperties();
properties.setProperty("s3.endpoint", "http://euc/services/Walrus");
properties.setProperty(PROPERTY_S3_SERVICE_PATH, "/services/Walrus");
properties.setProperty(PROPERTY_S3_VIRTUAL_HOST_BUCKETS, "false");
return properties;