View Javadoc

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