From ec81cb87a1dd6bcac1a228fa1515baad9d0bd164 Mon Sep 17 00:00:00 2001 From: Max Mielchen Date: Thu, 9 Mar 2023 14:38:52 +0100 Subject: [PATCH 1/4] update java version from 8 to 11 to deploy on jitpack --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 533d6fa..ce10319 100644 --- a/pom.xml +++ b/pom.xml @@ -9,8 +9,8 @@ 1.3.1 - 1.8 - 1.8 + 11 + 11 From 8c08e196985b85769a12717bb9e21850d5036a40 Mon Sep 17 00:00:00 2001 From: Max Mielchen Date: Thu, 9 Mar 2023 14:54:14 +0100 Subject: [PATCH 2/4] update pom for jitpack deploy --- README.md | 67 +++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index e09fbba..158b982 100755 --- a/README.md +++ b/README.md @@ -1,8 +1,9 @@ # untis4j - a java API for webuntis

- GitHub Workflow Status - GitHub Workflow Status + GitHub Workflow Status + GitHub Workflow Status + Jitpack Deploy Status GitHub all releases

@@ -23,46 +24,44 @@ you can easily implement the method yourself with the `Session.getCustomData(... ### Maven -Add the GitHub repository to your build file +POM ```XML - - github - GitHub Packages - https://maven.pkg.github.com/untisapi/untis4j - + + + jitpack.io + https://jitpack.io + + + + + + com.github.untisapi + untis4j + RELEASE + + ``` -Add the dependency -```XML - - org.bytedream - untis4j - ${version} - -``` +### Gradle -### Groovy - -Add the GitHub repository to your build file +Groovy DSL ```groovy -maven { url 'https://maven.pkg.github.com/untisapi/untis4j' } -``` - -Add the dependency -```groovy -implementation 'org.bytedream:untis4j:${version}' -``` - -### Kotlin - -Add the GitHub repository to your build file -```kotlin -maven { url='https://maven.pkg.github.com/untisapi/untis4j' } +repositories { + maven { url 'https://jitpack.io' } +} +dependencies { + implementation 'com.github.untisapi:RELEASE' +} ``` -Add the dependency +Kotlin DSL ```kotlin -implementation ("org.bytedream:untis4j:${version}") +repositories { + maven { url='https://jitpack.io' } +} +dependencies { + implementation ("com.github.untisapi:RELEASE") +} ``` # Examples From 96a83ab55dcc6a16e1063825f9be4b6b29945f29 Mon Sep 17 00:00:00 2001 From: Max Mielchen Date: Thu, 9 Mar 2023 15:04:27 +0100 Subject: [PATCH 3/4] remove unused function --- src/main/java/org/bytedream/untis4j/CacheManager.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/main/java/org/bytedream/untis4j/CacheManager.java b/src/main/java/org/bytedream/untis4j/CacheManager.java index 5ed899f..de1113c 100755 --- a/src/main/java/org/bytedream/untis4j/CacheManager.java +++ b/src/main/java/org/bytedream/untis4j/CacheManager.java @@ -59,14 +59,6 @@ public T getOrRequest(UntisUtils.Method method, Request public T getOrRequest(UntisUtils.Method method, RequestManager requestManager, Map params, ResponseConsumer action) { int keyHashCode = mapCode(method, params); - Function function = (objects) -> { - try { - return action.getResponse(requestManager.POST(method.getMethod(), params)); - } catch (IOException e) { - return null; - } - }; - BaseResponse response; if ((response = cachedInformation.get(keyHashCode)) == null) { try { @@ -103,5 +95,4 @@ public void update(UntisUtils.Method method, RequestManager requestManager, Map< private int mapCode(UntisUtils.Method method, Map params) { return Arrays.toString(new Object[]{method, params}).hashCode(); } - } From 2ee5e67b9d6e2c014b1ff7c6876ce89b1c69343b Mon Sep 17 00:00:00 2001 From: Max Mielchen Date: Fri, 10 Mar 2023 14:01:02 +0100 Subject: [PATCH 4/4] use more efficient caching system --- pom.xml | 5 + .../org/bytedream/untis4j/RequestManager.java | 125 ++++++++++-------- .../java/org/bytedream/untis4j/Session.java | 50 +------ 3 files changed, 80 insertions(+), 100 deletions(-) diff --git a/pom.xml b/pom.xml index ce10319..ba230c0 100644 --- a/pom.xml +++ b/pom.xml @@ -19,6 +19,11 @@ json 20220924 + + com.github.ben-manes.caffeine + caffeine + 3.1.5 +
diff --git a/src/main/java/org/bytedream/untis4j/RequestManager.java b/src/main/java/org/bytedream/untis4j/RequestManager.java index 35af85d..52cdc46 100755 --- a/src/main/java/org/bytedream/untis4j/RequestManager.java +++ b/src/main/java/org/bytedream/untis4j/RequestManager.java @@ -1,5 +1,7 @@ package org.bytedream.untis4j; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.LoadingCache; import org.json.JSONException; import org.json.JSONObject; @@ -10,6 +12,7 @@ import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; +import java.time.Duration; import java.util.HashMap; import java.util.Map; @@ -25,6 +28,7 @@ public class RequestManager { private final Infos infos; private final String url; private boolean loggedIn = true; + private final LoadingCache requests = Caffeine.newBuilder().maximumSize(1_000).expireAfterWrite(Duration.ofMinutes(10)).refreshAfterWrite(Duration.ofMinutes(1)).build(this::POST); /** * Initialize the {@link RequestManager} class @@ -105,18 +109,27 @@ public static Infos generateUserInfosAndLogin(String username, String password, /** * Sends a POST request to the server * - * @param method params you want to send with the request + * @param method the POST method + * @param params params you want to send with the request * @return {@link Response} with all information about the response * @throws IOException if an IO Exception occurs - * @see RequestManager#POST(String, Map) * @since 1.0 */ - public Response POST(String method) throws IOException { - return this.POST(method, new HashMap<>()); + public Response POST(String method, Map params) throws IOException { + + if (loggedIn || method.equals(UntisUtils.Method.LOGIN.getMethod())) { + Response response = POST(UntisUtils.processParams(method, params)); + if (method.equals(UntisUtils.Method.LOGOUT.getMethod()) && loggedIn && response != null) { + loggedIn = false; + } + return response; + } else { + throw new LoginException("Not logged in"); + } } /** - * Sends a POST request to the server + * Sends a POST request to the server, but only if it is not in the cache * * @param method the POST method * @param params params you want to send with the request @@ -124,63 +137,65 @@ public Response POST(String method) throws IOException { * @throws IOException if an IO Exception occurs * @since 1.0 */ - public Response POST(String method, Map params) throws IOException { - - if (loggedIn || method.equals(UntisUtils.Method.LOGIN.getMethod())) { - boolean error; - URL url = new URL(this.url); - String requestBody = UntisUtils.processParams(method, params); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - - connection.setRequestMethod("POST"); - connection.setInstanceFollowRedirects(true); - connection.setDoOutput(true); - connection.setRequestProperty("User-Agent", infos.getUserAgent()); - connection.setRequestProperty("Content-Type", "application/json"); - - connection.setRequestProperty("Cookie", "JSESSIONID=" + infos.getSessionId() + "; schoolname=" + infos.getSchoolName()); - - DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); - outputStream.writeBytes(requestBody); - - BufferedReader input; - - try { - input = new BufferedReader(new InputStreamReader(connection.getInputStream())); - error = false; - } catch (NullPointerException e) { - error = true; - input = new BufferedReader(new InputStreamReader(connection.getErrorStream())); - } - - StringBuilder stringBuilder = new StringBuilder(); - String line; - while ((line = input.readLine()) != null) { - stringBuilder.append(line); - } - - JSONObject jsonObject; - - try { - jsonObject = new JSONObject(stringBuilder.toString()); - - if (jsonObject.has("error")) { - JSONObject errorObject = jsonObject.getJSONObject("error"); - throw new ConnectException("The response contains an error (" + errorObject.getInt("errorObject") + "): " + errorObject.getString("message")); - } - } catch (JSONException e) { - throw new ConnectException("An unexpected exception occurred: " + stringBuilder); - } + public Response CachedPOST(String method, Map params) throws IOException { - if (method.equals(UntisUtils.Method.LOGOUT.getMethod()) && loggedIn && !error) { + if (loggedIn) { + Response response = requests.get(UntisUtils.processParams(method, params)); + if (method.equals(UntisUtils.Method.LOGOUT.getMethod()) && loggedIn && response != null) { loggedIn = false; } - - return new Response(connection.getResponseCode(), jsonObject); + return response; } else { throw new LoginException("Not logged in"); } + } + + /** + * Sends a POST request to the server + * + * @return {@link Response} with all information about the response + * @throws IOException if an IO Exception occurs + * @since 1.0 + */ + private Response POST(String request) throws IOException + { + boolean error; + URL url = new URL(this.url); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setInstanceFollowRedirects(true); + connection.setDoOutput(true); + connection.setRequestProperty("User-Agent", infos.getUserAgent()); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Cookie", "JSESSIONID=" + infos.getSessionId() + "; schoolname=" + infos.getSchoolName()); + DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); + outputStream.writeBytes(request); + BufferedReader input; + try { + input = new BufferedReader(new InputStreamReader(connection.getInputStream())); + error = false; + } catch (NullPointerException e) { + error = true; + input = new BufferedReader(new InputStreamReader(connection.getErrorStream())); + } + StringBuilder stringBuilder = new StringBuilder(); + String line; + while ((line = input.readLine()) != null) { + stringBuilder.append(line); + } + JSONObject jsonObject; + try { + jsonObject = new JSONObject(stringBuilder.toString()); + if (jsonObject.has("error")) { + JSONObject errorObject = jsonObject.getJSONObject("error"); + throw new ConnectException("The response contains an error (" + errorObject.getInt("errorObject") + "): " + errorObject.getString("message")); + } + } catch (JSONException e) { + throw new ConnectException("An unexpected exception occurred: " + stringBuilder); + } + if (error) return null; + return new Response(connection.getResponseCode(), jsonObject); } /** diff --git a/src/main/java/org/bytedream/untis4j/Session.java b/src/main/java/org/bytedream/untis4j/Session.java index 73c4539..2cdb0c3 100755 --- a/src/main/java/org/bytedream/untis4j/Session.java +++ b/src/main/java/org/bytedream/untis4j/Session.java @@ -5,6 +5,7 @@ package org.bytedream.untis4j; +import com.github.benmanes.caffeine.cache.LoadingCache; import org.bytedream.untis4j.responseObjects.*; import org.bytedream.untis4j.responseObjects.baseObjects.BaseResponse; import org.bytedream.untis4j.responseObjects.baseObjects.BaseResponseLists; @@ -32,10 +33,7 @@ public class Session { private Infos infos; - - private long lastChange; private boolean useCache = true; - private CacheManager cacheManager = new CacheManager(); private RequestManager requestManager; /** @@ -50,12 +48,6 @@ public class Session { private Session(Infos infos, RequestManager requestManager) { this.infos = infos; this.requestManager = requestManager; - - try { - this.lastChange = getLatestImportTime().getLatestImportTime(); - } catch (IOException e) { - e.printStackTrace(); - } } /** @@ -112,7 +104,7 @@ public static Session login(String username, String password, String server, Str * @since 1.0 */ public void logout() throws IOException { - requestManager.POST(UntisUtils.Method.LOGOUT.getMethod()); + requestManager.POST(UntisUtils.Method.LOGOUT.getMethod(), new HashMap<>()); } /** @@ -156,15 +148,7 @@ private T requestSender(UntisUtils.Method method, Respo */ private T requestSender(UntisUtils.Method method, Map params, ResponseConsumer action) throws IOException { if (useCache) { - try { - long newLatestImportTime = this.getLatestImportTime().getLatestImportTime(); - if (lastChange < newLatestImportTime) { - lastChange = newLatestImportTime; - cacheManager.update(method, requestManager, params, action); - } - } catch (IOException ignore) { - } - return cacheManager.getOrRequest(method, requestManager, params, action); + return action.getResponse(requestManager.CachedPOST(method.getMethod(), params)); } else { return action.getResponse(requestManager.POST(method.getMethod(), params)); } @@ -463,7 +447,7 @@ public Classes getClasses(Integer schoolYearId) throws IOException { * @since 1.0 */ public LatestImportTime getLatestImportTime() throws IOException { - Response response = requestManager.POST(UntisUtils.Method.GETLATESTIMPORTTIME.getMethod()); + Response response = requestManager.POST(UntisUtils.Method.GETLATESTIMPORTTIME.getMethod(), new HashMap<>()); JSONObject jsonResponse = response.getResponse(); @@ -1082,11 +1066,7 @@ public Response getCustomData(String method) throws IOException { * @since 1.0 */ public Response getCustomData(String method, Map params) throws IOException { - if (params != null) { - return requestManager.POST(method, params); - } else { - return requestManager.POST(method); - } + return requestManager.POST(method, Objects.requireNonNullElseGet(params, HashMap::new)); } /** @@ -1099,26 +1079,6 @@ public Infos getInfos() { return infos; } - /** - * Returns the used {@link CacheManager} - * - * @return the used {@link CacheManager} - * @since 1.1 - */ - public CacheManager getCacheManager() { - return cacheManager; - } - - /** - * Replaces the current session cache manager with a new one - * - * @param cacheManager the new cache manager - * @since 1.1 - */ - public void setCacheManager(CacheManager cacheManager) { - this.cacheManager = cacheManager; - } - /** * Gets if every request response should be saved in cache *