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