|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Joshua Sing <[email protected]> |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + */ |
| 16 | + |
| 17 | +package dev.hypera.updatelib.checkers.impl; |
| 18 | + |
| 19 | +import dev.hypera.updatelib.checkers.UpdateChecker; |
| 20 | +import dev.hypera.updatelib.data.CheckData; |
| 21 | +import dev.hypera.updatelib.exceptions.InvalidResourceException; |
| 22 | +import dev.hypera.updatelib.objects.UpdateStatus; |
| 23 | +import dev.hypera.updatelib.objects.UpdateStatusBuilder; |
| 24 | +import dev.hypera.updatelib.utils.ReaderUtils; |
| 25 | +import org.json.simple.JSONArray; |
| 26 | +import org.json.simple.JSONObject; |
| 27 | +import org.json.simple.JSONValue; |
| 28 | + |
| 29 | +import javax.net.ssl.HttpsURLConnection; |
| 30 | +import java.io.BufferedReader; |
| 31 | +import java.io.InputStreamReader; |
| 32 | +import java.net.URL; |
| 33 | + |
| 34 | +public class SongodaUpdateChecker implements UpdateChecker { |
| 35 | + |
| 36 | + private static final String URL_FORMAT = "https://songoda.com/api/v2/products/id/%s"; |
| 37 | + |
| 38 | + /** |
| 39 | + * Check for an update using Songoda's API. |
| 40 | + * |
| 41 | + * @param data Check data. |
| 42 | + * |
| 43 | + * @return {@link UpdateStatus} |
| 44 | + * @throws Exception Any exceptions that occur while checking for updates. |
| 45 | + * @since 3.1.0-SNAPSHOT |
| 46 | + */ |
| 47 | + @Override |
| 48 | + public UpdateStatus check(CheckData data) throws Exception { |
| 49 | + URL url = new URL(String.format(URL_FORMAT, data.getResourceId())); |
| 50 | + |
| 51 | + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); |
| 52 | + httpsURLConnection.setConnectTimeout(data.getTimeout()); |
| 53 | + httpsURLConnection.setReadTimeout(data.getTimeout()); |
| 54 | + |
| 55 | + if(httpsURLConnection.getResponseCode() == 404) |
| 56 | + throw new InvalidResourceException("Cannot find Songoda resource with id '" + data.getResourceId() + "'"); |
| 57 | + |
| 58 | + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream())); |
| 59 | + JSONObject json = (JSONObject) JSONValue.parse(ReaderUtils.readAll(bufferedReader)); |
| 60 | + bufferedReader.close(); |
| 61 | + |
| 62 | + JSONObject songodaData = (JSONObject) json.get("data"); |
| 63 | + String distributedVersion = ((JSONObject) ((JSONArray) songodaData.get("versions")).get(0)).get("version").toString(); |
| 64 | + |
| 65 | + return UpdateStatusBuilder.create(distributedVersion, data.getCurrentVersion()).build(); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments