diff --git a/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java b/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java index f08c59e92a..00645901dd 100644 --- a/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java +++ b/apis/s3/src/main/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfigured.java @@ -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; /** * @@ -52,8 +52,8 @@ public class BindAsHostPrefixIfConfigured implements Binder { @Inject public BindAsHostPrefixIfConfigured(BindAsHostPrefix bindAsHostPrefix, - @Named(PROPERTY_S3_VIRTUAL_HOST_BUCKETS) boolean isVhostStyle, - @Named(PROPERTY_S3_SERVICE_PATH) String servicePath, Provider uriBuilderProvider) { + @Named(PROPERTY_S3_VIRTUAL_HOST_BUCKETS) boolean isVhostStyle, + @Named(PROPERTY_S3_SERVICE_PATH) String servicePath, Provider uriBuilderProvider) { this.bindAsHostPrefix = bindAsHostPrefix; this.isVhostStyle = isVhostStyle; this.servicePath = servicePath; @@ -69,14 +69,17 @@ public class BindAsHostPrefixIfConfigured implements Binder { } else { 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); - indexToInsert = indexToInsert == -1 ? 0 : indexToInsert; - indexToInsert += servicePath.length(); + .getAnnotation(SkipEncoding.class).value())); + 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. newLinkedHashMap())) - .build(); + return (R) request.toBuilder().endpoint(builder.buildFromEncodedMap(ImmutableMap. of())) + .build(); } } } diff --git a/apis/s3/src/test/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfiguredNoPathTest.java b/apis/s3/src/test/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfiguredNoPathTest.java new file mode 100644 index 0000000000..f39734d4bc --- /dev/null +++ b/apis/s3/src/test/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfiguredNoPathTest.java @@ -0,0 +1,65 @@ +/** + * + * Copyright (C) 2011 Cloud Conscious, LLC. + * + * ==================================================================== + * 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 { + + @Override + protected TypeLiteral> createTypeLiteral() { + return new TypeLiteral>() { + }; + } + + public void testBucketWithHostnameStyle() throws IOException, SecurityException, NoSuchMethodException { + + Method method = S3AsyncClient.class.getMethod("deleteObject", String.class, String.class); + GeneratedHttpRequest 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; + } + +} diff --git a/apis/s3/src/test/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfiguredTest.java b/apis/s3/src/test/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfiguredTest.java index 8751ccb821..1c13e7c5f6 100644 --- a/apis/s3/src/test/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfiguredTest.java +++ b/apis/s3/src/test/java/org/jclouds/s3/binders/BindAsHostPrefixIfConfiguredTest.java @@ -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