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