|
| 1 | +/* |
| 2 | + * ============================================================================ |
| 3 | + * Name : ForgejoStatusClient.java |
| 4 | + * Author : AppDevForAll |
| 5 | + * Copyright : Copyright (c) 2026 AppDevForAll |
| 6 | + * Description : K2GO-422. App-side client of the dash-node Forgejo status endpoint |
| 7 | + * (static/dashboard GET /k2go-api/forgejo/status, dash-node 1.3.6). |
| 8 | + * Read-only: it tells the module detail whether to offer "Install |
| 9 | + * repos", show the repos as present, or block (an admin exists that |
| 10 | + * K2Go cannot authenticate). The box does the work over its own API, |
| 11 | + * so this is a plain HTTP GET the app runs on an IO thread. |
| 12 | + * ============================================================================ |
| 13 | + */ |
| 14 | +package org.appdevforall.k2go.forgejo.data; |
| 15 | + |
| 16 | +import androidx.annotation.Nullable; |
| 17 | + |
| 18 | +import org.appdevforall.k2go.config.BoxEndpoints; |
| 19 | +import org.json.JSONArray; |
| 20 | +import org.json.JSONObject; |
| 21 | + |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.net.HttpURLConnection; |
| 25 | +import java.net.URL; |
| 26 | +import java.nio.charset.StandardCharsets; |
| 27 | + |
| 28 | +/** Fetches the box Forgejo status. {@link #fetch()} BLOCKS (one HTTP GET), so callers run it off the |
| 29 | + * main thread. Returns {@code null} when the box did not answer (unreachable / not ready). */ |
| 30 | +public final class ForgejoStatusClient { |
| 31 | + |
| 32 | + /** Immutable snapshot of the box Forgejo, as the status endpoint reports it. */ |
| 33 | + public static final class Status { |
| 34 | + public final boolean reachable; |
| 35 | + public final boolean adminExists; |
| 36 | + public final boolean adminAuthenticable; |
| 37 | + public final boolean manageable; |
| 38 | + public final int repoCount; |
| 39 | + |
| 40 | + Status(boolean reachable, boolean adminExists, boolean adminAuthenticable, |
| 41 | + boolean manageable, int repoCount) { |
| 42 | + this.reachable = reachable; |
| 43 | + this.adminExists = adminExists; |
| 44 | + this.adminAuthenticable = adminAuthenticable; |
| 45 | + this.manageable = manageable; |
| 46 | + this.repoCount = repoCount; |
| 47 | + } |
| 48 | + |
| 49 | + /** K2Go can administer the forge and it has no example repos yet -> offer "Install repos". */ |
| 50 | + public boolean canInstallRepos() { return manageable && repoCount == 0; } |
| 51 | + |
| 52 | + /** The example repos are present -> nothing to install (refresh/update is K2GO-422 section A). */ |
| 53 | + public boolean hasRepos() { return repoCount > 0; } |
| 54 | + |
| 55 | + /** An admin exists that K2Go cannot authenticate -> do not offer any repo action. */ |
| 56 | + public boolean blocked() { return !manageable; } |
| 57 | + } |
| 58 | + |
| 59 | + private static final String STATUS_URL = BoxEndpoints.API + "/forgejo/status"; |
| 60 | + |
| 61 | + /** GET the status; null on any read/parse error (treat as "cannot tell", offer nothing). */ |
| 62 | + @Nullable |
| 63 | + public Status fetch() { |
| 64 | + try { |
| 65 | + HttpURLConnection c = (HttpURLConnection) new URL(STATUS_URL).openConnection(); |
| 66 | + c.setUseCaches(false); |
| 67 | + c.setConnectTimeout(4000); |
| 68 | + // The box may do a couple of short HTTP calls to the forge before answering; keep the read |
| 69 | + // timeout comfortably above their combined worst case so a warming forge does not make the |
| 70 | + // status read time out (which would silently hide the button). |
| 71 | + c.setReadTimeout(30000); |
| 72 | + c.setRequestMethod("GET"); |
| 73 | + c.setRequestProperty("Accept", "application/json"); |
| 74 | + int code = c.getResponseCode(); |
| 75 | + boolean ok = code >= 200 && code < 300; |
| 76 | + String text = readAll(ok ? c.getInputStream() : c.getErrorStream()); |
| 77 | + c.disconnect(); |
| 78 | + if (!ok) return null; |
| 79 | + JSONObject j = new JSONObject(text.isEmpty() ? "{}" : text); |
| 80 | + JSONArray repos = j.optJSONArray("repos"); |
| 81 | + int repoCount = repos != null ? repos.length() : 0; |
| 82 | + return new Status( |
| 83 | + j.optBoolean("reachable", false), |
| 84 | + j.optBoolean("adminExists", false), |
| 85 | + j.optBoolean("adminAuthenticable", false), |
| 86 | + j.optBoolean("manageable", false), |
| 87 | + repoCount); |
| 88 | + } catch (Exception e) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static String readAll(@Nullable InputStream is) throws Exception { |
| 94 | + if (is == null) return ""; |
| 95 | + ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
| 96 | + byte[] chunk = new byte[4096]; |
| 97 | + int n; |
| 98 | + while ((n = is.read(chunk)) != -1) buf.write(chunk, 0, n); |
| 99 | + is.close(); |
| 100 | + return buf.toString(StandardCharsets.UTF_8.name()); |
| 101 | + } |
| 102 | +} |
0 commit comments