1 /**
2 * Copyright 2004-2014 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.kuali.common.aws.cloudfront;
17
18 import java.text.SimpleDateFormat;
19 import java.util.List;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.kuali.common.util.Counter;
23 import org.kuali.common.util.Str;
24 import org.kuali.common.util.html.HtmlUtils;
25
26 import com.amazonaws.services.s3.model.S3ObjectSummary;
27
28 /**
29 * Convert information from an S3 bucket into pojo's
30 */
31 public class DefaultListingConverterService implements ListingConverterService {
32
33 /**
34 * Convert "foo/bar/css/" into "foo/bar/css"<br>
35 * Convert "foo/bar/css" into "foo/bar"<br>
36 */
37 protected String getTrimmedPrefix(String prefix, String delimiter) {
38 return Str.removeSuffix(prefix, delimiter);
39 }
40
41 /**
42 * Convert each DisplayRow object in the list to a String[] and add the String[] to the list of data
43 */
44 protected void addDisplayRows(List<DisplayRow> displayRows, List<String[]> data) {
45 for (DisplayRow displayRow : displayRows) {
46 addDisplayRow(displayRow, data);
47 }
48 }
49
50 /**
51 * Convert a DisplayRow object to a String[]
52 */
53 protected void addDisplayRow(DisplayRow displayRow, List<String[]> data) {
54 if (displayRow == null) {
55 return;
56 }
57 String[] row = new String[4];
58 row[0] = displayRow.getImage();
59 row[1] = displayRow.getAhref();
60 row[2] = displayRow.getLastModified();
61 row[3] = displayRow.getSize();
62 data.add(row);
63 }
64
65 /**
66 * Trim the prefix off of the text we display for this object.<br>
67 * Display "style.css" instead of "css/style.css"
68 */
69 protected String getShow(String key, String prefix) {
70 if (prefix == null) {
71 return key;
72 }
73 int index = prefix.length();
74 String s = key.substring(index);
75 return s;
76 }
77
78 /**
79 * Convert a commonPrefix into a DisplayRow object for the UI
80 */
81 protected DisplayRow getDisplayRow(String commonPrefix, IndexDataContext context, Counter indent) {
82 /**
83 * // Create some UI friendly strings String image = HtmlUtils.getImage(context.getConverterContext().getDirImage(), indent); String show = getShow(commonPrefix,
84 * context.getListing().getPrefix()); String dest = context.getBucketContext().getDelimiter() + commonPrefix; String ahref = HtmlUtils.getHref(dest, show, indent); String
85 * date = "-"; String size = "-";
86 *
87 * // Store them in an object DisplayRow displayRow = new DisplayRow(); displayRow.setImage(image); displayRow.setAhref(ahref); displayRow.setLastModified(date);
88 * displayRow.setSize(size); displayRow.setShow(show); return displayRow;
89 **/
90 return null;
91 }
92
93 protected List<DisplayRow> getDirectoryDisplayRows(IndexDataContext context, Counter indent) {
94 /**
95 * List<DisplayRow> displayRows = new ArrayList<DisplayRow>(); for (String commonPrefix : context.getListing().getCommonPrefixes()) { DisplayRow displayRow =
96 * getDisplayRow(commonPrefix, context, indent); if (displayRow == null) { continue; } displayRows.add(displayRow); } return displayRows;
97 **/
98 return null;
99 }
100
101 @Override
102 public List<String[]> getIndexData(IndexDataContext context) {
103 /**
104 * ListingConverterContext lcc = context.getConverterContext(); ObjectListing listing = context.getListing();
105 *
106 * SimpleDateFormat formatter = CloudFrontUtils.getSimpleDateFormat(lcc.getDateDisplayFormat(), lcc.getDateDisplayTimeZone()); Counter indent = new Counter(); DisplayRow
107 * upOneDirectory = getUpOneDirectoryDisplayRow(context, listing.getPrefix(), indent); List<DisplayRow> objectDisplayRows = getObjectDisplayRows(context, indent,
108 * formatter); List<DisplayRow> directoryDisplayRows = getDirectoryDisplayRows(context, indent); Comparator<DisplayRow> comparator = new DisplayRowComparator();
109 * Collections.sort(directoryDisplayRows, comparator); List<String[]> indexData = new ArrayList<String[]>(); addDisplayRow(upOneDirectory, indexData);
110 * addDisplayRows(directoryDisplayRows, indexData); addDisplayRows(objectDisplayRows, indexData); return indexData;
111 **/
112 return null;
113 }
114
115 protected boolean isDirectory(S3ObjectSummary summary, List<String> commonPrefixes, String prefix, String delimiter) {
116 String key = summary.getKey();
117 if (StringUtils.equals(key, prefix)) {
118 return true;
119 }
120 for (String commonPrefix : commonPrefixes) {
121 if (StringUtils.equals(key, commonPrefix)) {
122 return true;
123 }
124 String trimmedPrefix = Str.removeSuffix(commonPrefix, delimiter);
125 if (StringUtils.equals(key, trimmedPrefix)) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 /**
133 * Convert an S3ObjectSummary into a DisplayRow object for the UI
134 */
135 protected DisplayRow getDisplayRow(IndexDataContext context, S3ObjectSummary summary, Counter indent, SimpleDateFormat formatter) {
136 /**
137
138 String delimiter = context.getBucketContext().getDelimiter();
139
140 String key = summary.getKey();
141
142 // Create some UI friendly strings
143 String image = HtmlUtils.getImage(context.getConverterContext().getFileImage(), indent);
144 String show = getShow(key, context.getListing().getPrefix());
145 String dest = delimiter + key;
146 String ahref = HtmlUtils.getHref(dest, show, indent);
147 String date = formatter.format(summary.getLastModified());
148 String size = FormatUtils.getSize(summary.getSize());
149
150 // Store them in an object
151 DisplayRow displayRow = new DisplayRow();
152 displayRow.setShow(show);
153 displayRow.setImage(image);
154 displayRow.setAhref(ahref);
155 displayRow.setLastModified(date);
156 displayRow.setSize(size);
157 return displayRow;
158 **/
159 return null;
160 }
161
162 protected List<DisplayRow> getObjectDisplayRows(IndexDataContext context, Counter indent, SimpleDateFormat formatter) {
163 /**
164 String delimiter = context.getBucketContext().getDelimiter();
165 ObjectListing listing = context.getListing();
166 List<DisplayRow> displayRows = new ArrayList<DisplayRow>();
167 for (S3ObjectSummary summary : listing.getObjectSummaries()) {
168 if (isDirectory(summary, listing.getCommonPrefixes(), listing.getPrefix(), delimiter)) {
169 continue;
170 }
171 DisplayRow displayRow = getDisplayRow(context, summary, indent, formatter);
172 if (displayRow == null) {
173 continue;
174 }
175 displayRows.add(displayRow);
176 }
177 return displayRows;
178 **/
179 return null;
180 }
181
182 /**
183 * Convert a commonPrefix into a DisplayRow object for the UI
184 */
185 protected DisplayRow getUpOneDirectoryDisplayRow(IndexDataContext context, String prefix, Counter indent) {
186
187 String delimiter = context.getBucketContext().getDelimiter();
188 String browseKey = context.getConverterContext().getBrowseKey();
189
190 if (StringUtils.isEmpty(prefix)) {
191 return null;
192 }
193
194 // Create some UI friendly strings
195 String image = "";
196 String show = ".." + delimiter;
197 String dest = getUpOneDirectoryDest(prefix, delimiter, browseKey);
198 String ahref = HtmlUtils.getHref(dest, show, indent);
199 String date = "";
200 String size = "";
201
202 // Store them in an object
203 DisplayRow displayRow = new DisplayRow();
204 displayRow.setImage(image);
205 displayRow.setAhref(ahref);
206 displayRow.setLastModified(date);
207 displayRow.setSize(size);
208 return displayRow;
209 }
210
211 /**
212 * If prefix is "foo/" and delimiter is "/" return "/"<br>
213 * If prefix is "foo/bar/" and delimiter is "/" return "foo/"
214 */
215 protected String getUpOneDirectoryDest(String prefix, String delimiter, String browseKey) {
216 if (prefix.endsWith(delimiter)) {
217 prefix = prefix.substring(0, prefix.length() - 1);
218 }
219 int pos = prefix.lastIndexOf(delimiter);
220 if (pos == -1) {
221 return delimiter + browseKey;
222 } else {
223 return delimiter + prefix.substring(0, pos + 1);
224 }
225 }
226
227 }