Coverage Report - com.jcabi.github.RtContents
 
Classes in this File Line Coverage Branch Coverage Complexity
RtContents
75%
21/28
7%
2/28
1.308
RtContents$1
100%
2/2
N/A
1.308
RtContents$AjcClosure1
0%
0/1
N/A
1.308
RtContents$AjcClosure11
0%
0/1
N/A
1.308
RtContents$AjcClosure13
100%
1/1
N/A
1.308
RtContents$AjcClosure15
100%
1/1
N/A
1.308
RtContents$AjcClosure17
100%
1/1
N/A
1.308
RtContents$AjcClosure19
0%
0/1
N/A
1.308
RtContents$AjcClosure3
100%
1/1
N/A
1.308
RtContents$AjcClosure5
100%
1/1
N/A
1.308
RtContents$AjcClosure7
100%
1/1
N/A
1.308
RtContents$AjcClosure9
100%
1/1
N/A
1.308
 
 1  2
 /**
 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.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.validation.constraints.NotNull;
 43  
 import lombok.EqualsAndHashCode;
 44  
 
 45  
 /**
 46  
  * Github contents.
 47  
  * @author Andres Candal (andres.candal@rollasolution.com)
 48  
  * @version $Id$
 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  
             .path("/repos")
 83  
             .path(repo.coordinates().user())
 84  
             .path(repo.coordinates().repo())
 85  
             .path("/contents")
 86  
             .back();
 87  10
     }
 88  
 
 89  
     @Override
 90  
     @NotNull(message = "repository can't be NULL")
 91  
     public Repo repo() {
 92  0
         return this.owner;
 93  
     }
 94  
 
 95  
     @Override
 96  
     @NotNull(message = "Content can't be NULL")
 97  
     public Content readme() throws IOException {
 98  2
         return new RtContent(
 99  
             this.entry, this.owner,
 100  
             this.entry.uri()
 101  
                 .path("/repos")
 102  
                 .path(this.owner.coordinates().user())
 103  
                 .path(this.owner.coordinates().repo())
 104  
                 .path("/readme")
 105  
                 .back()
 106  
                 .method(Request.GET)
 107  
                 .fetch()
 108  
                 .as(RestResponse.class)
 109  
                 .assertStatus(HttpURLConnection.HTTP_OK)
 110  
                 .as(JsonResponse.class)
 111  
                 .json().readObject().getString("path")
 112  
         );
 113  
     }
 114  
 
 115  
     @Override
 116  
     @NotNull(message = "Content can't be NULL")
 117  
     public Content readme(
 118  
         @NotNull(message = "branch can't be NULL") final String branch
 119  
     ) throws IOException {
 120  2
         final JsonStructure json = Json.createObjectBuilder()
 121  
             .add("ref", branch)
 122  
             .build();
 123  1
         return new RtContent(
 124  
             this.entry, this.owner,
 125  
             this.entry.uri()
 126  
                 .path("/repos")
 127  
                 .path(this.owner.coordinates().user())
 128  
                 .path(this.owner.coordinates().repo())
 129  
                 .path("/readme")
 130  
                 .back()
 131  
                 .method(Request.GET)
 132  
                 .body().set(json).back()
 133  
                 .fetch()
 134  
                 .as(RestResponse.class)
 135  
                 .assertStatus(HttpURLConnection.HTTP_OK)
 136  
                 .as(JsonResponse.class)
 137  
                 .json().readObject().getString("path")
 138  
         );
 139  
     }
 140  
 
 141  
     @Override
 142  
     @NotNull(message = "Content can't be NULL")
 143  
     public Content create(
 144  
         @NotNull(message = "JSON can't be NULL") final JsonObject content
 145  
     )
 146  
         throws IOException {
 147  2
         if (!content.containsKey("path")) {
 148  0
             throw new IllegalStateException(
 149  
                 "Content should have path parameter"
 150  
             );
 151  
         }
 152  1
         final String path = content.getString("path");
 153  1
         return new RtContent(
 154  
             this.entry, this.owner,
 155  
             this.request.method(Request.PUT)
 156  
                 .uri().path(path).back()
 157  
                 .body().set(content).back()
 158  
                 .fetch()
 159  
                 .as(RestResponse.class)
 160  
                 .assertStatus(HttpURLConnection.HTTP_CREATED)
 161  
                 .as(JsonResponse.class)
 162  
                 .json().readObject().getJsonObject("content").getString("path")
 163  
         );
 164  
     }
 165  
 
 166  
     @Override
 167  
     @NotNull(message = "Content can't be NULL")
 168  
     public Content get(
 169  
         @NotNull(message = "path can't be NULL") final String path,
 170  
         @NotNull(message = "ref can't be NULL") final String ref
 171  
     ) throws IOException {
 172  6
         return this.content(path, ref);
 173  
     }
 174  
 
 175  
     @Override
 176  
     @NotNull(message = "Content can't be NULL")
 177  
     public Content get(
 178  
         @NotNull(message = "path can't be NULL") final String path
 179  
     ) throws IOException {
 180  0
         return this.content(path, "master");
 181  
     }
 182  
 
 183  
     @Override
 184  
     @NotNull(message = "Iterable of Content can't be NULL")
 185  
     public Iterable<Content> iterate(
 186  
         @NotNull(message = "path can't be NULL") final String path,
 187  
         @NotNull(message = "ref can't be NULL") final String ref
 188  
     ) {
 189  2
         return new RtPagination<Content>(
 190  
             this.request.method(Request.GET)
 191  
                 .uri().path(path).queryParam("ref", ref).back(),
 192  3
             new RtValuePagination.Mapping<Content, JsonObject>() {
 193  
                 @Override
 194  
                 public Content map(final JsonObject object) {
 195  2
                     return new RtContent(
 196  
                         RtContents.this.entry, RtContents.this.owner,
 197  
                         object.getString("path")
 198  
                     );
 199  
                 };
 200  
             }
 201  
         );
 202  
     }
 203  
 
 204  
     @Override
 205  
     @NotNull(message = "Repo commit is never NULL")
 206  
     public RepoCommit remove(@NotNull(message = "content can't be NULL")
 207  
         final JsonObject content
 208  
     )
 209  
         throws IOException {
 210  2
         if (!content.containsKey("path")) {
 211  0
             throw new IllegalStateException(
 212  
                 "Content should have path parameter"
 213  
             );
 214  
         }
 215  1
         final String path = content.getString("path");
 216  1
         return new RtRepoCommit(
 217  
             this.entry,
 218  
             this.owner,
 219  
             this.request.method(Request.DELETE)
 220  
                 .uri().path(path).back()
 221  
                 .body().set(content).back().fetch()
 222  
                 .as(RestResponse.class)
 223  
                 .assertStatus(HttpURLConnection.HTTP_OK)
 224  
                 .as(JsonResponse.class).json()
 225  
                 .readObject().getJsonObject("commit").getString("sha")
 226  
         );
 227  
     }
 228  
 
 229  
     @Override
 230  
     @NotNull(message = "RepoCommit can't be NULL")
 231  
     public RepoCommit update(
 232  
         @NotNull(message = "path is never NULL") final String path,
 233  
         @NotNull(message = "json is never NULL") final JsonObject json)
 234  
         throws IOException {
 235  2
         return new RtRepoCommit(
 236  
             this.entry,
 237  
             this.owner,
 238  
             this.request.uri().path(path).back()
 239  
                 .method(Request.PUT)
 240  
                 .body().set(json).back()
 241  
                 .fetch()
 242  
                 .as(RestResponse.class)
 243  
                 .assertStatus(HttpURLConnection.HTTP_OK)
 244  
                 .as(JsonResponse.class).json()
 245  
                 .readObject().getJsonObject("commit").getString("sha")
 246  
         );
 247  
     }
 248  
 
 249  
     @Override
 250  
     public boolean exists(final String path, final String ref)
 251  
         throws IOException {
 252  0
         final RestResponse response = this.request.method(Request.GET)
 253  
             .uri().path(path).queryParam("ref", ref).back()
 254  
             .fetch().as(RestResponse.class);
 255  0
         return response.status() == HttpURLConnection.HTTP_OK;
 256  
     }
 257  
 
 258  
     /**
 259  
      * Get the contents of a file or symbolic link in a repository.
 260  
      * @param path The content path
 261  
      * @param ref The name of the commit/branch/tag.
 262  
      * @return Content fetched
 263  
      * @throws IOException If there is any I/O problem
 264  
      * @see <a href="http://developer.github.com/v3/repos/contents/#get-contents">Get contents</a>
 265  
      */
 266  
     private Content content(
 267  
         final String path, final String ref
 268  
     ) throws IOException {
 269  3
         final String name = "ref";
 270  3
         return new RtContent(
 271  
             this.entry.uri().queryParam(name, ref).back(), this.owner,
 272  
             this.request.method(Request.GET)
 273  
                 .uri().path(path).queryParam(name, ref).back()
 274  
                 .fetch()
 275  
                 .as(RestResponse.class)
 276  
                 .assertStatus(HttpURLConnection.HTTP_OK)
 277  
                 .as(JsonResponse.class)
 278  
                 .json().readObject().getString("path")
 279  
         );
 280  
     }
 281  
 
 282  
 }