Coverage Report - com.jcabi.github.RtContents
 
Classes in this File Line Coverage Branch Coverage Complexity
RtContents
90%
90/99
8%
3/36
1.385
RtContents$1
100%
4/4
N/A
1.385
RtContents$AjcClosure1
0%
0/1
N/A
1.385
RtContents$AjcClosure11
0%
0/1
N/A
1.385
RtContents$AjcClosure13
100%
1/1
N/A
1.385
RtContents$AjcClosure15
100%
1/1
N/A
1.385
RtContents$AjcClosure17
100%
1/1
N/A
1.385
RtContents$AjcClosure19
0%
0/1
N/A
1.385
RtContents$AjcClosure3
100%
1/1
N/A
1.385
RtContents$AjcClosure5
100%
1/1
N/A
1.385
RtContents$AjcClosure7
100%
1/1
N/A
1.385
RtContents$AjcClosure9
100%
1/1
N/A
1.385
 
 1  2
 /**
 2  
  * Copyright (c) 2013-2017, 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.JsonResponse;
 36  
 import com.jcabi.http.response.RestResponse;
 37  
 import java.io.IOException;
 38  
 import java.net.HttpURLConnection;
 39  
 import javax.json.Json;
 40  
 import javax.json.JsonObject;
 41  
 import javax.json.JsonStructure;
 42  
 import javax.json.JsonValue;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github contents.
 47  
  * @author Andres Candal (andres.candal@rollasolution.com)
 48  
  * @version $Id: 28d33353829cb8f1e79cc8df9a87c5ad1f51e7f6 $
 49  
  * @since 0.8
 50  
  * @checkstyle MultipleStringLiteralsCheck (300 lines)
 51  
  */
 52  
 @Immutable
 53  
 @Loggable(Loggable.DEBUG)
 54  0
 @EqualsAndHashCode(of = { "entry", "request", "owner" })
 55  
 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
 56  4
 final class RtContents implements Contents {
 57  
 
 58  
     /**
 59  
      * API entry point.
 60  
      */
 61  
     private final transient Request entry;
 62  
 
 63  
     /**
 64  
      * Repository.
 65  
      */
 66  
     private final transient Repo owner;
 67  
 
 68  
     /**
 69  
      * RESTful request.
 70  
      */
 71  
     private final transient Request request;
 72  
 
 73  
     /**
 74  
      * Public ctor.
 75  
      * @param req RESTful API entry point
 76  
      * @param repo Repository
 77  
      */
 78  10
     public RtContents(final Request req, final Repo repo) {
 79  10
         this.entry = req;
 80  10
         this.owner = repo;
 81  10
         this.request = req.uri()
 82  10
             .path("/repos")
 83  10
             .path(repo.coordinates().user())
 84  10
             .path(repo.coordinates().repo())
 85  10
             .path("/contents")
 86  10
             .back();
 87  10
     }
 88  
 
 89  
     @Override
 90  
     public Repo repo() {
 91  0
         return this.owner;
 92  
     }
 93  
 
 94  
     @Override
 95  
     public Content readme() throws IOException {
 96  3
         return new RtContent(
 97  
             this.entry, this.owner,
 98  1
             this.entry.uri()
 99  1
                 .path("/repos")
 100  1
                 .path(this.owner.coordinates().user())
 101  1
                 .path(this.owner.coordinates().repo())
 102  1
                 .path("/readme")
 103  1
                 .back()
 104  1
                 .method(Request.GET)
 105  1
                 .fetch()
 106  1
                 .as(RestResponse.class)
 107  1
                 .assertStatus(HttpURLConnection.HTTP_OK)
 108  1
                 .as(JsonResponse.class)
 109  1
                 .json().readObject().getString("path")
 110  
         );
 111  
     }
 112  
 
 113  
     @Override
 114  
     public Content readme(
 115  
         final String branch
 116  
     ) throws IOException {
 117  2
         final JsonStructure json = Json.createObjectBuilder()
 118  1
             .add("ref", branch)
 119  1
             .build();
 120  2
         return new RtContent(
 121  
             this.entry, this.owner,
 122  1
             this.entry.uri()
 123  1
                 .path("/repos")
 124  1
                 .path(this.owner.coordinates().user())
 125  1
                 .path(this.owner.coordinates().repo())
 126  1
                 .path("/readme")
 127  1
                 .back()
 128  1
                 .method(Request.GET)
 129  1
                 .body().set(json).back()
 130  1
                 .fetch()
 131  1
                 .as(RestResponse.class)
 132  1
                 .assertStatus(HttpURLConnection.HTTP_OK)
 133  1
                 .as(JsonResponse.class)
 134  1
                 .json().readObject().getString("path")
 135  
         );
 136  
     }
 137  
 
 138  
     @Override
 139  
     public Content create(
 140  
         final JsonObject content
 141  
     )
 142  
         throws IOException {
 143  2
         if (!content.containsKey("path")) {
 144  0
             throw new IllegalStateException(
 145  
                 "Content should have path parameter"
 146  
             );
 147  
         }
 148  1
         final String path = content.getString("path");
 149  2
         return new RtContent(
 150  
             this.entry, this.owner,
 151  1
             this.request.method(Request.PUT)
 152  1
                 .uri().path(path).back()
 153  1
                 .body().set(content).back()
 154  1
                 .fetch()
 155  1
                 .as(RestResponse.class)
 156  1
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 157  1
                 .as(JsonResponse.class)
 158  1
                 .json().readObject().getJsonObject("content").getString("path")
 159  
         );
 160  
     }
 161  
 
 162  
     @Override
 163  
     public Content get(
 164  
         final String path,
 165  
         final String ref
 166  
     ) throws IOException {
 167  6
         return this.content(path, ref);
 168  
     }
 169  
 
 170  
     @Override
 171  
     public Content get(
 172  
         final String path
 173  
     ) throws IOException {
 174  0
         return this.content(path, "master");
 175  
     }
 176  
 
 177  
     @Override
 178  
     public Iterable<Content> iterate(
 179  
         final String path,
 180  
         final String ref
 181  
     ) {
 182  3
         return new RtPagination<Content>(
 183  1
             this.request.method(Request.GET)
 184  1
                 .uri().path(path).queryParam("ref", ref).back(),
 185  3
             new RtValuePagination.Mapping<Content, JsonObject>() {
 186  
                 @Override
 187  
                 public Content map(final JsonObject object) {
 188  4
                     return new RtContent(
 189  2
                         RtContents.this.entry, RtContents.this.owner,
 190  2
                         object.getString("path")
 191  
                     );
 192  
                 };
 193  
             }
 194  
         );
 195  
     }
 196  
 
 197  
     @Override
 198  
     public RepoCommit remove(final JsonObject content
 199  
     )
 200  
         throws IOException {
 201  2
         if (!content.containsKey("path")) {
 202  0
             throw new IllegalStateException(
 203  
                 "Content should have path parameter"
 204  
             );
 205  
         }
 206  1
         final String path = content.getString("path");
 207  2
         return new RtRepoCommit(
 208  
             this.entry,
 209  
             this.owner,
 210  1
             this.request.method(Request.DELETE)
 211  1
                 .uri().path(path).back()
 212  1
                 .body().set(content).back().fetch()
 213  1
                 .as(RestResponse.class)
 214  1
                 .assertStatus(HttpURLConnection.HTTP_OK)
 215  1
                 .as(JsonResponse.class).json()
 216  1
                 .readObject().getJsonObject("commit").getString("sha")
 217  
         );
 218  
     }
 219  
 
 220  
     @Override
 221  
     public RepoCommit update(
 222  
         final String path,
 223  
         final JsonObject json)
 224  
         throws IOException {
 225  3
         return new RtRepoCommit(
 226  
             this.entry,
 227  
             this.owner,
 228  1
             this.request.uri().path(path).back()
 229  1
                 .method(Request.PUT)
 230  1
                 .body().set(json).back()
 231  1
                 .fetch()
 232  1
                 .as(RestResponse.class)
 233  1
                 .assertStatus(HttpURLConnection.HTTP_OK)
 234  1
                 .as(JsonResponse.class).json()
 235  1
                 .readObject().getJsonObject("commit").getString("sha")
 236  
         );
 237  
     }
 238  
 
 239  
     @Override
 240  
     public boolean exists(final String path, final String ref)
 241  
         throws IOException {
 242  0
         final RestResponse response = this.request.method(Request.GET)
 243  0
             .uri().path(path).queryParam("ref", ref).back()
 244  0
             .fetch().as(RestResponse.class);
 245  0
         return response.status() == HttpURLConnection.HTTP_OK;
 246  
     }
 247  
 
 248  
     /**
 249  
      * Get the contents of a file or symbolic link in a repository.
 250  
      * @param path The content path
 251  
      * @param ref The name of the commit/branch/tag.
 252  
      * @return Content fetched
 253  
      * @throws IOException If there is any I/O problem
 254  
      * @see <a href="http://developer.github.com/v3/repos/contents/#get-contents">Get contents</a>
 255  
      */
 256  
     private Content content(
 257  
         final String path, final String ref
 258  
     ) throws IOException {
 259  3
         final String name = "ref";
 260  3
         RtContent content = null;
 261  3
         final JsonStructure structure = this.request.method(Request.GET)
 262  3
                 .uri().path(path).queryParam(name, ref).back()
 263  3
                 .fetch()
 264  3
                 .as(RestResponse.class)
 265  3
                 .assertStatus(HttpURLConnection.HTTP_OK)
 266  3
                 .as(JsonResponse.class)
 267  3
                 .json().read();
 268  3
         if (JsonValue.ValueType.OBJECT.equals(structure.getValueType())) {
 269  3
             content = new RtContent(
 270  3
                     this.entry.uri().queryParam(name, ref).back(), this.owner,
 271  3
                     ((JsonObject) structure).getString("path")
 272  
             );
 273  
         }
 274  3
         return content;
 275  
     }
 276  
 
 277  
 }