]> git.basschouten.com Git - openhab-addons.git/blob
3e4c66a37d28f94a04f9e74e6431fb8854b70736
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.asuswrt.internal.api;
14
15 import static org.openhab.binding.asuswrt.internal.constants.AsuswrtBindingSettings.COOKIE_LIFETIME_S;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * The {@link AsuswrtCookie} is used for storing cookie details.
21  *
22  * @author Christian Wild - Initial contribution
23  */
24 @NonNullByDefault
25 public class AsuswrtCookie {
26     protected String cookie = "";
27     protected String token = "";
28     protected Long cookieTimeStamp = 0L;
29
30     /*
31      * Set and reset functions
32      */
33
34     /**
35      * Sets a new cookie.
36      */
37     public void setCookie(String cookie) {
38         this.cookie = cookie;
39         cookieTimeStamp = System.currentTimeMillis();
40     }
41
42     /**
43      * Resets a cookie.
44      */
45     public void resetCookie() {
46         cookie = "";
47         token = "";
48         cookieTimeStamp = 0L;
49     }
50
51     /*
52      * Cookie checks
53      */
54
55     /**
56      * Checks if a cookie is set.
57      */
58     public boolean cookieIsSet() {
59         return !cookie.isBlank();
60     }
61
62     /**
63      * Checks if a cookie is expired.
64      *
65      * @return <code>true</code> if cookie is set and expired
66      */
67     public boolean cookieIsExpired() {
68         return cookieTimeStamp > 0L && System.currentTimeMillis() > cookieTimeStamp + (COOKIE_LIFETIME_S * 1000);
69     }
70
71     /**
72      * Checks if a cookie is set and not expired.
73      */
74     public boolean isValid() {
75         return !cookieIsExpired() && cookieIsSet();
76     }
77
78     /**
79      * Gets the cookie.
80      */
81     public String getCookie() {
82         return cookie;
83     }
84 }