Coverage Report - com.jcabi.s3.Ocket
 
Classes in this File Line Coverage Branch Coverage Complexity
Ocket
N/A
N/A
1.042
Ocket$Empty
0%
0/12
0%
0/4
1.042
Ocket$Empty$AjcClosure1
0%
0/1
N/A
1.042
Ocket$Empty$AjcClosure11
0%
0/1
N/A
1.042
Ocket$Empty$AjcClosure13
0%
0/1
N/A
1.042
Ocket$Empty$AjcClosure3
0%
0/1
N/A
1.042
Ocket$Empty$AjcClosure5
0%
0/1
N/A
1.042
Ocket$Empty$AjcClosure7
0%
0/1
N/A
1.042
Ocket$Empty$AjcClosure9
0%
0/1
N/A
1.042
Ocket$Text
59%
16/27
0%
0/12
1.042
Ocket$Text$AjcClosure1
100%
1/1
N/A
1.042
Ocket$Text$AjcClosure11
100%
1/1
N/A
1.042
Ocket$Text$AjcClosure13
0%
0/1
N/A
1.042
Ocket$Text$AjcClosure15
0%
0/1
N/A
1.042
Ocket$Text$AjcClosure17
0%
0/1
N/A
1.042
Ocket$Text$AjcClosure19
0%
0/1
N/A
1.042
Ocket$Text$AjcClosure3
100%
1/1
N/A
1.042
Ocket$Text$AjcClosure5
100%
1/1
N/A
1.042
Ocket$Text$AjcClosure7
0%
0/1
N/A
1.042
Ocket$Text$AjcClosure9
0%
0/1
N/A
1.042
 
 1  0
 /**
 2  
  * Copyright (c) 2012-2015, jcabi.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 jcabi.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.jcabi.s3;
 31  
 
 32  
 import com.amazonaws.services.s3.model.ObjectMetadata;
 33  
 import com.jcabi.aspects.Immutable;
 34  
 import com.jcabi.aspects.Loggable;
 35  
 import java.io.ByteArrayInputStream;
 36  
 import java.io.ByteArrayOutputStream;
 37  
 import java.io.IOException;
 38  
 import java.io.InputStream;
 39  
 import java.io.OutputStream;
 40  
 import lombok.EqualsAndHashCode;
 41  
 import lombok.ToString;
 42  
 import org.apache.commons.io.Charsets;
 43  
 import org.apache.commons.lang3.CharEncoding;
 44  
 
 45  
 /**
 46  
  * Amazon S3 object abstraction.
 47  
  *
 48  
  * <p>You get an instance of this interface from {@link Bucket}, for example:
 49  
  *
 50  
  * <pre> Region region = new Region.Simple(key, secret);
 51  
  * Bucket bucket = region.bucket("my.example.com");
 52  
  * Ocket ocket = bucket.ocket("src/main/README.txt");</pre>
 53  
  *
 54  
  * <p>In order to read and write plain text content in Unicode we recommend
 55  
  * to use {@code Ocket.Text} decorator:
 56  
  *
 57  
  * <pre> Ocket.Text ocket = new Ocket.Smart(
 58  
  *   bucket.ocket("src/main/README.txt")
 59  
  * );
 60  
  * ocket.write("hello, world!", "text/plain");
 61  
  * </pre>
 62  
  *
 63  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 64  
  * @version $Id: 5cda921c79558a997c1c52ddec20d117790f2688 $
 65  
  * @since 0.1
 66  
  */
 67  
 @Immutable
 68  
 @SuppressWarnings("PMD.TooManyMethods")
 69  
 public interface Ocket extends Comparable<Ocket> {
 70  
 
 71  
     /**
 72  
      * Get bucket we're in.
 73  
      * @return Bucket
 74  
      */
 75  
     Bucket bucket();
 76  
 
 77  
     /**
 78  
      * Get object key.
 79  
      * @return Key
 80  
      */
 81  
     String key();
 82  
 
 83  
     /**
 84  
      * Object metadata.
 85  
      *
 86  
      * <p>Throws {@link OcketNotFoundException} if this object
 87  
      * doesn't exist in S3 bucket.</p>
 88  
      *
 89  
      * @return Metadata
 90  
      * @throws IOException If fails
 91  
      */
 92  
     ObjectMetadata meta() throws IOException;
 93  
 
 94  
     /**
 95  
      * Check whether this S3 object exists.
 96  
      * @return TRUE if it exists in S3, FALSE otherwise
 97  
      * @throws IOException If fails
 98  
      * @since 0.4
 99  
      */
 100  
     boolean exists() throws IOException;
 101  
 
 102  
     /**
 103  
      * Read content.
 104  
      *
 105  
      * <p>Throws {@link OcketNotFoundException} if this object
 106  
      * doesn't exist in S3 bucket.</p>
 107  
      *
 108  
      * @param output Where to write
 109  
      * @throws IOException If fails
 110  
      */
 111  
     void read(OutputStream output) throws IOException;
 112  
 
 113  
     /**
 114  
      * Write new content to the object.
 115  
      * @param input Where to get content
 116  
      * @param meta Metadata to save. Should contains input length for large
 117  
      *  object, otherwise multi-part uploads won't be possible.
 118  
      * @throws IOException If fails
 119  
      */
 120  
     void write(InputStream input, ObjectMetadata meta)
 121  
         throws IOException;
 122  
 
 123  
     /**
 124  
      * Unicode text S3 object with supplementary functions.
 125  
      */
 126  0
     @Immutable
 127  0
     @ToString
 128  0
     @EqualsAndHashCode(of = "origin")
 129  
     @Loggable(Loggable.DEBUG)
 130  
     final class Text implements Ocket {
 131  
         /**
 132  
          * Original encapsulated ocket.
 133  
          */
 134  
         private final transient Ocket origin;
 135  
         /**
 136  
          * Public ctor.
 137  
          * @param ocket Original ocket
 138  
          */
 139  12
         public Text(final Ocket ocket) {
 140  12
             this.origin = ocket;
 141  12
         }
 142  
         /**
 143  
          * Read content as string.
 144  
          * @return Content
 145  
          * @throws IOException If fails
 146  
          */
 147  
         public String read() throws IOException {
 148  2
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
 149  1
             this.origin.read(baos);
 150  1
             return baos.toString(CharEncoding.UTF_8);
 151  
         }
 152  
         /**
 153  
          * Write content as string.
 154  
          * @param text Text to write
 155  
          * @throws IOException If fails
 156  
          */
 157  
         public void write(final String text) throws IOException {
 158  18
             this.write(text, "text/plain");
 159  9
         }
 160  
         /**
 161  
          * Write content as string, with a specified content type.
 162  
          * @param text Text to write
 163  
          * @param type Content type
 164  
          * @throws IOException If fails
 165  
          */
 166  
         public void write(final String text, final String type)
 167  
             throws IOException {
 168  18
             final ObjectMetadata meta = new ObjectMetadata();
 169  9
             meta.setContentType(type);
 170  9
             meta.setContentLength((long) text.getBytes(Charsets.UTF_8).length);
 171  9
             meta.setContentEncoding(CharEncoding.UTF_8);
 172  9
             this.origin.write(
 173  
                 new ByteArrayInputStream(text.getBytes(CharEncoding.UTF_8)),
 174  
                 meta
 175  
             );
 176  9
         }
 177  
         @Override
 178  
         public Bucket bucket() {
 179  0
             return this.origin.bucket();
 180  
         }
 181  
         @Override
 182  
         public String key() {
 183  0
             return this.origin.key();
 184  
         }
 185  
         @Override
 186  
         public ObjectMetadata meta() throws IOException {
 187  4
             return this.origin.meta();
 188  
         }
 189  
         @Override
 190  
         public boolean exists() throws IOException {
 191  0
             return this.origin.exists();
 192  
         }
 193  
         @Override
 194  
         public void read(final OutputStream output) throws IOException {
 195  0
             this.origin.read(output);
 196  0
         }
 197  
         @Override
 198  
         public void write(final InputStream input, final ObjectMetadata meta)
 199  
             throws IOException {
 200  0
             this.origin.write(input, meta);
 201  0
         }
 202  
         @Override
 203  
         public int compareTo(final Ocket ocket) {
 204  0
             return this.origin.compareTo(ocket);
 205  
         }
 206  
     }
 207  
 
 208  
     /**
 209  
      * Ocket with no content at all.
 210  
      */
 211  0
     @Immutable
 212  0
     @ToString
 213  0
     @EqualsAndHashCode
 214  
     @Loggable(Loggable.DEBUG)
 215  0
     final class Empty implements Ocket {
 216  
         @Override
 217  
         public Bucket bucket() {
 218  0
             throw new UnsupportedOperationException("#bucket()");
 219  
         }
 220  
         @Override
 221  
         public String key() {
 222  0
             return "empty";
 223  
         }
 224  
         @Override
 225  
         public ObjectMetadata meta() {
 226  0
             return new ObjectMetadata();
 227  
         }
 228  
         @Override
 229  
         public boolean exists() {
 230  0
             return true;
 231  
         }
 232  
         @Override
 233  
         public void read(final OutputStream output) {
 234  
             // nothing
 235  0
         }
 236  
         @Override
 237  
         public void write(final InputStream input, final ObjectMetadata meta) {
 238  
             // nothing
 239  0
         }
 240  
         @Override
 241  
         public int compareTo(final Ocket ocket) {
 242  0
             return 0;
 243  
         }
 244  
     }
 245  
 
 246  
 }