Skip to content

Commit

Permalink
Fix #8003.
Browse files Browse the repository at this point in the history
Former-commit-id: 9e5ba51fd93951c553ddc1c262d4d0b13e586842
  • Loading branch information
dkocher committed Jun 10, 2014
1 parent 71b3667 commit 43b60e3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
52 changes: 43 additions & 9 deletions source/ch/cyberduck/core/azure/AzureMetadataFeature.java
Expand Up @@ -23,15 +23,20 @@
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.features.Headers;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHeaders;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import com.microsoft.windowsazure.services.blob.client.BlobRequestOptions;
import com.microsoft.windowsazure.services.blob.client.CloudBlobContainer;
import com.microsoft.windowsazure.services.blob.client.CloudBlockBlob;
import com.microsoft.windowsazure.services.core.storage.RetryNoRetry;
import com.microsoft.windowsazure.services.core.storage.StorageException;
import com.microsoft.azure.storage.AccessCondition;
import com.microsoft.azure.storage.RetryNoRetry;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.blob.BlobProperties;
import com.microsoft.azure.storage.blob.BlobRequestOptions;
import com.microsoft.azure.storage.blob.CloudBlobContainer;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

/**
* @version $Id$
Expand All @@ -58,8 +63,18 @@ public Map<String, String> getMetadata(Path file) throws BackgroundException {
else {
final CloudBlockBlob blob = session.getClient().getContainerReference(containerService.getContainer(file).getName())
.getBlockBlobReference(containerService.getKey(file));
// Populates the blob properties and metadata
blob.downloadAttributes();
return blob.getMetadata();
final Map<String, String> metadata = new HashMap<String, String>();
metadata.putAll(blob.getMetadata());
final BlobProperties properties = blob.getProperties();
if(StringUtils.isNotBlank(properties.getCacheControl())) {
metadata.put(HttpHeaders.CACHE_CONTROL, properties.getCacheControl());
}
if(StringUtils.isNotBlank(properties.getContentType())) {
metadata.put(HttpHeaders.CONTENT_TYPE, properties.getContentType());
}
return metadata;
}
}
catch(URISyntaxException e) {
Expand All @@ -78,13 +93,32 @@ public void setMetadata(Path file, Map<String, String> metadata) throws Backgrou
if(containerService.isContainer(file)) {
final CloudBlobContainer container = session.getClient().getContainerReference(containerService.getContainer(file).getName());
container.setMetadata(new HashMap<String, String>(metadata));
container.uploadMetadata(options, null);
container.uploadMetadata(AccessCondition.generateEmptyCondition(), options, null);
}
else {
final CloudBlockBlob blob = session.getClient().getContainerReference(containerService.getContainer(file).getName())
.getBlockBlobReference(containerService.getKey(file));
blob.setMetadata(new HashMap<String, String>(metadata));
blob.uploadMetadata(null, options, null);
// Populates the blob properties and metadata
blob.downloadAttributes();
// Replace metadata
final HashMap<String, String> pruned = new HashMap<String, String>();
for(Map.Entry<String, String> m : metadata.entrySet()) {
final BlobProperties properties = blob.getProperties();
if(HttpHeaders.CACHE_CONTROL.equalsIgnoreCase(m.getKey())) {
// Update properties
properties.setCacheControl(m.getValue());
continue;
}
if(HttpHeaders.CONTENT_TYPE.equalsIgnoreCase(m.getKey())) {
// Update properties
properties.setContentType(m.getValue());
continue;
}
pruned.put(m.getKey(), m.getValue());
}
blob.setMetadata(pruned);
blob.uploadMetadata(AccessCondition.generateEmptyCondition(), options, null);
blob.uploadProperties();
}
}
catch(URISyntaxException e) {
Expand Down
26 changes: 25 additions & 1 deletion test/ch/cyberduck/core/azure/AzureMetadataFeatureTest.java
Expand Up @@ -25,7 +25,6 @@
*/
public class AzureMetadataFeatureTest extends AbstractTestCase {


@Test
public void testSetMetadata() throws Exception {
final Host host = new Host(new AzureProtocol(), "cyberduck.blob.core.windows.net", new Credentials(
Expand All @@ -46,4 +45,29 @@ public void testSetMetadata() throws Exception {
new AzureDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginController());
session.close();
}

@Test
public void testSetCacheControl() throws Exception {
final Host host = new Host(new AzureProtocol(), "cyberduck.blob.core.windows.net", new Credentials(
properties.getProperty("azure.account"), properties.getProperty("azure.key")
));
final AzureSession session = new AzureSession(host);
new LoginConnectionService(new DisabledLoginController(), new DisabledHostKeyCallback(),
new DisabledPasswordStore(), new DisabledProgressListener()).connect(session, Cache.empty());
final Path container = new Path("cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
final Path test = new Path(container, UUID.randomUUID().toString() + ".txt", EnumSet.of(Path.Type.file));
new AzureTouchFeature(session).touch(test);
final AzureMetadataFeature service = new AzureMetadataFeature(session);
service.setMetadata(test, Collections.<String, String>singletonMap("Cache-Control",
"public, max-age=0"));
final Map<String, String> metadata = service.getMetadata(test);
assertFalse(metadata.isEmpty());
assertTrue(metadata.containsKey("Cache-Control"));
assertEquals("public, max-age=0", metadata.get("Cache-Control"));
// Make sure content type is not deleted
assertTrue(metadata.containsKey("Content-Type"));
assertEquals("application/octet-stream", metadata.get("Content-Type"));
new AzureDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginController());
session.close();
}
}

0 comments on commit 43b60e3

Please sign in to comment.