1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package dev.aherscu.qa.s3.publisher.maven.plugin.util;
18
19 import java.io.*;
20 import java.util.*;
21
22 import javax.activation.*;
23
24 import com.amazonaws.services.s3.model.*;
25
26 import dev.aherscu.qa.s3.publisher.maven.plugin.config.*;
27
28 public class ObjectMetadataBuilder {
29
30 private static final MimetypesFileTypeMap mimeMap =
31 new MimetypesFileTypeMap();
32
33 private final ManagedFile managedFile;
34 private final File encodedFile;
35 private final ObjectMetadata objectMetadata;
36 private Date expiresReference;
37
38 public ObjectMetadataBuilder(final ManagedFile managedFile,
39 final File encodedFile) {
40 this.managedFile = managedFile;
41 this.encodedFile = encodedFile;
42 objectMetadata = new ObjectMetadata();
43 }
44
45 private static boolean isEmptyContentType(final String contentType) {
46 return contentType == null || "".equals(contentType);
47 }
48
49 public ObjectMetadata buildMetadata() {
50 initializeCalendar();
51 assignContentLength();
52 assignLastModified();
53 assignContentEncoding();
54 assignContentType();
55 assignCacheControl();
56 assignContentDisposition();
57 assignContentLanguage();
58 assignExpires();
59 assignWebsiteRedirectLocation();
60
61 return objectMetadata;
62 }
63
64 Date getExpiresReference() {
65 return expiresReference;
66 }
67
68 private void assignCacheControl() {
69 if (managedFile.getMetadata().getCacheControl() != null) {
70 objectMetadata
71 .setCacheControl(managedFile.getMetadata().getCacheControl());
72 }
73 }
74
75 private void assignContentDisposition() {
76 if (managedFile.getMetadata().getContentDisposition() != null) {
77 objectMetadata.setContentDisposition(
78 managedFile.getMetadata().getContentDisposition());
79 }
80 }
81
82 private void assignContentEncoding() {
83 if (!encodedFile.equals(new File(managedFile.getFilename()))) {
84 objectMetadata.setContentEncoding(
85 managedFile.getMetadata().getContentEncoding());
86 }
87 }
88
89 private void assignContentLanguage() {
90 if (managedFile.getMetadata().getContentLanguage() != null) {
91 objectMetadata.setHeader("Content-Disposition",
92 managedFile.getMetadata().getContentLanguage());
93 }
94 }
95
96 private void assignContentLength() {
97 objectMetadata.setContentLength(encodedFile.length());
98 }
99
100 private void assignContentType() {
101 String contentType;
102 if (!isEmptyContentType(managedFile.getMetadata().getContentType())) {
103 contentType = managedFile.getMetadata().getContentType();
104 } else {
105 contentType = mimeMap.getContentType(managedFile.getFilename());
106 }
107 objectMetadata.setContentType(contentType);
108 }
109
110 private void assignExpires() {
111 if (managedFile.getMetadata().getSecondsToExpire() != 0) {
112 final Calendar calendar = Calendar.getInstance();
113 calendar.add(Calendar.SECOND,
114 managedFile.getMetadata().getSecondsToExpire());
115 objectMetadata.setHttpExpiresDate(calendar.getTime());
116 }
117 }
118
119 private void assignLastModified() {
120 objectMetadata.setLastModified(new Date(encodedFile.lastModified()));
121 }
122
123 private void assignWebsiteRedirectLocation() {
124 if (managedFile.getMetadata().getWebsiteRedirectLocation() != null) {
125 objectMetadata.setHeader("x-amz-website-redirect-location",
126 managedFile.getMetadata().getWebsiteRedirectLocation());
127 }
128 }
129
130 private void initializeCalendar() {
131 final Calendar calendar = Calendar.getInstance();
132 expiresReference = calendar.getTime();
133 }
134 }