Skip to content

Commit fff1ee9

Browse files
committed
GitHub API access now exclusively from Repository class
1 parent 92177cf commit fff1ee9

File tree

2 files changed

+32
-31
lines changed

2 files changed

+32
-31
lines changed

src/main/java/com/factsmission/psps/Repository.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import java.net.URL;
3333
import java.util.Base64;
3434
import java.util.HashMap;
35+
import java.util.HashSet;
3536
import java.util.Iterator;
3637
import java.util.Map;
38+
import java.util.Set;
3739
import org.json.simple.JSONArray;
3840
import org.json.simple.JSONObject;
3941
import org.json.simple.parser.JSONParser;
@@ -63,7 +65,7 @@ public class Repository {
6365
}
6466
}
6567

66-
InputStream getAuthenticatedStream(URL url) throws IOException {
68+
private InputStream getAuthenticatedStream(URL url) throws IOException {
6769
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
6870
String authStringEnoded = Base64.getEncoder().encodeToString((token + ":").getBytes("utf-8"));
6971
connection.addRequestProperty("Authorization", "Basic " + authStringEnoded);
@@ -83,11 +85,11 @@ private Object getParsedJson(URL stuffURL) throws IOException {
8385
}
8486
}
8587

86-
JSONObject getJsonObject(URL stuffURL) throws IOException {
88+
private JSONObject getJsonObject(URL stuffURL) throws IOException {
8789
return (JSONObject) getParsedJson(stuffURL);
8890
}
8991

90-
JSONArray getJsonArray(URL stuffURL) throws IOException {
92+
private JSONArray getJsonArray(URL stuffURL) throws IOException {
9193
return (JSONArray) getParsedJson(stuffURL);
9294
}
9395

@@ -145,4 +147,13 @@ byte[] getContent(String path) throws IOException {
145147
}
146148
}
147149

150+
151+
String[] getBranches() throws IOException {
152+
JSONArray jsonArray = getJsonArray(new URL(apiBaseURI, "branches"));
153+
Set<String> resultSet = new HashSet<>();
154+
jsonArray.forEach((obj) -> {
155+
resultSet.add((String) ((JSONObject) obj).get("name"));
156+
});
157+
return resultSet.toArray(new String[resultSet.size()]);
158+
}
148159
}

src/main/java/com/factsmission/psps/RepositoryProcessor.java

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.Map;
3434
import java.util.Map.Entry;
3535
import java.util.Set;
36+
import java.util.logging.Level;
37+
import java.util.logging.Logger;
3638

3739
import org.apache.clerezza.commons.rdf.Graph;
3840
import org.apache.clerezza.commons.rdf.IRI;
@@ -41,7 +43,6 @@
4143
import org.apache.clerezza.rdf.core.serializedform.Parser;
4244
import org.apache.clerezza.rdf.core.serializedform.Serializer;
4345
import org.apache.clerezza.rdf.ontologies.DCTERMS;
44-
import org.json.simple.JSONArray;
4546
import org.json.simple.JSONObject;
4647
import org.json.simple.parser.JSONParser;
4748
import org.json.simple.parser.ParseException;
@@ -53,8 +54,6 @@ public class RepositoryProcessor {
5354
private final Parser parser = Parser.getInstance();
5455

5556
private boolean supressFileExtension;
56-
57-
private final URL apiBaseURI;
5857
private final Set<BranchProcessor> branchProcessors = new HashSet<>();
5958
private final Repository repository;
6059

@@ -65,7 +64,7 @@ class BranchProcessor {
6564
private FileStorage fileStorage = new FileStorage();
6665
private final String branch;
6766

68-
private BranchProcessor(String branch) throws IOException, ParseException {
67+
private BranchProcessor(String branch) throws IOException {
6968
this.branch = branch;
7069
processBranch();
7170
}
@@ -96,7 +95,7 @@ protected void loadStuffToGraph(byte[] content, String path, String rdfType) thr
9695
}
9796
}
9897

99-
private void processFile(String path) throws IOException, ParseException {
98+
private void processFile(String path) throws IOException {
10099
String rdfType = getRdfFormat(path);
101100
byte[] content = repository.getContent(path);
102101
if (content == null) {
@@ -110,13 +109,13 @@ private void processFile(String path) throws IOException, ParseException {
110109
}
111110
}
112111

113-
private void processBranch() throws IOException, ParseException {
112+
private void processBranch() throws IOException {
114113
synchronized(repository) {
115114
repository.useBranch(branch);
116115
baseIRI = getBaseIRI(repository.getContent("BASEURI"));
117116
for (String path : repository.getPaths()) {
118117
processFile(path);
119-
};
118+
}
120119
Graph repoGraph = new SimpleGraph();
121120
repoGraph.add(new TripleImpl(getBranchIRI(), Ontology.latestCommit, new IRI(repository.getCommitURL())));
122121
repoGraph.add(new TripleImpl(getBranchIRI(), Ontology.repository, getRepoIRI()));
@@ -137,13 +136,18 @@ IRI constructFileBaseIRI(String path) {
137136
return new IRI(baseIRI.getUnicodeString() + path);
138137
}
139138

140-
private IRI getBaseIRI(byte[] contentBytes) throws IOException, ParseException {
139+
private IRI getBaseIRI(byte[] contentBytes) throws IOException {
141140
if (contentBytes != null) {
142141
String content = new String(contentBytes, "UTF-8");
143142
if (content.trim().charAt(0) == '{') {
144-
JSONParser jsonParser = new JSONParser();
145-
JSONObject contentObject = (JSONObject) jsonParser.parse(content);
146-
return new IRI((String) contentObject.get(branch));
143+
try {
144+
JSONParser jsonParser = new JSONParser();
145+
JSONObject contentObject = (JSONObject) jsonParser.parse(content);
146+
return new IRI((String) contentObject.get(branch));
147+
} catch (ParseException ex) {
148+
Logger.getLogger(RepositoryProcessor.class.getName()).log(Level.SEVERE, "Couln'd parse BASEURI, usind default base IRI", ex);
149+
return getDefaultBaseIRI();
150+
}
147151
} else {
148152
return new IRI(content.trim());
149153
}
@@ -155,16 +159,10 @@ private IRI getBaseIRI(byte[] contentBytes) throws IOException, ParseException {
155159
}
156160

157161
RepositoryProcessor(String repository, String token, boolean supressFileExtension) throws IOException {
158-
String apiBaseURIString = "https://api.github.com/repos/" + repository + "/";
159-
this.apiBaseURI = new URL(apiBaseURIString);
160162
this.repositoryName = repository;
161163
this.repository = new Repository(repository, token);
162164
this.supressFileExtension = supressFileExtension;
163-
try {
164-
processRepository();
165-
} catch (ParseException ex) {
166-
throw new IOException(ex);
167-
}
165+
processRepository();
168166
}
169167

170168
public static void main(String[] args) throws Exception {
@@ -196,9 +194,9 @@ public Map<IRI, Graph> getGraphs() {
196194
return result;
197195
}
198196

199-
private void processRepository() throws IOException, ParseException {
197+
private void processRepository() throws IOException {
200198
System.out.println("Loading RDF data from " + repositoryName);
201-
String[] branches = getBranches();
199+
String[] branches = repository.getBranches();
202200
for (String branch : branches) {
203201
BranchProcessor branchProcessor = new BranchProcessor(branch);
204202
branchProcessors.add(branchProcessor);
@@ -245,13 +243,5 @@ private String getRdfFormat(String path) {
245243
return null;
246244
}
247245

248-
private String[] getBranches() throws IOException {
249-
JSONArray jsonArray = repository.getJsonArray(new URL(apiBaseURI, "branches"));
250-
Set<String> resultSet = new HashSet<>();
251-
jsonArray.forEach((obj) -> {
252-
resultSet.add((String) ((JSONObject) obj).get("name"));
253-
});
254-
return resultSet.toArray(new String[resultSet.size()]);
255-
}
256246

257247
}

0 commit comments

Comments
 (0)