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