Issue 1092: fix hostname when path-based buckets are used in non-default location
diff --git a/apis/s3/src/main/java/org/jclouds/s3/S3AsyncClient.java b/apis/s3/src/main/java/org/jclouds/s3/S3AsyncClient.java
index 749d28c..724f1e2 100644
--- a/apis/s3/src/main/java/org/jclouds/s3/S3AsyncClient.java
+++ b/apis/s3/src/main/java/org/jclouds/s3/S3AsyncClient.java
@@ -99,7 +99,7 @@
  * All commands return a ListenableFuture of the result from S3. Any exceptions incurred during
  * processing will be backend in an {@link ExecutionException} as documented in
  * {@link ListenableFuture#get()}.
- * 
+ *
  * @author Adrian Cole
  * @author James Murty
  * @see S3Client
@@ -187,7 +187,6 @@
     */
    @DELETE
    @Path("/")
-   @Endpoint(Bucket.class)
    @ExceptionParser(ReturnTrueOn404OrNotFoundFalseOnIllegalState.class)
    ListenableFuture<Boolean> deleteBucketIfEmpty(
             @Bucket @EndpointParam(parser = DefaultEndpointThenInvalidateRegion.class) @BinderParam(BindAsHostPrefixIfConfigured.class) @ParamValidators(BucketNameValidator.class) String bucketName);
@@ -197,12 +196,11 @@
     */
    @HEAD
    @Path("/")
-   @Endpoint(Bucket.class)
    @QueryParams(keys = "max-keys", values = "0")
    @ExceptionParser(ReturnFalseOnContainerNotFound.class)
    ListenableFuture<Boolean> bucketExists(
-            @Bucket @BinderParam(BindAsHostPrefixIfConfigured.class) @ParamValidators(BucketNameValidator.class) String bucketName);
-   
+            @Bucket @EndpointParam(parser = AssignCorrectHostnameForBucket.class) @BinderParam(BindAsHostPrefixIfConfigured.class) @ParamValidators(BucketNameValidator.class) String bucketName);
+
 
    /**
     * @see S3Client#getBucketLocation
@@ -213,7 +211,7 @@
    @Endpoint(Bucket.class)
    @XMLResponseParser(LocationConstraintHandler.class)
    ListenableFuture<String> getBucketLocation(
-            @Bucket @BinderParam(BindAsHostPrefixIfConfigured.class) @ParamValidators(BucketNameValidator.class) String bucketName);   
+            @Bucket @BinderParam(BindAsHostPrefixIfConfigured.class) @ParamValidators(BucketNameValidator.class) String bucketName);
 
    /**
     * @see S3Client#getBucketPayer
diff --git a/apis/s3/src/main/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegion.java b/apis/s3/src/main/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegion.java
index 680f165..d7eaffa 100644
--- a/apis/s3/src/main/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegion.java
+++ b/apis/s3/src/main/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegion.java
@@ -24,7 +24,6 @@
 import javax.inject.Singleton;
 
 import org.jclouds.javax.annotation.Nullable;
-import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
 import org.jclouds.s3.Bucket;
 
 import com.google.common.base.Function;
@@ -32,17 +31,17 @@
 import com.google.common.cache.LoadingCache;
 
 /**
- * 
+ *
  * @author Adrian Cole
  */
 @Singleton
 public class DefaultEndpointThenInvalidateRegion implements Function<Object, URI> {
 
    private final LoadingCache<String, Optional<String>> bucketToRegionCache;
-   private final RegionToEndpointOrProviderIfNull r2;
+   private final AssignCorrectHostnameForBucket r2;
 
    @Inject
-   public DefaultEndpointThenInvalidateRegion(RegionToEndpointOrProviderIfNull r2,
+   public DefaultEndpointThenInvalidateRegion(AssignCorrectHostnameForBucket r2,
             @Bucket LoadingCache<String, Optional<String>> bucketToRegionCache) {
       this.r2 = r2;
       this.bucketToRegionCache = bucketToRegionCache;
@@ -51,7 +50,7 @@
    @Override
    public URI apply(@Nullable Object from) {
       try {
-         return r2.apply(null);
+         return r2.apply(from);
       } finally {
          bucketToRegionCache.invalidate(from.toString());
       }
diff --git a/apis/s3/src/test/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegionTest.java b/apis/s3/src/test/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegionTest.java
index 514ee8c..c8c8fd5 100644
--- a/apis/s3/src/test/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegionTest.java
+++ b/apis/s3/src/test/java/org/jclouds/s3/functions/DefaultEndpointThenInvalidateRegionTest.java
@@ -25,7 +25,6 @@
 
 import java.net.URI;
 
-import org.jclouds.location.functions.RegionToEndpointOrProviderIfNull;
 import org.testng.annotations.Test;
 
 import com.google.common.base.Optional;
@@ -40,10 +39,10 @@
    @SuppressWarnings("unchecked")
    @Test
    void testInvalidate() throws Exception {
-      RegionToEndpointOrProviderIfNull r2 = createMock(RegionToEndpointOrProviderIfNull.class);
+      AssignCorrectHostnameForBucket r2 = createMock(AssignCorrectHostnameForBucket.class);
       LoadingCache<String, Optional<String>> bucketToRegionCache = createMock(LoadingCache.class);
 
-      expect(r2.apply(null)).andReturn(URI.create("http://east-url"));
+      expect(r2.apply("mybucket")).andReturn(URI.create("http://east-url"));
       bucketToRegionCache.invalidate("mybucket");
 
       replay(r2, bucketToRegionCache);
diff --git a/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseServiceIntegrationTest.java b/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseServiceIntegrationTest.java
index 3e61b5f..9fbfc2f 100644
--- a/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseServiceIntegrationTest.java
+++ b/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseServiceIntegrationTest.java
@@ -19,6 +19,7 @@
 package org.jclouds.blobstore.integration.internal;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
 
 import java.util.Set;
 
@@ -34,7 +35,7 @@
 import com.google.common.collect.Iterables;
 
 /**
- * 
+ *
  * @author Adrian Cole
  */
 public class BaseServiceIntegrationTest extends BaseBlobStoreIntegrationTest {
@@ -64,6 +65,7 @@
                         return containerName.equals(md.getName()) && location.equals(md.getLocation());
                      }
                   }) : String.format("container %s/%s not found in list %s", location, containerName, list);
+                  assertTrue(view.getBlobStore().containerExists(containerName), containerName);
                }
 
             });
