| 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.s3.domain.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import javax.inject.Inject; |
| 24 | |
| 25 | import org.jclouds.s3.domain.AccessControlList; |
| 26 | import org.jclouds.s3.domain.MutableObjectMetadata; |
| 27 | import org.jclouds.s3.domain.S3Object; |
| 28 | import org.jclouds.http.internal.PayloadEnclosingImpl; |
| 29 | import org.jclouds.io.Payload; |
| 30 | |
| 31 | import com.google.common.collect.LinkedHashMultimap; |
| 32 | import com.google.common.collect.Multimap; |
| 33 | |
| 34 | /** |
| 35 | * Default Implementation of {@link S3Object}. |
| 36 | * |
| 37 | * @author Adrian Cole |
| 38 | */ |
| 39 | public class S3ObjectImpl extends PayloadEnclosingImpl implements S3Object, Comparable<S3Object> { |
| 40 | |
| 41 | private AccessControlList accessControlList; |
| 42 | |
| 43 | /** |
| 44 | * {@inheritDoc} |
| 45 | */ |
| 46 | @Override |
| 47 | public void setAccessControlList(AccessControlList acl) { |
| 48 | this.accessControlList = acl; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * {@inheritDoc} |
| 53 | */ |
| 54 | @Override |
| 55 | public AccessControlList getAccessControlList() { |
| 56 | return this.accessControlList; |
| 57 | } |
| 58 | |
| 59 | private final MutableObjectMetadata metadata; |
| 60 | private Multimap<String, String> allHeaders = LinkedHashMultimap.create(); |
| 61 | |
| 62 | @Inject |
| 63 | public S3ObjectImpl(MutableObjectMetadata metadata) { |
| 64 | super(); |
| 65 | this.metadata = metadata; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * {@inheritDoc} |
| 70 | */ |
| 71 | @Override |
| 72 | public MutableObjectMetadata getMetadata() { |
| 73 | return metadata; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * {@inheritDoc} |
| 78 | */ |
| 79 | @Override |
| 80 | public Multimap<String, String> getAllHeaders() { |
| 81 | return allHeaders; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * {@inheritDoc} |
| 86 | */ |
| 87 | @Override |
| 88 | public void setAllHeaders(Multimap<String, String> allHeaders) { |
| 89 | this.allHeaders = checkNotNull(allHeaders, "allHeaders"); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * {@inheritDoc} |
| 94 | */ |
| 95 | @Override |
| 96 | public int compareTo(S3Object o) { |
| 97 | if (getMetadata().getKey() == null) |
| 98 | return -1; |
| 99 | return (this == o) ? 0 : getMetadata().getKey().compareTo(o.getMetadata().getKey()); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int hashCode() { |
| 104 | final int prime = 31; |
| 105 | int result = super.hashCode(); |
| 106 | result = prime * result + ((metadata == null) ? 0 : metadata.hashCode()); |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean equals(Object obj) { |
| 112 | if (this == obj) |
| 113 | return true; |
| 114 | if (!super.equals(obj)) |
| 115 | return false; |
| 116 | if (getClass() != obj.getClass()) |
| 117 | return false; |
| 118 | S3ObjectImpl other = (S3ObjectImpl) obj; |
| 119 | if (metadata == null) { |
| 120 | if (other.metadata != null) |
| 121 | return false; |
| 122 | } else if (!metadata.equals(other.metadata)) |
| 123 | return false; |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public String toString() { |
| 129 | return "[metadata=" + metadata + "]"; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void setPayload(Payload data) { |
| 134 | super.setPayload(data); |
| 135 | metadata.setContentMetadata(data.getContentMetadata()); |
| 136 | } |
| 137 | |
| 138 | } |