Coverage Report - com.jcabi.s3.AwsBucket
 
Classes in this File Line Coverage Branch Coverage Complexity
AwsBucket
51%
15/29
0%
0/20
1.4
AwsBucket$1
100%
2/2
N/A
1.4
AwsBucket$AjcClosure1
0%
0/1
N/A
1.4
AwsBucket$AjcClosure11
100%
1/1
N/A
1.4
AwsBucket$AjcClosure13
0%
0/1
N/A
1.4
AwsBucket$AjcClosure3
0%
0/1
N/A
1.4
AwsBucket$AjcClosure5
100%
1/1
N/A
1.4
AwsBucket$AjcClosure7
100%
1/1
N/A
1.4
AwsBucket$AjcClosure9
0%
0/1
N/A
1.4
 
 1  2
 /**
 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.AmazonServiceException;
 33  
 import com.amazonaws.services.s3.AmazonS3;
 34  
 import com.amazonaws.services.s3.model.DeleteObjectRequest;
 35  
 import com.jcabi.aspects.Immutable;
 36  
 import com.jcabi.aspects.Loggable;
 37  
 import com.jcabi.log.Logger;
 38  
 import java.io.IOException;
 39  
 import java.util.Iterator;
 40  
 import lombok.EqualsAndHashCode;
 41  
 
 42  
 /**
 43  
  * Amazon S3 bucket.
 44  
  *
 45  
  * @author Yegor Bugayenko (yegor@tpc2.com)
 46  
  * @version $Id: 350807e1a07a734df18410a409174af52cbcfd15 $
 47  
  * @since 0.1
 48  
  */
 49  0
 @Immutable
 50  0
 @EqualsAndHashCode(of = { "regn", "bkt" })
 51  
 @Loggable(Loggable.DEBUG)
 52  
 @SuppressWarnings("PMD.TooManyMethods")
 53  2
 final class AwsBucket implements Bucket {
 54  
 
 55  
     /**
 56  
      * Region we're in.
 57  
      */
 58  
     private final transient Region regn;
 59  
 
 60  
     /**
 61  
      * Bucket name.
 62  
      */
 63  
     private final transient String bkt;
 64  
 
 65  
     /**
 66  
      * Public ctor.
 67  
      * @param reg Region we're in
 68  
      * @param name Bucket name
 69  
      */
 70  5
     AwsBucket(final Region reg, final String name) {
 71  5
         this.regn = reg;
 72  5
         this.bkt = name;
 73  5
     }
 74  
 
 75  
     @Override
 76  
     public String toString() {
 77  0
         return this.bkt;
 78  
     }
 79  
 
 80  
     @Override
 81  
     public Region region() {
 82  0
         return this.regn;
 83  
     }
 84  
 
 85  
     @Override
 86  
     public String name() {
 87  0
         return this.bkt;
 88  
     }
 89  
 
 90  
     @Override
 91  
     public Ocket ocket(final String key) {
 92  2
         return new AwsOcket(this, key);
 93  
     }
 94  
 
 95  
     @Override
 96  
     public boolean exists() throws IOException {
 97  6
         final AmazonS3 aws = this.regn.aws();
 98  
         final boolean result;
 99  
         try {
 100  3
             result = aws.doesBucketExist(this.bkt);
 101  1
         } catch (final AmazonServiceException ex) {
 102  1
             throw new IOException(
 103  
                 String.format(
 104  
                     "failed to check existence of '%s' bucket",
 105  
                     this.bkt
 106  
                 ),
 107  
                 ex
 108  
             );
 109  2
         }
 110  2
         Logger.debug(this, "Does bucket '%s' exist? %b", this.bkt, result);
 111  2
         return result;
 112  
     }
 113  
 
 114  
     @Override
 115  
     public void remove(final String key) throws IOException {
 116  
         try {
 117  0
             final AmazonS3 aws = this.regn.aws();
 118  0
             final long start = System.currentTimeMillis();
 119  0
             aws.deleteObject(new DeleteObjectRequest(this.bkt, key));
 120  0
             Logger.info(
 121  
                 this,
 122  
                 "ocket '%s' removed in bucket '%s' in %[ms]s",
 123  
                 key, this.bkt, System.currentTimeMillis() - start
 124  
             );
 125  0
         } catch (final AmazonServiceException ex) {
 126  0
             throw new IOException(
 127  
                 String.format(
 128  
                     "failed to remove '%s' bucket",
 129  
                     this.bkt
 130  
                 ),
 131  
                 ex
 132  
             );
 133  0
         }
 134  0
     }
 135  
 
 136  
     @Override
 137  
     public Iterable<String> list(final String pfx) {
 138  2
         return new Iterable<String>() {
 139  
             @Override
 140  
             public Iterator<String> iterator() {
 141  1
                 return new AwsListIterator(
 142  
                     AwsBucket.this.regn, AwsBucket.this.bkt, pfx
 143  
                 );
 144  
             }
 145  
         };
 146  
     }
 147  
 
 148  
     @Override
 149  
     public int compareTo(final Bucket bucket) {
 150  0
         return this.name().compareTo(bucket.name());
 151  
     }
 152  
 
 153  
 }
 154