View Javadoc

1   /**
2    * Licensed to jclouds, Inc. (jclouds) under one or more
3    * contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  jclouds licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.jclouds.azureblob.blobstore.functions;
20  
21  import static com.google.common.base.Preconditions.checkNotNull;
22  
23  import java.util.Map;
24  
25  import javax.inject.Inject;
26  import javax.inject.Singleton;
27  
28  import org.jclouds.azureblob.domain.BlobProperties;
29  import org.jclouds.azureblob.domain.PublicAccess;
30  import org.jclouds.blobstore.domain.MutableBlobMetadata;
31  import org.jclouds.blobstore.domain.StorageType;
32  import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl;
33  import org.jclouds.blobstore.strategy.IfDirectoryReturnNameStrategy;
34  import org.jclouds.http.HttpUtils;
35  
36  import com.google.common.base.Function;
37  
38  /**
39   * @author Adrian Cole
40   */
41  @Singleton
42  public class BlobPropertiesToBlobMetadata implements Function<BlobProperties, MutableBlobMetadata> {
43     private final IfDirectoryReturnNameStrategy ifDirectoryReturnName;
44     private final Map<String, PublicAccess> containerAcls;
45  
46     @Inject
47     public BlobPropertiesToBlobMetadata(IfDirectoryReturnNameStrategy ifDirectoryReturnName,
48              Map<String, PublicAccess> containerAcls) {
49        this.ifDirectoryReturnName = checkNotNull(ifDirectoryReturnName, "ifDirectoryReturnName");
50        this.containerAcls = checkNotNull(containerAcls, "containerAcls");
51     }
52  
53     public MutableBlobMetadata apply(BlobProperties from) {
54        if (from == null)
55           return null;
56        MutableBlobMetadata to = new MutableBlobMetadataImpl();
57        HttpUtils.copy(from.getContentMetadata(), to.getContentMetadata());
58        to.setUserMetadata(from.getMetadata());
59        to.setETag(from.getETag());
60        to.setLastModified(from.getLastModified());
61        to.setName(from.getName());
62        to.setContainer(from.getContainer());
63        to.setUri(from.getUrl());
64        try {
65           PublicAccess containerAcl = containerAcls.get(from.getContainer());
66           if (containerAcl != null && containerAcl != PublicAccess.PRIVATE)
67              to.setPublicUri(from.getUrl());
68        } catch (NullPointerException e) {
69           // MapMaker cannot return null, but a call to get acls can
70        }
71        String directoryName = ifDirectoryReturnName.execute(to);
72        if (directoryName != null) {
73           to.setName(directoryName);
74           to.setType(StorageType.RELATIVE_PATH);
75        } else {
76           to.setType(StorageType.BLOB);
77        }
78        return to;
79     }
80  }