diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java index 9ac73c7486..988b95399a 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/CommonSwiftAsyncClient.java @@ -41,6 +41,7 @@ import org.jclouds.blobstore.BlobStoreFallbacks.NullOnKeyNotFound; import org.jclouds.blobstore.binders.BindMapToHeadersWithPrefix; import org.jclouds.blobstore.domain.PageSet; import org.jclouds.http.functions.ParseETagHeader; +import org.jclouds.http.functions.ReturnTrueIf201; import org.jclouds.http.options.GetOptions; import org.jclouds.openstack.swift.SwiftFallbacks.TrueOn404FalseOn409; import org.jclouds.openstack.swift.binders.BindIterableToHeadersWithContainerDeleteMetadataPrefix; @@ -142,6 +143,7 @@ public interface CommonSwiftAsyncClient extends Closeable { */ @Named("CreateContainer") @PUT + @ResponseParser(ReturnTrueIf201.class) @Path("/{container}") ListenableFuture createContainer(@PathParam("container") String container, CreateContainerOptions... options); @@ -161,6 +163,7 @@ public interface CommonSwiftAsyncClient extends Closeable { */ @Named("CreateContainer") @PUT + @ResponseParser(ReturnTrueIf201.class) @Path("/{container}") ListenableFuture createContainer(@PathParam("container") String container); diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java index 61159bda45..e217445b51 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/SwiftClientExpectTest.java @@ -54,6 +54,37 @@ public class SwiftClientExpectTest extends BaseSwiftExpectTest { assertTrue(clientWhenContainerExists.containerExists("foo")); } + @Test + public void testCreateContainerReturnStatus() { + String containerName = "foo"; + HttpRequest createContainerRequest = HttpRequest.builder() + .method("PUT") + .endpoint(swiftEndpointWithHostReplaced + "/" + containerName) + .addHeader("X-Auth-Token", authToken).build(); + HttpResponse createContainerResponse = HttpResponse.builder() + .statusCode(201) + .build(); + + SwiftClient clientWhenNonExistingContainer = requestsSendResponses( + authRequest, authResponse, createContainerRequest, + createContainerResponse); + + assertTrue(clientWhenNonExistingContainer.createContainer( + containerName)); + + // Try creating the same container again. This should return a status + // code of 202 as per the following: + // http://docs.openstack.org/api/openstack-object-storage/1.0/content/create-container.html + // http://docs.rackspace.com/files/api/v1/cf-devguide/content/Create_Container-d1e1694.html + + createContainerResponse = HttpResponse.builder().statusCode(202) + .build(); + SwiftClient clientWhenExistingContainer = requestsSendResponses( + authRequest, authResponse, createContainerRequest, + createContainerResponse); + assertFalse(clientWhenExistingContainer.createContainer(containerName)); + } + @Test public void testContainerExistsWhenResponseIs404ReturnsFalse() { HttpRequest headContainer = HttpRequest.builder() diff --git a/core/src/main/java/org/jclouds/http/functions/ReturnTrueIf201.java b/core/src/main/java/org/jclouds/http/functions/ReturnTrueIf201.java new file mode 100644 index 0000000000..e44e98d27a --- /dev/null +++ b/core/src/main/java/org/jclouds/http/functions/ReturnTrueIf201.java @@ -0,0 +1,38 @@ +/* + * 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. + */ +package org.jclouds.http.functions; + +import static org.jclouds.http.HttpUtils.releasePayload; + +import javax.inject.Singleton; + +import org.jclouds.http.HttpResponse; + +import com.google.common.base.Function; + +/** + * Parses the status code of an HTTPResponse and returns True if it is 201. + * False otherwise. + */ +@Singleton +public class ReturnTrueIf201 implements Function { + @Override + public Boolean apply(HttpResponse from) { + releasePayload(from); + return from.getStatusCode() == 201; + } +}