1 package org.kuali.maven.mojo.s3;
2
3 import java.text.NumberFormat;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import org.apache.commons.lang.StringUtils;
8
9 import com.amazonaws.services.s3.model.ObjectListing;
10 import com.amazonaws.services.s3.model.S3ObjectSummary;
11
12
13
14
15 public class S3DataConverter {
16 HtmlUtils html = new HtmlUtils();
17 NumberFormat nf = getNumberFormatInstance();
18 S3BucketContext context;
19 String browseHtml;
20
21 public S3DataConverter() {
22 this(null);
23 }
24
25 public S3DataConverter(final S3BucketContext context) {
26 super();
27 this.context = context;
28 }
29
30
31
32
33
34
35 protected NumberFormat getNumberFormatInstance() {
36 NumberFormat nf = NumberFormat.getInstance();
37 nf.setMaximumFractionDigits(1);
38 nf.setMinimumFractionDigits(1);
39 nf.setGroupingUsed(false);
40 return nf;
41 }
42
43
44
45
46
47 protected String getTrimmedPrefix(final String prefix, final String delimiter) {
48 int pos = prefix.lastIndexOf(delimiter);
49 if (pos == -1) {
50 return prefix;
51 }
52 return prefix.substring(0, pos);
53 }
54
55
56
57
58
59 protected void addDisplayRows(final List<DisplayRow> displayRows,
60 final List<String[]> data) {
61 for (DisplayRow displayRow : displayRows) {
62 addDisplayRow(displayRow, data);
63 }
64 }
65
66
67
68
69 protected void addDisplayRow(final DisplayRow displayRow, final List<String[]> data) {
70 if (displayRow == null) {
71 return;
72 }
73 String[] row = new String[4];
74 row[0] = displayRow.getImage();
75 row[1] = displayRow.getAhref();
76 row[2] = displayRow.getLastModified();
77 row[3] = displayRow.getSize();
78 data.add(row);
79 }
80
81
82
83
84
85 protected String getShow(final String key, final String prefix) {
86 if (prefix == null) {
87 return key;
88 }
89 int index = prefix.length();
90 String s = key.substring(index);
91 return s;
92 }
93
94
95
96
97 protected DisplayRow getDisplayRow(final String commonPrefix, final String prefix,
98 final String delimiter) {
99
100
101 String image = html.getImage(context.getDirectoryImage());
102 String show = getShow(commonPrefix, prefix);
103 String dest = delimiter + commonPrefix;
104 String ahref = html.getHref(dest, show);
105 String date = "-";
106 String size = "-";
107
108
109 DisplayRow displayRow = new DisplayRow();
110 displayRow.setImage(image);
111 displayRow.setAhref(ahref);
112 displayRow.setLastModified(date);
113 displayRow.setSize(size);
114 return displayRow;
115 }
116
117 protected List<DisplayRow> getDirectoryDisplayRows(
118 final ObjectListing objectListing, final String prefix, final String delimiter) {
119 List<DisplayRow> displayRows = new ArrayList<DisplayRow>();
120 for (String commonPrefix : objectListing.getCommonPrefixes()) {
121 DisplayRow displayRow = getDisplayRow(commonPrefix, prefix,
122 delimiter);
123 if (displayRow == null) {
124 continue;
125 }
126 displayRows.add(displayRow);
127 }
128 return displayRows;
129 }
130
131
132
133
134
135 protected List<String[]> getData(final ObjectListing objectListing,
136 final String prefix, final String delimiter) {
137 DisplayRow upOneDirectory = getUpOneDirectoryDisplayRow(prefix,
138 delimiter);
139 List<DisplayRow> objectDisplayRows = getObjectDisplayRows(
140 objectListing, prefix, delimiter);
141 List<DisplayRow> directoryDisplayRows = getDirectoryDisplayRows(
142 objectListing, prefix, delimiter);
143 List<String[]> data = new ArrayList<String[]>();
144 addDisplayRow(upOneDirectory, data);
145 addDisplayRows(directoryDisplayRows, data);
146 addDisplayRows(objectDisplayRows, data);
147 return data;
148 }
149
150 protected boolean isDirectory(final S3ObjectSummary summary,
151 final List<String> commonPrefixes, final String prefix, final String delimiter) {
152 String key = summary.getKey();
153 if (key.equals(prefix)) {
154 return true;
155 }
156 for (String commonPrefix : commonPrefixes) {
157 if (key.equals(commonPrefix)) {
158 return true;
159 }
160 String trimmedPrefix = getTrimmedPrefix(commonPrefix, delimiter);
161 if (key.equals(trimmedPrefix)) {
162 return true;
163 }
164 }
165 return false;
166 }
167
168
169
170
171 protected DisplayRow getDisplayRow(final S3ObjectSummary summary, final String prefix,
172 final String delimiter) {
173 String key = summary.getKey();
174
175
176 String image = html.getImage(context.getFileImage());
177 String show = getShow(key, prefix);
178 String dest = delimiter + key;
179 String ahref = html.getHref(dest, show);
180 String date = context.getLastModifiedDateFormatter().format(
181 summary.getLastModified());
182 String size = nf.format((summary.getSize() / 1024D)) + " KiB";
183
184
185 DisplayRow displayRow = new DisplayRow();
186 displayRow.setImage(image);
187 displayRow.setAhref(ahref);
188 displayRow.setLastModified(date);
189 displayRow.setSize(size);
190 return displayRow;
191 }
192
193 protected List<DisplayRow> getObjectDisplayRows(
194 final ObjectListing objectListing, final String prefix, final String delimiter) {
195 List<DisplayRow> displayRows = new ArrayList<DisplayRow>();
196 for (S3ObjectSummary summary : objectListing.getObjectSummaries()) {
197 if (isDirectory(summary, objectListing.getCommonPrefixes(), prefix,
198 delimiter)) {
199 continue;
200 }
201 DisplayRow displayRow = getDisplayRow(summary, prefix, delimiter);
202 if (displayRow == null) {
203 continue;
204 }
205 displayRows.add(displayRow);
206 }
207 return displayRows;
208 }
209
210
211
212
213 protected DisplayRow getUpOneDirectoryDisplayRow(final String prefix,
214 final String delimiter) {
215 if (StringUtils.isEmpty(prefix)) {
216 return null;
217 }
218
219
220 String image = "";
221 String show = ".." + delimiter;
222 String dest = getUpOneDirectoryDest(prefix, delimiter);
223 String ahref = html.getHref(dest, show);
224 String date = "";
225 String size = "";
226
227
228 DisplayRow displayRow = new DisplayRow();
229 displayRow.setImage(image);
230 displayRow.setAhref(ahref);
231 displayRow.setLastModified(date);
232 displayRow.setSize(size);
233 return displayRow;
234 }
235
236
237
238
239
240 protected String getUpOneDirectoryDest(String prefix, final String delimiter) {
241 if (prefix.endsWith(delimiter)) {
242 prefix = prefix.substring(0, prefix.length() - 1);
243 }
244 int pos = prefix.lastIndexOf(delimiter);
245 if (pos == -1) {
246 return delimiter + getBrowseHtml();
247 } else {
248 return delimiter + prefix.substring(0, pos + 1);
249 }
250 }
251
252 public void setContext(final S3BucketContext context) {
253 this.context = context;
254 }
255
256 public S3BucketContext getContext() {
257 return context;
258 }
259
260 public String getBrowseHtml() {
261 return browseHtml;
262 }
263
264 public void setBrowseHtml(final String browseHtml) {
265 this.browseHtml = browseHtml;
266 }
267
268 }