1 /** 2 * Copyright (c) 2013-2023, 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 java.io.IOException; 35 import java.io.InputStream; 36 import java.net.URL; 37 import javax.json.JsonObject; 38 import javax.xml.bind.DatatypeConverter; 39 import lombok.EqualsAndHashCode; 40 import lombok.ToString; 41 42 /** 43 * Github content. 44 * 45 * @author Andres Candal (andres.candal@rollasolution.com) 46 * @version $Id: 5e32a2c66bba916faff87237b3734640691951ef $ 47 * @since 0.8 48 * @see <a href="https://developer.github.com/v3/repos/contents/">Contents API</a> 49 */ 50 @Immutable 51 @SuppressWarnings("PMD.TooManyMethods") 52 public interface Content extends Comparable<Content>, 53 JsonReadable, JsonPatchable { 54 55 /** 56 * Repository we're in. 57 * @return Repo 58 */ 59 Repo repo(); 60 61 /** 62 * Get its path name. 63 * @return The path name 64 */ 65 String path(); 66 67 /** 68 * Get the raw contents. 69 * @throws IOException If an IO error occurs 70 * @return Input stream of the raw content 71 */ 72 InputStream raw() throws IOException; 73 74 /** 75 * Smart Content with extra features. 76 */ 77 @Immutable 78 @ToString 79 @Loggable(Loggable.DEBUG) 80 @EqualsAndHashCode(of = { "content", "jsn" }) 81 final class Smart implements Content { 82 /** 83 * Encapsulated content. 84 */ 85 private final transient Content content; 86 /** 87 * SmartJson object for convenient JSON parsing. 88 */ 89 private final transient SmartJson jsn; 90 /** 91 * Public ctor. 92 * @param cont Content 93 */ 94 public Smart( 95 final Content cont) { 96 this.content = cont; 97 this.jsn = new SmartJson(cont); 98 } 99 /** 100 * Get its name. 101 * @return Name of content 102 * @throws IOException If there is any I/O problem 103 */ 104 public String name() throws IOException { 105 return this.jsn.text("name"); 106 } 107 /** 108 * Get its type. 109 * @return Type of content 110 * @throws IOException If there is any I/O problem 111 */ 112 public String type() throws IOException { 113 return this.jsn.text("type"); 114 } 115 /** 116 * Get its size. 117 * @return Size content 118 * @throws IOException If it fails 119 */ 120 public int size() throws IOException { 121 return this.jsn.number("size"); 122 } 123 /** 124 * Get its sha hash. 125 * @return Sha hash of content 126 * @throws IOException If there is any I/O problem 127 */ 128 public String sha() throws IOException { 129 return this.jsn.text("sha"); 130 } 131 /** 132 * Get its URL. 133 * @return URL of content 134 * @throws IOException If there is any I/O problem 135 */ 136 public URL url() throws IOException { 137 return new URL(this.jsn.text("url")); 138 } 139 /** 140 * Get its HTML URL. 141 * @return URL of content 142 * @throws IOException If there is any I/O problem 143 */ 144 public URL htmlUrl() throws IOException { 145 return new URL(this.jsn.text("html_url")); 146 } 147 /** 148 * Get its GIT URL. 149 * @return URL of content 150 * @throws IOException If there is any I/O problem 151 */ 152 public URL gitUrl() throws IOException { 153 return new URL(this.jsn.text("git_url")); 154 } 155 /** 156 * Get its encoded content. 157 * @return Base64 encoded content 158 * @throws IOException If there is any I/O problem 159 */ 160 public String content() throws IOException { 161 return this.jsn.text("content"); 162 } 163 /** 164 * Get its decoded content. 165 * @return Decoded content 166 * @throws IOException If there is any I/O problem 167 */ 168 public byte[] decoded() throws IOException { 169 return DatatypeConverter.parseBase64Binary(this.content()); 170 } 171 @Override 172 public int compareTo(final Content cont) { 173 return this.content.compareTo(cont); 174 } 175 @Override 176 public void patch(final JsonObject json) throws IOException { 177 this.content.patch(json); 178 } 179 @Override 180 public JsonObject json() throws IOException { 181 return this.content.json(); 182 } 183 @Override 184 public Repo repo() { 185 return this.content.repo(); 186 } 187 @Override 188 public String path() { 189 return this.content.path(); 190 } 191 @Override 192 public InputStream raw() throws IOException { 193 return this.content.raw(); 194 } 195 } 196 }