Coverage Report - com.jcabi.github.RtContent
 
Classes in this File Line Coverage Branch Coverage Complexity
RtContent
81%
13/16
0%
0/22
1
RtContent$AjcClosure1
0%
0/1
N/A
1
RtContent$AjcClosure11
100%
1/1
N/A
1
RtContent$AjcClosure3
100%
1/1
N/A
1
RtContent$AjcClosure5
100%
1/1
N/A
1
RtContent$AjcClosure7
100%
1/1
N/A
1
RtContent$AjcClosure9
100%
1/1
N/A
1
 
 1  6
 /**
 2  
  * Copyright (c) 2013-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.github;
 31  
 
 32  
 import com.jcabi.aspects.Immutable;
 33  
 import com.jcabi.aspects.Loggable;
 34  
 import com.jcabi.http.Request;
 35  
 import com.jcabi.http.response.RestResponse;
 36  
 import java.io.ByteArrayInputStream;
 37  
 import java.io.IOException;
 38  
 import java.io.InputStream;
 39  
 import java.net.HttpURLConnection;
 40  
 import javax.json.JsonObject;
 41  
 import javax.validation.constraints.NotNull;
 42  
 import javax.ws.rs.core.HttpHeaders;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github content.
 47  
  *
 48  
  * @author Carlos Miranda (miranda.cma@gmail.com)
 49  
  * @version $Id$
 50  
  */
 51  0
 @Immutable
 52  
 @Loggable(Loggable.DEBUG)
 53  0
 @EqualsAndHashCode(of = { "location", "request", "owner" })
 54  
 final class RtContent implements Content {
 55  
 
 56  
     /**
 57  
      * RESTful request.
 58  
      */
 59  
     private final transient Request request;
 60  
 
 61  
     /**
 62  
      * Repository we're in.
 63  
      */
 64  
     private final transient Repo owner;
 65  
 
 66  
     /**
 67  
      * Path of this Content.
 68  
      */
 69  
     private final transient String location;
 70  
 
 71  
     /**
 72  
      * Public ctor.
 73  
      * @param req Request
 74  
      * @param repo Repository
 75  
      * @param path Path of the content
 76  
      */
 77  13
     RtContent(final Request req, final Repo repo, final String path) {
 78  13
         final Coordinates coords = repo.coordinates();
 79  13
         this.request = req.uri()
 80  
             .path("/repos")
 81  
             .path(coords.user())
 82  
             .path(coords.repo())
 83  
             .path("/contents")
 84  
             .path(path)
 85  
             .back();
 86  13
         this.owner = repo;
 87  13
         this.location = path;
 88  13
     }
 89  
 
 90  
     @Override
 91  
     @NotNull(message = "repository can't be NULL")
 92  
     public Repo repo() {
 93  0
         return this.owner;
 94  
     }
 95  
 
 96  
     @Override
 97  
     @NotNull(message = "string path can't be NULL")
 98  
     public String path() {
 99  20
         return this.location;
 100  
     }
 101  
 
 102  
     @Override
 103  
     public int compareTo(
 104  
         @NotNull(message = "other can't be NULL") final Content other
 105  
     ) {
 106  6
         return this.path().compareTo(other.path());
 107  
     }
 108  
 
 109  
     @Override
 110  
     @NotNull(message = "JSON can't be NULL")
 111  
     public JsonObject json() throws IOException {
 112  6
         return new RtJson(this.request).fetch();
 113  
     }
 114  
 
 115  
     @Override
 116  
     public void patch(@NotNull(message = "JSON object can't be NULL")
 117  
         final JsonObject json) throws IOException {
 118  2
         new RtJson(this.request).patch(json);
 119  1
     }
 120  
 
 121  
     @Override
 122  
     @NotNull(message = "InputStream can't be NULL")
 123  
     public InputStream raw() throws IOException {
 124  2
         return new ByteArrayInputStream(
 125  
             this.request.reset(HttpHeaders.ACCEPT)
 126  
                 .header(
 127  
                     HttpHeaders.ACCEPT,
 128  
                     "application/vnd.github.v3.raw"
 129  
                 ).fetch()
 130  
                 .as(RestResponse.class)
 131  
                 .assertStatus(HttpURLConnection.HTTP_OK).binary()
 132  
         );
 133  
     }
 134  
 }