1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.jclouds.io;
20
21 import static com.google.common.collect.Iterables.any;
22 import static javax.ws.rs.core.HttpHeaders.CONTENT_LENGTH;
23 import static javax.ws.rs.core.HttpHeaders.CONTENT_TYPE;
24
25 import java.io.Serializable;
26 import java.util.Arrays;
27 import java.util.Map.Entry;
28
29 import org.jclouds.javax.annotation.Nullable;
30
31 import org.jclouds.crypto.CryptoStreams;
32 import org.jclouds.io.payloads.BaseImmutableContentMetadata;
33
34 import com.google.common.base.Predicate;
35 import com.google.common.collect.Multimap;
36
37
38
39
40 public class ContentMetadataBuilder implements Serializable {
41
42 private static final long serialVersionUID = -5279643002875371558L;
43
44 public static ContentMetadataBuilder create() {
45 return new ContentMetadataBuilder();
46 }
47
48 protected String contentType = "application/unknown";
49 protected Long contentLength;
50 protected byte[] contentMD5;
51 protected String contentDisposition;
52 protected String contentLanguage;
53 protected String contentEncoding;
54
55 public ContentMetadataBuilder fromHttpHeaders(Multimap<String, String> headers) {
56 boolean chunked = any(headers.entries(), new Predicate<Entry<String, String>>() {
57 @Override
58 public boolean apply(Entry<String, String> input) {
59 return "Transfer-Encoding".equalsIgnoreCase(input.getKey()) && "chunked".equalsIgnoreCase(input.getValue());
60 }
61 });
62 for (Entry<String, String> header : headers.entries()) {
63 if (!chunked && CONTENT_LENGTH.equalsIgnoreCase(header.getKey())) {
64 contentLength(new Long(header.getValue()));
65 } else if ("Content-MD5".equalsIgnoreCase(header.getKey())) {
66 contentMD5(CryptoStreams.base64(header.getValue()));
67 } else if (CONTENT_TYPE.equalsIgnoreCase(header.getKey())) {
68 contentType(header.getValue());
69 } else if ("Content-Disposition".equalsIgnoreCase(header.getKey())) {
70 contentDisposition(header.getValue());
71 } else if ("Content-Encoding".equalsIgnoreCase(header.getKey())) {
72 contentEncoding(header.getValue());
73 } else if ("Content-Language".equalsIgnoreCase(header.getKey())) {
74 contentLanguage(header.getValue());
75 }
76 }
77 return this;
78 }
79
80 public ContentMetadataBuilder contentLength(@Nullable Long contentLength) {
81 this.contentLength = contentLength;
82 return this;
83 }
84
85 public ContentMetadataBuilder contentMD5(byte[] md5) {
86 if (md5 != null) {
87 byte[] retval = new byte[md5.length];
88 System.arraycopy(md5, 0, retval, 0, md5.length);
89 this.contentMD5 = md5;
90 }
91 return this;
92
93 }
94
95 public ContentMetadataBuilder contentType(@Nullable String contentType) {
96 this.contentType = contentType;
97 return this;
98 }
99
100 public ContentMetadataBuilder contentDisposition(@Nullable String contentDisposition) {
101 this.contentDisposition = contentDisposition;
102 return this;
103
104 }
105
106 public ContentMetadataBuilder contentLanguage(@Nullable String contentLanguage) {
107 this.contentLanguage = contentLanguage;
108 return this;
109 }
110
111 public ContentMetadataBuilder contentEncoding(@Nullable String contentEncoding) {
112 this.contentEncoding = contentEncoding;
113 return this;
114 }
115
116 public ContentMetadata build() {
117 return new BaseImmutableContentMetadata(contentType, contentLength, contentMD5, contentDisposition,
118 contentLanguage, contentEncoding);
119 }
120
121 public static ContentMetadataBuilder fromContentMetadata(ContentMetadata in) {
122 return new ContentMetadataBuilder().contentType(in.getContentType()).contentLength(in.getContentLength())
123 .contentMD5(in.getContentMD5()).contentDisposition(in.getContentDisposition()).contentLanguage(
124 in.getContentLanguage()).contentEncoding(in.getContentEncoding());
125 }
126
127 @Override
128 public int hashCode() {
129 final int prime = 31;
130 int result = 1;
131 result = prime * result + ((contentDisposition == null) ? 0 : contentDisposition.hashCode());
132 result = prime * result + ((contentEncoding == null) ? 0 : contentEncoding.hashCode());
133 result = prime * result + ((contentLanguage == null) ? 0 : contentLanguage.hashCode());
134 result = prime * result + ((contentLength == null) ? 0 : contentLength.hashCode());
135 result = prime * result + Arrays.hashCode(contentMD5);
136 result = prime * result + ((contentType == null) ? 0 : contentType.hashCode());
137 return result;
138 }
139
140 @Override
141 public boolean equals(Object obj) {
142 if (this == obj)
143 return true;
144 if (obj == null)
145 return false;
146 if (getClass() != obj.getClass())
147 return false;
148 ContentMetadataBuilder other = (ContentMetadataBuilder) obj;
149 if (contentDisposition == null) {
150 if (other.contentDisposition != null)
151 return false;
152 } else if (!contentDisposition.equals(other.contentDisposition))
153 return false;
154 if (contentEncoding == null) {
155 if (other.contentEncoding != null)
156 return false;
157 } else if (!contentEncoding.equals(other.contentEncoding))
158 return false;
159 if (contentLanguage == null) {
160 if (other.contentLanguage != null)
161 return false;
162 } else if (!contentLanguage.equals(other.contentLanguage))
163 return false;
164 if (contentLength == null) {
165 if (other.contentLength != null)
166 return false;
167 } else if (!contentLength.equals(other.contentLength))
168 return false;
169 if (!Arrays.equals(contentMD5, other.contentMD5))
170 return false;
171 if (contentType == null) {
172 if (other.contentType != null)
173 return false;
174 } else if (!contentType.equals(other.contentType))
175 return false;
176 return true;
177 }
178
179 @Override
180 public String toString() {
181 return "[contentDisposition=" + contentDisposition + ", contentEncoding=" + contentEncoding
182 + ", contentLanguage=" + contentLanguage + ", contentLength=" + contentLength + ", contentMD5="
183 + Arrays.toString(contentMD5) + ", contentType=" + contentType + "]";
184 }
185 }