View Javadoc
1   /**
2    * Copyright (c) 2012-2017, s3auth.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the s3auth.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.s3auth.hosts;
31  
32  import com.amazonaws.services.s3.AmazonS3;
33  import com.amazonaws.services.s3.model.ListObjectsRequest;
34  import com.amazonaws.services.s3.model.ObjectListing;
35  import com.amazonaws.services.s3.model.S3ObjectSummary;
36  import com.google.common.collect.ImmutableSet;
37  import com.jcabi.aspects.Immutable;
38  import com.jcabi.aspects.Loggable;
39  import com.jcabi.xml.XMLDocument;
40  import com.jcabi.xml.XSL;
41  import com.jcabi.xml.XSLDocument;
42  import java.io.IOException;
43  import java.io.OutputStream;
44  import java.net.HttpURLConnection;
45  import java.util.Collection;
46  import java.util.Date;
47  import java.util.LinkedList;
48  import java.util.zip.CRC32;
49  import javax.validation.constraints.NotNull;
50  import javax.ws.rs.core.HttpHeaders;
51  import lombok.EqualsAndHashCode;
52  import lombok.ToString;
53  import org.apache.commons.io.Charsets;
54  import org.xembly.Directives;
55  import org.xembly.ImpossibleModificationException;
56  import org.xembly.Xembler;
57  
58  /**
59   * XML Directory Listing.
60   * @author Carlos Miranda (miranda.cma@gmail.com)
61   * @version $Id: f34411d24d8092c2c941571028c1c7c1acde27ad $
62   * @checkstyle ClassDataAbstraction (200 lines)
63   */
64  @Immutable
65  @ToString
66  @EqualsAndHashCode(of = "content")
67  @Loggable(Loggable.DEBUG)
68  final class DirectoryListing implements Resource {
69  
70      /**
71       * The STYLESHEET used for transforming the output.
72       */
73      private static final XSL STYLESHEET = XSLDocument.make(
74          DirectoryListing.class.getResourceAsStream("directory.xsl")
75      );
76  
77      /**
78       * Byte representation of transformed data.
79       */
80      @Immutable.Array
81      private final transient byte[] content;
82  
83      /**
84       * Public constructor.
85       * @param client Amazon S3 client
86       * @param bucket Bucket name
87       * @param key The S3 object key
88       */
89      DirectoryListing(@NotNull final AmazonS3 client,
90          @NotNull final String bucket, @NotNull final String key) {
91          ObjectListing listing = client.listObjects(
92              new ListObjectsRequest().withDelimiter("/").withPrefix(key)
93                  .withBucketName(bucket)
94          );
95          final Collection<S3ObjectSummary> objects =
96              new LinkedList<S3ObjectSummary>();
97          objects.addAll(listing.getObjectSummaries());
98          while (listing.isTruncated()) {
99              listing = client.listNextBatchOfObjects(listing);
100             objects.addAll(listing.getObjectSummaries());
101         }
102         // @checkstyle LineLength (2 lines)
103         final Directives dirs = new Directives()
104             .add("directory").attr("prefix", key);
105         for (final String prefix : listing.getCommonPrefixes()) {
106             dirs.add("commonPrefix").set(prefix).up();
107         }
108         for (final S3ObjectSummary object : objects) {
109             dirs.add("object")
110                 .add("path")
111                 .set(object.getKey()).up()
112                 .add("size")
113                 .set(Long.toString(object.getSize())).up()
114                 .up();
115         }
116         try {
117             this.content = DirectoryListing.STYLESHEET.transform(
118                 new XMLDocument(new Xembler(dirs).xml())
119             ).toString().getBytes(Charsets.UTF_8);
120         } catch (final ImpossibleModificationException ex) {
121             throw new IllegalStateException(
122                 "Unable to generate directory listing", ex
123             );
124         }
125     }
126 
127     @Override
128     public int status() {
129         return HttpURLConnection.HTTP_OK;
130     }
131 
132     @Override
133     public long writeTo(final OutputStream stream) throws IOException {
134         stream.write(this.content);
135         return (long) this.content.length;
136     }
137 
138     @Override
139     public Collection<String> headers() {
140         final ImmutableSet.Builder<String> headers = ImmutableSet.builder();
141         headers.add(
142             DirectoryListing.header(
143                 HttpHeaders.CONTENT_TYPE,
144                 this.contentType()
145             )
146         );
147         headers.add(
148             DirectoryListing.header(
149                 HttpHeaders.CONTENT_LENGTH,
150                 String.valueOf(this.content.length)
151             )
152         );
153         return headers.build();
154     }
155 
156     @Override
157     public String etag() {
158         final CRC32 crc = new CRC32();
159         crc.update(this.content);
160         return Long.toHexString(crc.getValue());
161     }
162     @Override
163     public Date lastModified() {
164         return new Date();
165     }
166 
167     @Override
168     public String contentType() {
169         return "text/html";
170     }
171 
172     @Override
173     public void close() {
174         // nothing to do
175     }
176 
177     /**
178      * Create a HTTP header from name and value.
179      * @param name Name of the header
180      * @param value The value
181      * @return Full HTTP header string
182      */
183     @NotNull
184     private static String header(@NotNull final String name,
185         @NotNull final String value) {
186         return String.format("%s: %s", name, value);
187     }
188 }