diff --git a/providers/aws-s3/src/test/java/org/jclouds/aws/s3/blobstore/integration/PathBasedContainerLiveTest.java b/providers/aws-s3/src/test/java/org/jclouds/aws/s3/blobstore/integration/PathBasedContainerLiveTest.java
new file mode 100644
index 0000000..27daa6e
--- /dev/null
+++ b/providers/aws-s3/src/test/java/org/jclouds/aws/s3/blobstore/integration/PathBasedContainerLiveTest.java
@@ -0,0 +1,44 @@
+/**
+ * Licensed to jclouds, Inc. (jclouds) under one or more
+ * contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  jclouds 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.
+ */
+package org.jclouds.aws.s3.blobstore.integration;
+
+import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS;
+
+import java.util.Properties;
+
+import org.jclouds.s3.blobstore.integration.S3ContainerLiveTest;
+import org.testng.annotations.Test;
+
+/**
+ * @author Adrian Cole
+ */
+@Test(groups = "live", singleThreaded = true, testName = "PathBasedContainerLiveTest")
+public class PathBasedContainerLiveTest  extends S3ContainerLiveTest {
+   public PathBasedContainerLiveTest() {
+      provider = "aws-s3";
+   }
+
+   @Override
+   public Properties setupProperties() {
+      Properties properties = super.setupProperties();
+      properties.setProperty(PROPERTY_S3_VIRTUAL_HOST_BUCKETS, "false");
+      return properties;
+   }
+
+}