]> git.basschouten.com Git - openhab-addons.git/blob
d1cfe3d3f3820aa161bcba07e9ca9962b37f193a
[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.boschindego.internal;
14
15 import java.time.Duration;
16 import java.time.Instant;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  * Session for storing Bosch Indego context information.
22  * 
23  * @author Jacob Laursen - Initial contribution
24  */
25 @NonNullByDefault
26 public class IndegoSession {
27
28     private static final Duration DEFAULT_EXPIRATION_PERIOD = Duration.ofSeconds(10);
29
30     private String contextId;
31     private String serialNumber;
32     private Instant expirationTime;
33
34     public IndegoSession() {
35         this("", "", Instant.MIN);
36     }
37
38     public IndegoSession(String contextId, String serialNumber, Instant expirationTime) {
39         this.contextId = contextId;
40         this.serialNumber = serialNumber;
41         this.expirationTime = expirationTime.equals(Instant.MIN) ? Instant.now().plus(DEFAULT_EXPIRATION_PERIOD)
42                 : expirationTime;
43     }
44
45     /**
46      * Get context id for HTTP requests (headers "x-im-context-id: <contextId>" and
47      * "Cookie: BOSCH_INDEGO_SSO=<contextId>").
48      * 
49      * @return current context id
50      */
51     public String getContextId() {
52         return contextId;
53     }
54
55     /**
56      * Get serial number of device.
57      * 
58      * @return serial number
59      */
60     public String getSerialNumber() {
61         return serialNumber;
62     }
63
64     /**
65      * Get expiration time of session as {@link Instant}.
66      * 
67      * @return expiration time
68      */
69     public Instant getExpirationTime() {
70         return expirationTime;
71     }
72
73     /**
74      * Check if session is initialized, i.e. has serial number.
75      * 
76      * @see #isValid()
77      * @return true if session is initialized
78      */
79     public boolean isInitialized() {
80         return !serialNumber.isEmpty();
81     }
82
83     /**
84      * Check if session is valid, i.e. has not yet expired.
85      *
86      * @return true if session is still valid
87      */
88     public boolean isValid() {
89         return !contextId.isEmpty() && expirationTime.isAfter(Instant.now());
90     }
91
92     /**
93      * Invalidate session.
94      */
95     public void invalidate() {
96         contextId = "";
97         expirationTime = Instant.MIN;
98     }
99
100     @Override
101     public String toString() {
102         return String.format("%s (serialNumber %s, expirationTime %s)", contextId, serialNumber, expirationTime);
103     }
104 }