Coverage Report - org.kuali.maven.mojo.s3.S3DataConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
S3DataConverter
0%
0/129
0%
0/36
2.368
 
 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  0
         HtmlUtils html = new HtmlUtils();
 19  0
         NumberFormat nf = getNumberFormatInstance();
 20  
         S3BucketContext context;
 21  
         String browseHtml;
 22  
 
 23  
         public S3DataConverter() {
 24  0
                 this(null);
 25  0
         }
 26  
 
 27  
         public S3DataConverter(final S3BucketContext context) {
 28  0
                 super();
 29  0
                 this.context = context;
 30  0
         }
 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  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  
          * 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  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  
          * 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  0
                 for (DisplayRow displayRow : displayRows) {
 61  0
                         addDisplayRow(displayRow, data);
 62  
                 }
 63  0
         }
 64  
 
 65  
         /**
 66  
          * Convert a DisplayRow object to a String[]
 67  
          */
 68  
         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  0
         }
 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  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  
          * 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  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  
                 // Store them in an object
 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  
 
 116  
         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  0
                 }
 126  0
                 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  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
                 for (DisplayRow df : directoryDisplayRows) {
 138  0
                         System.out.println("before=" + df.getShow());
 139  
                 }
 140  0
                 Comparator<DisplayRow> c = new DisplayRowComparator();
 141  0
                 Collections.sort(directoryDisplayRows, c);
 142  0
                 for (DisplayRow df : directoryDisplayRows) {
 143  0
                         System.out.println("after=" + df.getShow());
 144  
                 }
 145  0
                 List<String[]> data = new ArrayList<String[]>();
 146  0
                 addDisplayRow(upOneDirectory, data);
 147  0
                 addDisplayRows(directoryDisplayRows, data);
 148  0
                 addDisplayRows(objectDisplayRows, data);
 149  0
                 return data;
 150  
         }
 151  
 
 152  
         protected boolean isDirectory(final S3ObjectSummary summary, final List<String> commonPrefixes,
 153  
                         final String prefix, final String delimiter) {
 154  0
                 String key = summary.getKey();
 155  0
                 if (key.equals(prefix)) {
 156  0
                         return true;
 157  
                 }
 158  0
                 for (String commonPrefix : commonPrefixes) {
 159  0
                         if (key.equals(commonPrefix)) {
 160  0
                                 return true;
 161  
                         }
 162  0
                         String trimmedPrefix = getTrimmedPrefix(commonPrefix, delimiter);
 163  0
                         if (key.equals(trimmedPrefix)) {
 164  0
                                 return true;
 165  
                         }
 166  0
                 }
 167  0
                 return false;
 168  
         }
 169  
 
 170  
         /**
 171  
          * Convert an S3ObjectSummary into a DisplayRow object for the UI
 172  
          */
 173  
         protected DisplayRow getDisplayRow(final S3ObjectSummary summary, final String prefix, final String delimiter) {
 174  0
                 String key = summary.getKey();
 175  
 
 176  
                 // Create some UI friendly strings
 177  0
                 String image = html.getImage(context.getFileImage());
 178  0
                 String show = getShow(key, prefix);
 179  0
                 String dest = delimiter + key;
 180  0
                 String ahref = html.getHref(dest, show);
 181  0
                 String date = context.getLastModifiedDateFormatter().format(summary.getLastModified());
 182  0
                 String size = nf.format((summary.getSize() / 1024D)) + " KiB";
 183  
 
 184  
                 // Store them in an object
 185  0
                 DisplayRow displayRow = new DisplayRow();
 186  0
                 displayRow.setShow(show);
 187  0
                 displayRow.setImage(image);
 188  0
                 displayRow.setAhref(ahref);
 189  0
                 displayRow.setLastModified(date);
 190  0
                 displayRow.setSize(size);
 191  0
                 return displayRow;
 192  
         }
 193  
 
 194  
         protected List<DisplayRow> getObjectDisplayRows(final ObjectListing objectListing, final String prefix,
 195  
                         final String delimiter) {
 196  0
                 List<DisplayRow> displayRows = new ArrayList<DisplayRow>();
 197  0
                 for (S3ObjectSummary summary : objectListing.getObjectSummaries()) {
 198  0
                         if (isDirectory(summary, objectListing.getCommonPrefixes(), prefix, 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  
          * Convert a commonPrefix into a DisplayRow object for the UI
 212  
          */
 213  
         protected DisplayRow getUpOneDirectoryDisplayRow(final String prefix, final String delimiter) {
 214  0
                 if (StringUtils.isEmpty(prefix)) {
 215  0
                         return null;
 216  
                 }
 217  
 
 218  
                 // Create some UI friendly strings
 219  0
                 String image = "";
 220  0
                 String show = ".." + delimiter;
 221  0
                 String dest = getUpOneDirectoryDest(prefix, delimiter);
 222  0
                 String ahref = html.getHref(dest, show);
 223  0
                 String date = "";
 224  0
                 String size = "";
 225  
 
 226  
                 // Store them in an object
 227  0
                 DisplayRow displayRow = new DisplayRow();
 228  0
                 displayRow.setImage(image);
 229  0
                 displayRow.setAhref(ahref);
 230  0
                 displayRow.setLastModified(date);
 231  0
                 displayRow.setSize(size);
 232  0
                 return displayRow;
 233  
         }
 234  
 
 235  
         /**
 236  
          * If prefix is "foo/" and delimiter is "/" return "/"<br>
 237  
          * If prefix is "foo/bar/" and delimiter is "/" return "foo/"
 238  
          */
 239  
         protected String getUpOneDirectoryDest(String prefix, final String delimiter) {
 240  0
                 if (prefix.endsWith(delimiter)) {
 241  0
                         prefix = prefix.substring(0, prefix.length() - 1);
 242  
                 }
 243  0
                 int pos = prefix.lastIndexOf(delimiter);
 244  0
                 if (pos == -1) {
 245  0
                         return delimiter + getBrowseHtml();
 246  
                 } else {
 247  0
                         return delimiter + prefix.substring(0, pos + 1);
 248  
                 }
 249  
         }
 250  
 
 251  
         public void setContext(final S3BucketContext context) {
 252  0
                 this.context = context;
 253  0
         }
 254  
 
 255  
         public S3BucketContext getContext() {
 256  0
                 return context;
 257  
         }
 258  
 
 259  
         public String getBrowseHtml() {
 260  0
                 return browseHtml;
 261  
         }
 262  
 
 263  
         public void setBrowseHtml(final String browseHtml) {
 264  0
                 this.browseHtml = browseHtml;
 265  0
         }
 266  
 
 267  
 }