1 package org.kuali.common.aws.s3.model; 2 3 import static com.amazonaws.services.s3.Headers.CONTENT_TYPE; 4 import static com.google.common.base.Optional.absent; 5 import static com.google.common.base.Optional.fromNullable; 6 import static com.google.common.collect.Maps.newHashMap; 7 import static org.kuali.common.core.base.Optionals.fromNullableDate; 8 import static org.kuali.common.core.base.Optionals.fromTrimToNull; 9 10 import java.util.Map; 11 12 import org.kuali.common.core.build.ValidatingBuilder; 13 import org.kuali.common.core.validate.annotation.IdiotProofImmutable; 14 15 import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 16 import com.google.common.base.Optional; 17 import com.google.common.collect.ImmutableMap; 18 19 @IdiotProofImmutable 20 @JsonDeserialize(builder = ObjectMetadata.Builder.class) 21 public final class ObjectMetadata { 22 23 private final ImmutableMap<String, String> userMetadata; 24 private final ImmutableMap<String, Object> rawMetadata; 25 private final Optional<Long> httpExpiresTime; 26 private final Optional<Long> expirationTime; 27 private final Optional<String> expirationTimeRuleId; 28 private final Optional<Boolean> ongoingRestore; 29 private final Optional<Long> restoreExpirationTime; 30 31 private ObjectMetadata(Builder builder) { 32 this.userMetadata = ImmutableMap.copyOf(builder.userMetadata); 33 this.rawMetadata = ImmutableMap.copyOf(builder.rawMetadata); 34 this.httpExpiresTime = builder.httpExpiresTime; 35 this.expirationTime = builder.expirationTime; 36 this.expirationTimeRuleId = builder.expirationTimeRuleId; 37 this.ongoingRestore = builder.ongoingRestore; 38 this.restoreExpirationTime = builder.restoreExpirationTime; 39 } 40 41 public static ObjectMetadata copyOf(com.amazonaws.services.s3.model.ObjectMetadata mutable) { 42 Builder builder = builder(); 43 builder.withUserMetadata(mutable.getUserMetadata()); 44 builder.withRawMetadata(mutable.getRawMetadata()); 45 builder.withExpirationTimeRuleId(fromTrimToNull(mutable.getExpirationTimeRuleId())); 46 builder.withOngoingRestore(fromNullable(mutable.getOngoingRestore())); 47 builder.withHttpExpiresTime(fromNullableDate(mutable.getHttpExpiresDate())); 48 builder.withExpirationTime(fromNullableDate(mutable.getExpirationTime())); 49 builder.withRestoreExpirationTime(fromNullableDate(mutable.getRestoreExpirationTime())); 50 return builder.build(); 51 } 52 53 public static ObjectMetadata build() { 54 return builder().build(); 55 } 56 57 public static ObjectMetadata build(String contentType) { 58 Map<String, Object> raw = newHashMap(); 59 raw.put(CONTENT_TYPE, contentType); 60 return builder().withRawMetadata(raw).build(); 61 } 62 63 public static Builder builder() { 64 return new Builder(); 65 } 66 67 public static class Builder extends ValidatingBuilder<ObjectMetadata> { 68 69 private Map<String, String> userMetadata = newHashMap(); 70 private Map<String, Object> rawMetadata = newHashMap(); 71 private Optional<Long> httpExpiresTime = absent(); 72 private Optional<Long> expirationTime = absent(); 73 private Optional<String> expirationTimeRuleId = absent(); 74 private Optional<Boolean> ongoingRestore = absent(); 75 private Optional<Long> restoreExpirationTime = absent(); 76 77 public Builder withUserMetadata(Map<String, String> userMetadata) { 78 this.userMetadata = userMetadata; 79 return this; 80 } 81 82 public Builder withRawMetadata(Map<String, Object> rawMetadata) { 83 this.rawMetadata = rawMetadata; 84 return this; 85 } 86 87 public Builder withHttpExpiresTime(Optional<Long> httpExpiresTime) { 88 this.httpExpiresTime = httpExpiresTime; 89 return this; 90 } 91 92 public Builder withExpirationTime(Optional<Long> expirationTime) { 93 this.expirationTime = expirationTime; 94 return this; 95 } 96 97 public Builder withExpirationTimeRuleId(Optional<String> expirationTimeRuleId) { 98 this.expirationTimeRuleId = expirationTimeRuleId; 99 return this; 100 } 101 102 public Builder withOngoingRestore(Optional<Boolean> ongoingRestore) { 103 this.ongoingRestore = ongoingRestore; 104 return this; 105 } 106 107 public Builder withRestoreExpirationTime(Optional<Long> restoreExpirationTime) { 108 this.restoreExpirationTime = restoreExpirationTime; 109 return this; 110 } 111 112 @Override 113 public ObjectMetadata build() { 114 return validate(new ObjectMetadata(this)); 115 } 116 } 117 118 public Map<String, String> getUserMetadata() { 119 return userMetadata; 120 } 121 122 public Map<String, Object> getRawMetadata() { 123 return rawMetadata; 124 } 125 126 public Optional<Long> httpExpiresTime() { 127 return httpExpiresTime; 128 } 129 130 public Optional<Long> getExpirationTime() { 131 return expirationTime; 132 } 133 134 public Optional<String> getExpirationTimeRuleId() { 135 return expirationTimeRuleId; 136 } 137 138 public Optional<Boolean> getOngoingRestore() { 139 return ongoingRestore; 140 } 141 142 public Optional<Long> getRestoreExpirationTime() { 143 return restoreExpirationTime; 144 } 145 146 }