1
2
3
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
21
22
23
24 @Immutable
25 @SuppressWarnings("PMD.TooManyMethods")
26 public interface Content extends Comparable<Content>,
27 JsonReadable, JsonPatchable {
28
29
30
31
32
33 Repo repo();
34
35
36
37
38
39 String path();
40
41
42
43
44
45
46 InputStream raw() throws IOException;
47
48
49
50
51
52 @Immutable
53 @ToString
54 @Loggable(Loggable.DEBUG)
55 @EqualsAndHashCode(of = { "content", "jsn" })
56 final class Smart implements Content {
57
58
59
60 private final transient Content content;
61
62
63
64
65 private final transient SmartJson jsn;
66
67
68
69
70
71 public Smart(
72 final Content cont) {
73 this.content = cont;
74 this.jsn = new SmartJson(cont);
75 }
76
77
78
79
80
81
82 public String name() throws IOException {
83 return this.jsn.text("name");
84 }
85
86
87
88
89
90
91 public String type() throws IOException {
92 return this.jsn.text("type");
93 }
94
95
96
97
98
99
100 public int size() throws IOException {
101 return this.jsn.number("size");
102 }
103
104
105
106
107
108
109 public String sha() throws IOException {
110 return this.jsn.text("sha");
111 }
112
113
114
115
116
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
128
129
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
141
142
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
154
155
156
157 public String content() throws IOException {
158 return this.jsn.text("content");
159 }
160
161
162
163
164
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 }