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.ListVersionsRequest;
34  import com.amazonaws.services.s3.model.S3VersionSummary;
35  import com.amazonaws.services.s3.model.VersionListing;
36  import com.google.common.collect.ImmutableList;
37  import com.google.common.collect.ImmutableSet;
38  import com.jcabi.aspects.Immutable;
39  import com.jcabi.aspects.Loggable;
40  import com.jcabi.xml.XMLDocument;
41  import com.jcabi.xml.XSL;
42  import com.jcabi.xml.XSLDocument;
43  import java.io.IOException;
44  import java.io.OutputStream;
45  import java.net.HttpURLConnection;
46  import java.util.Collection;
47  import java.util.Date;
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 S3 Object Version Listing.
60   * @author Carlos Miranda (miranda.cma@gmail.com)
61   * @version $Id: 0685f26b0b9213e32023695b065a1b1f37e4980e $
62   */
63  @Immutable
64  @ToString
65  @EqualsAndHashCode(of = "content")
66  @Loggable(Loggable.DEBUG)
67  final class ObjectVersionListing implements Resource {
68  
69      /**
70       * The STYLESHEET used for transforming the output.
71       */
72      private static final XSL STYLESHEET = XSLDocument.make(
73          ObjectVersionListing.class.getResourceAsStream("versions.xsl")
74      );
75  
76      /**
77       * Byte representation of XML data.
78       */
79      @Immutable.Array
80      private final transient byte[] content;
81  
82      /**
83       * Public constructor.
84       * @param client Amazon S3 client
85       * @param bucket Bucket name
86       * @param key The S3 object key
87       */
88      ObjectVersionListing(@NotNull final AmazonS3 client,
89          @NotNull final String bucket, @NotNull final String key) {
90          VersionListing listing = client.listVersions(
91              new ListVersionsRequest().withPrefix(key).withBucketName(bucket)
92          );
93          final ImmutableList.Builder<S3VersionSummary> versions =
94              ImmutableList.builder();
95          versions.addAll(listing.getVersionSummaries());
96          while (listing.isTruncated()) {
97              listing = client.listNextBatchOfVersions(listing);
98              versions.addAll(listing.getVersionSummaries());
99          }
100         // @checkstyle LineLength (2 lines)
101         final Directives dirs = new Directives()
102             .add("versions").attr("object", key);
103         for (final S3VersionSummary version : versions.build()) {
104             dirs.add("version")
105                 .attr("key", version.getKey())
106                 .set(version.getVersionId()).up();
107         }
108         try {
109             this.content = ObjectVersionListing.STYLESHEET.transform(
110                 new XMLDocument(new Xembler(dirs).xml())
111             ).toString().getBytes(Charsets.UTF_8);
112         } catch (final ImpossibleModificationException ex) {
113             throw new IllegalStateException(
114                 "Unable to generate version listing", ex
115             );
116         }
117     }
118 
119     @Override
120     public int status() {
121         return HttpURLConnection.HTTP_OK;
122     }
123 
124     @Override
125     public long writeTo(final OutputStream stream) throws IOException {
126         stream.write(this.content);
127         return this.content.length;
128     }
129 
130     @Override
131     public Collection<String> headers() throws IOException {
132         final ImmutableSet.Builder<String> headers = ImmutableSet.builder();
133         headers.add(
134             ObjectVersionListing.header(
135                 HttpHeaders.CONTENT_TYPE,
136                 this.contentType()
137             )
138         );
139         headers.add(
140             ObjectVersionListing.header(
141                 HttpHeaders.CONTENT_LENGTH,
142                 String.valueOf(this.content.length)
143             )
144         );
145         return headers.build();
146     }
147 
148     @Override
149     public String etag() {
150         final CRC32 crc = new CRC32();
151         crc.update(this.content);
152         return Long.toHexString(crc.getValue());
153     }
154     @Override
155     public Date lastModified() {
156         return new Date();
157     }
158 
159     @Override
160     public String contentType() {
161         return "application/xhtml+xml";
162     }
163 
164     @Override
165     public void close() {
166         // nothing to do
167     }
168 
169     /**
170      * Create a HTTP header from name and value.
171      * @param name Name of the header
172      * @param value The value
173      * @return Full HTTP header string
174      */
175     @NotNull
176     private static String header(@NotNull final String name,
177         @NotNull final String value) {
178         return String.format("%s: %s", name, value);
179     }
180 }