Skip to content

Commit

Permalink
Merge pull request #8 from christav/update-breaklease-signature-88
Browse files Browse the repository at this point in the history
Update breaklease signature, fix for #88
  • Loading branch information
Chris Tavares committed Feb 7, 2013
2 parents e35b776 + 11dc82d commit df8140d
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.microsoft.windowsazure.services.blob.models.AcquireLeaseResult;
import com.microsoft.windowsazure.services.blob.models.BlobServiceOptions;
import com.microsoft.windowsazure.services.blob.models.BlockList;
import com.microsoft.windowsazure.services.blob.models.BreakLeaseResult;
import com.microsoft.windowsazure.services.blob.models.CommitBlobBlocksOptions;
import com.microsoft.windowsazure.services.blob.models.ContainerACL;
import com.microsoft.windowsazure.services.blob.models.CopyBlobOptions;
Expand Down Expand Up @@ -1440,16 +1441,23 @@ void releaseLease(String container, String blob, String leaseId, BlobServiceOpti
* <p>
* A lease that has been broken but has not yet expired can also be released, in which case another client may
* immediately acquire a new lease on the blob.
*
*
* @deprecated Server ignores the leaseId parameter, replaced by {@link #breakLease(String, String)} without
* the useless parameter.
*
* @param container
* A {@link String} containing the name of the blob's container.
* @param blob
* A {@link String} containing the name of the blob to break a lease on.
* @param leaseId
* A {@link String} containing an optional lease ID.
*
* @param leaseId lease id to break. Ignored.
*
* @throws ServiceException
* if an error occurs accessing the storage service.
*
* @return result containing time remaining before a new lease can be acquired
*/
@Deprecated()
void breakLease(String container, String blob, String leaseId) throws ServiceException;

/**
Expand All @@ -1465,17 +1473,78 @@ void releaseLease(String container, String blob, String leaseId, BlobServiceOpti
* <p>
* A lease that has been broken but has not yet expired can also be released, in which case another client may
* immediately acquire a new lease on the blob.
*
*
* @deprecated Server ignores the leaseId parameter, replaced by
* {@link #breakLease(String, String, com.microsoft.windowsazure.services.blob.models.BlobServiceOptions)}
* without the useless parameter.
*
*
* @param container
* A {@link String} containing the name of the blob's container.
* @param blob
* A {@link String} containing the name of the blob to break a lease on.
* @param leaseId
* A {@link String} containing an optional lease ID.
* @param options
* A {@link BlobServiceOptions} instance containing options for the request.
* A {@link com.microsoft.windowsazure.services.blob.models.BlobServiceOptions} instance containing options for the request.
* @param leaseId lease id to break. Ignored.
*
* @throws ServiceException
* if an error occurs accessing the storage service.
* @return result containing time remaining before a new lease can be acquired
*/
@Deprecated()
void breakLease(String container, String blob, String leaseId, BlobServiceOptions options) throws ServiceException;

/**
* Breaks an active lease on a blob.
* <p>
* This method breaks the lease on the blob specified by the <em>blob</em> and <em>container</em> parameters. The
* <em>leaseId</em> parameter is not used by the server.
* <p>
* Once a lease is broken, it cannot be renewed. Any authorized request can break the lease; the request is not
* required to specify a matching lease ID. When a lease is broken, the remaining time on the lease is allowed to
* elapse, during which time no lease operation may be performed on the blob.
* <p>
* A lease that has been broken but has not yet expired can also be released, in which case another client may
* immediately acquire a new lease on the blob.
*
*
*
* @param container
* A {@link String} containing the name of the blob's container.
* @param blob
* A {@link String} containing the name of the blob to break a lease on.
* @throws ServiceException
* if an error occurs accessing the storage service.
*
* @return result containing time remaining before a new lease can be acquired
*/
BreakLeaseResult breakLease(String container, String blob) throws ServiceException;

/**
* Breaks an active lease on a blob, using the specified options.
* <p>
* This method breaks the lease on the blob specified by the <em>blob</em> and <em>container</em> parameters. The
* <em>leaseId</em> parameter is not used by the server. Use the {@link BlobServiceOptions options} parameter to
* specify the server timeout for the operation.
* <p>
* Once a lease is broken, it cannot be renewed. Any authorized request can break the lease; the request is not
* required to specify a matching lease ID. When a lease is broken, the remaining time on the lease is allowed to
* elapse, during which time no lease operation may be performed on the blob.
* <p>
* A lease that has been broken but has not yet expired can also be released, in which case another client may
* immediately acquire a new lease on the blob.
*
*
*
* @param container
* A {@link String} containing the name of the blob's container.
* @param blob
* A {@link String} containing the name of the blob to break a lease on.
* @param options
* A {@link com.microsoft.windowsazure.services.blob.models.BlobServiceOptions} instance containing options for the request.
* @throws ServiceException
* if an error occurs accessing the storage service.
* @return result containing time remaining before a new lease can be acquired
*/
BreakLeaseResult breakLease(String container, String blob, BlobServiceOptions options) throws ServiceException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.microsoft.windowsazure.services.blob.models.AcquireLeaseResult;
import com.microsoft.windowsazure.services.blob.models.BlobServiceOptions;
import com.microsoft.windowsazure.services.blob.models.BlockList;
import com.microsoft.windowsazure.services.blob.models.BreakLeaseResult;
import com.microsoft.windowsazure.services.blob.models.CommitBlobBlocksOptions;
import com.microsoft.windowsazure.services.blob.models.ContainerACL;
import com.microsoft.windowsazure.services.blob.models.CopyBlobOptions;
Expand Down Expand Up @@ -888,9 +889,21 @@ public void releaseLease(String container, String blob, String leaseId, BlobServ
}

@Override
@Deprecated
public void breakLease(String container, String blob, String leaseId) throws ServiceException {
breakLease(container, blob);
}

@Override
@Deprecated
public void breakLease(String container, String blob, String leaseId, BlobServiceOptions options) throws ServiceException {
breakLease(container, blob, options);
}

@Override
public BreakLeaseResult breakLease(String container, String blob) throws ServiceException {
try {
service.breakLease(container, blob, leaseId);
return service.breakLease(container, blob);
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
Expand All @@ -901,10 +914,10 @@ public void breakLease(String container, String blob, String leaseId) throws Ser
}

@Override
public void breakLease(String container, String blob, String leaseId, BlobServiceOptions options)
public BreakLeaseResult breakLease(String container, String blob, BlobServiceOptions options)
throws ServiceException {
try {
service.breakLease(container, blob, leaseId, options);
return service.breakLease(container, blob, options);
}
catch (UniformInterfaceException e) {
throw processCatch(new ServiceException(e));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.microsoft.windowsazure.services.blob.models.BlobProperties;
import com.microsoft.windowsazure.services.blob.models.BlobServiceOptions;
import com.microsoft.windowsazure.services.blob.models.BlockList;
import com.microsoft.windowsazure.services.blob.models.BreakLeaseResult;
import com.microsoft.windowsazure.services.blob.models.CommitBlobBlocksOptions;
import com.microsoft.windowsazure.services.blob.models.ContainerACL;
import com.microsoft.windowsazure.services.blob.models.ContainerACL.PublicAccessType;
Expand Down Expand Up @@ -801,33 +802,56 @@ public void releaseLease(String container, String blob, String leaseId, BlobServ
}

@Override
@Deprecated
public void breakLease(String container, String blob, String leaseId) throws ServiceException {
breakLease(container, blob, leaseId, new BlobServiceOptions());
breakLease(container, blob);
}

@Override
public void breakLease(String container, String blob, String leaseId, BlobServiceOptions options)
throws ServiceException {
putLeaseImpl("break", container, blob, leaseId, options, null/* accessCondition */);
@Deprecated
public void breakLease(String container, String blob, String leaseId, BlobServiceOptions options) throws ServiceException {
breakLease(container, blob, options);
}

@Override
public BreakLeaseResult breakLease(String container, String blob) throws ServiceException {
return breakLease(container, blob, new BlobServiceOptions());
}

@Override
public BreakLeaseResult breakLease(String container, String blob, BlobServiceOptions options)
throws ServiceException {
ClientResponse response = doLeaseOperation("break", container, blob, null, options, null);

BreakLeaseResult result = new BreakLeaseResult();
result.setRemainingLeaseTimeInSeconds(Integer.parseInt(response.getHeaders().getFirst("x-ms-lease-time")));
return result;
}

private AcquireLeaseResult putLeaseImpl(String leaseAction, String container, String blob, String leaseId,
BlobServiceOptions options, AccessCondition accessCondition) throws ServiceException {
ClientResponse response = doLeaseOperation(leaseAction, container, blob, leaseId, options, accessCondition);

AcquireLeaseResult result = new AcquireLeaseResult();
result.setLeaseId(response.getHeaders().getFirst("x-ms-lease-id"));
return result;
}

private ClientResponse doLeaseOperation(String leaseAction, String container, String blob, String leaseId, BlobServiceOptions options, AccessCondition accessCondition) {
String path = createPathFromContainer(container);
WebResource webResource = getResource(options).path(path).path(blob).queryParam("comp", "lease");

Builder builder = webResource.header("x-ms-version", API_VERSION);
builder = addOptionalHeader(builder, "x-ms-lease-id", leaseId);
if (leaseId != null) {
builder = addOptionalHeader(builder, "x-ms-lease-id", leaseId);
}
builder = addOptionalHeader(builder, "x-ms-lease-action", leaseAction);
builder = addOptionalAccessConditionHeader(builder, accessCondition);

// Note: Add content type here to enable proper HMAC signing
ClientResponse response = builder.put(ClientResponse.class, "");
ThrowIfError(response);

AcquireLeaseResult result = new AcquireLeaseResult();
result.setLeaseId(response.getHeaders().getFirst("x-ms-lease-id"));
return result;
return response;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

/**
* A wrapper class for the response returned from a Blob Service REST API Lease Blob operation. This is returned by
* calls to implementations of {@link BlobContract#breakLease(String, String, String, BlobServiceOptions)},
* {@link BlobContract#releaseLease(String, String, String, BlobServiceOptions)} and
* {@link BlobContract#renewLease(String, String, String, BlobServiceOptions)}.
* calls to implementations of {@link BlobContract#acquireLease(String, String)},
* {@link BlobContract#acquireLease(String, String, AcquireLeaseOptions)},
* {@link BlobContract#renewLease(String, String, String, BlobServiceOptions)}, and
* {@link BlobContract#renewLease(String, String, String)}.
* <p>
* See the <a href="http://msdn.microsoft.com/en-us/library/windowsazure/ee691972.aspx">Lease Blob</a> documentation on
* MSDN for details of the underlying Blob Service REST API operation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright Microsoft Corporation
*
* 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 com.microsoft.windowsazure.services.blob.models;

/**
* A wrapper class for the response returned from a Blob Service REST API Break Lease Blob operation. This is returned by
* calls to implementations of {@link com.microsoft.windowsazure.services.blob.BlobContract#breakLease(String, String, BlobServiceOptions)},
* <p>
* See the <a href="http://msdn.microsoft.com/en-us/library/windowsazure/ee691972.aspx">Lease Blob</a> documentation on
* MSDN for details of the underlying Blob Service REST API operation.
*/
public class BreakLeaseResult {
private int remainingLeaseTimeInSeconds;

public int getRemainingLeaseTimeInSeconds() {
return remainingLeaseTimeInSeconds;
}

public void setRemainingLeaseTimeInSeconds(int remainingLeaseTimeInSeconds) {
this.remainingLeaseTimeInSeconds = remainingLeaseTimeInSeconds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.microsoft.windowsazure.services.blob.models.AccessCondition;
import com.microsoft.windowsazure.services.blob.models.BlobProperties;
import com.microsoft.windowsazure.services.blob.models.BlockList;
import com.microsoft.windowsazure.services.blob.models.BreakLeaseResult;
import com.microsoft.windowsazure.services.blob.models.ContainerACL;
import com.microsoft.windowsazure.services.blob.models.ContainerACL.PublicAccessType;
import com.microsoft.windowsazure.services.blob.models.CopyBlobResult;
Expand Down Expand Up @@ -325,7 +326,7 @@ public void deleteContainerWorks() throws Exception {

// Assert
for (Container container : listContainerResult.getContainers()) {
assertTrue(!container.getName().equals(container));
assertTrue(!container.getName().equals(containerName));
}
}

Expand Down Expand Up @@ -1263,7 +1264,7 @@ public void getBlobWithIfNotModifiedSinceAccessConditionWorks() throws Exception
Date lastModifiedBase = (Date) props.getProperties().getLastModified().clone();

// +1 second
Date lastModifiedNext = new Date(lastModifiedBase.getTime() + 1 * 1000);
Date lastModifiedNext = new Date(lastModifiedBase.getTime() + 1000);

while (true) {
HashMap<String, String> metadata = new HashMap<String, String>();
Expand Down Expand Up @@ -1492,7 +1493,7 @@ public void deleteBlobSnapshotSuccess() throws Exception {
ListBlobsResult listBlobsResult = service.listBlobs(TEST_CONTAINER_FOR_BLOBS);
List<BlobEntry> blobEntry = listBlobsResult.getBlobs();
for (BlobEntry blobEntryItem : blobEntry) {
assertTrue(blobEntryItem.getSnapshot() != snapshot);
assertTrue(blobEntryItem.getSnapshot() == null || !blobEntryItem.getSnapshot().equals(snapshot));
}
assertTrue(true);
}
Expand Down Expand Up @@ -1595,11 +1596,13 @@ public void breakLeaseWorks() throws Exception {
String content = "some content2";
service.createBlockBlob(TEST_CONTAINER_FOR_BLOBS, "test6", new ByteArrayInputStream(content.getBytes("UTF-8")));
String leaseId = service.acquireLease(TEST_CONTAINER_FOR_BLOBS, "test6").getLeaseId();
service.breakLease(TEST_CONTAINER_FOR_BLOBS, "test6", leaseId);
BreakLeaseResult breakResult = service.breakLease(TEST_CONTAINER_FOR_BLOBS, "test6");
service.releaseLease(TEST_CONTAINER_FOR_BLOBS, "test6", leaseId);

// Assert
assertNotNull(leaseId);
assertNotNull(breakResult);
assertTrue(breakResult.getRemainingLeaseTimeInSeconds() > 0);
}

class RetryPolicyObserver implements ServiceFilter {
Expand Down

0 comments on commit df8140d

Please sign in to comment.