|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2019 FactsMission AG. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.factsmission.psps; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.io.InputStreamReader; |
| 29 | +import java.io.Reader; |
| 30 | +import java.net.HttpURLConnection; |
| 31 | +import java.net.MalformedURLException; |
| 32 | +import java.net.URL; |
| 33 | +import java.util.Base64; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Iterator; |
| 36 | +import java.util.Map; |
| 37 | +import org.json.simple.JSONArray; |
| 38 | +import org.json.simple.JSONObject; |
| 39 | +import org.json.simple.parser.JSONParser; |
| 40 | +import org.json.simple.parser.ParseException; |
| 41 | + |
| 42 | +/** |
| 43 | + * |
| 44 | + * @author me |
| 45 | + */ |
| 46 | +public class Repository { |
| 47 | + |
| 48 | + private final String token; |
| 49 | + private final String repository; |
| 50 | + private String branch; |
| 51 | + private final URL apiBaseURI; |
| 52 | + private String latestCommitURL; |
| 53 | + private Map<String, URL> path2Stuff; |
| 54 | + |
| 55 | + Repository(String repository, String token) { |
| 56 | + this.repository = repository; |
| 57 | + this.token = token; |
| 58 | + String apiBaseURIString = "https://api.github.com/repos/" + repository + "/"; |
| 59 | + try { |
| 60 | + this.apiBaseURI = new URL(apiBaseURIString); |
| 61 | + } catch (MalformedURLException ex) { |
| 62 | + throw new RuntimeException(ex); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + InputStream getAuthenticatedStream(URL url) throws IOException { |
| 67 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 68 | + String authStringEnoded = Base64.getEncoder().encodeToString((token + ":").getBytes("utf-8")); |
| 69 | + connection.addRequestProperty("Authorization", "Basic " + authStringEnoded); |
| 70 | + return connection.getInputStream(); |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + private Object getParsedJson(URL stuffURL) throws IOException { |
| 75 | + try { |
| 76 | + InputStream stuffJsonStream = getAuthenticatedStream(stuffURL); |
| 77 | + Reader stuffJsonReader = new InputStreamReader(stuffJsonStream, "utf-8"); |
| 78 | + JSONParser jsonParser = new JSONParser(); |
| 79 | + Object obj = jsonParser.parse(stuffJsonReader); |
| 80 | + return obj; |
| 81 | + } catch (ParseException ex) { |
| 82 | + throw new RuntimeException(ex); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + JSONObject getJsonObject(URL stuffURL) throws IOException { |
| 87 | + return (JSONObject) getParsedJson(stuffURL); |
| 88 | + } |
| 89 | + |
| 90 | + JSONArray getJsonArray(URL stuffURL) throws IOException { |
| 91 | + return (JSONArray) getParsedJson(stuffURL); |
| 92 | + } |
| 93 | + |
| 94 | + void useBranch(String branch) throws IOException { |
| 95 | + this.branch = branch; |
| 96 | + try { |
| 97 | + URL masterURI = new URL(apiBaseURI, "branches/"+branch); |
| 98 | + JSONObject master = getJsonObject(masterURI); |
| 99 | + JSONObject commitA = (JSONObject) master.get("commit"); |
| 100 | + latestCommitURL = (String) commitA.get("html_url"); |
| 101 | + JSONObject commitB = (JSONObject) commitA.get("commit"); |
| 102 | + JSONObject tree = (JSONObject) commitB.get("tree"); |
| 103 | + String treeURLString = (String) tree.get("url"); |
| 104 | + processTree(treeURLString); |
| 105 | + } catch (MalformedURLException | ParseException ex) { |
| 106 | + throw new RuntimeException(ex); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private void processTree(String treeURLString) throws IOException, ParseException { |
| 111 | + String treeURLRecursiveString = treeURLString + "?recursive=1"; |
| 112 | + URL treeURLRecursive = new URL(treeURLRecursiveString); |
| 113 | + JSONObject jsonObject = getJsonObject(treeURLRecursive); |
| 114 | + JSONArray tree = (JSONArray) jsonObject.get("tree"); |
| 115 | + path2Stuff = new HashMap<>(); |
| 116 | + Iterator<JSONObject> iterator = tree.iterator(); |
| 117 | + while (iterator.hasNext()) { |
| 118 | + JSONObject next = iterator.next(); |
| 119 | + String path = (String) next.get("path"); |
| 120 | + String url = (String) next.get("url"); |
| 121 | + URL stuffURL = new URL(url); |
| 122 | + path2Stuff.put(path, stuffURL); |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + String getCommitURL() { |
| 128 | + return latestCommitURL; |
| 129 | + } |
| 130 | + |
| 131 | + Iterable<String> getPaths() { |
| 132 | + return path2Stuff.keySet(); |
| 133 | + } |
| 134 | + |
| 135 | + byte[] getContent(String path) throws IOException { |
| 136 | + JSONObject jsonObject = getJsonObject(path2Stuff.get(path)); |
| 137 | + String contentBase64 = (String) jsonObject.get("content"); |
| 138 | + if (contentBase64 == null) { |
| 139 | + return null; |
| 140 | + } |
| 141 | + try { |
| 142 | + return Base64.getMimeDecoder().decode(contentBase64); |
| 143 | + } catch (IllegalArgumentException ex) { |
| 144 | + throw new RuntimeException("Something bad happened", ex); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments