Sonar Fixes, Formatting
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / oauth / TimedToken.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.oauth;
23
24 import java.nio.file.Path;
25
26 import org.onap.aaf.cadi.persist.Persist;
27 import org.onap.aaf.cadi.persist.Persistable;
28 import org.onap.aaf.cadi.persist.Persisting;
29
30 import aafoauth.v2_0.Token;
31
32 /**
33  * TimedToken
34  *   Tokens come from the Token Server with an "Expired In" setting.  This class will take that, and
35  *   create a date from time of Creation, which works with local code.
36  *
37  * We create a Derived class, so that it can be used as is the originating Token type.
38  *
39  * "expired" is local computer time
40  * @author Jonathan
41  *
42  */
43 // Package on purpose
44 public class TimedToken extends Token implements Persistable<Token> {
45     private Persisting<Token> cacheable; // no double inheritance...
46
47 //    public TimedToken(Token t, byte[] hash) {
48 //        this(t,(System.currentTimeMillis()/1000)+t.getExpiresIn(),hash,null);
49 //    }
50 //
51     public TimedToken(Persist<Token,?> p, Token t, byte[] hash, Path path){
52         this(p,t,t.getExpiresIn()+(System.currentTimeMillis()/1000),hash, path);
53     }
54
55     public TimedToken(Persist<Token,?> p, Token t, long expires_secsFrom1970, byte[] hash, Path path) {
56         cacheable = new Persisting<Token>(p, t,expires_secsFrom1970, hash, path);
57         accessToken=t.getAccessToken();
58         expiresIn=t.getExpiresIn();
59         refreshToken=t.getRefreshToken();
60         scope = t.getScope();
61         state = t.getState();
62         tokenType = t.getTokenType();
63     }
64
65
66     @Override
67     public Token get() {
68         return cacheable.get();
69     }
70
71     @Override
72     public boolean checkSyncTime() {
73         return cacheable.checkSyncTime();
74     }
75
76     @Override
77     public boolean checkReloadable() {
78         return cacheable.checkReloadable();
79     }
80
81     @Override
82     public boolean hasBeenTouched() {
83         return cacheable.hasBeenTouched();
84     }
85
86     @Override
87     public long expires() {
88         return cacheable.expires();
89     }
90
91     @Override
92     public boolean expired() {
93         return cacheable.expired();
94     }
95
96     @Override
97     public boolean match(byte[] hashIn) {
98         return cacheable.match(hashIn);
99     }
100
101     @Override
102     public byte[] getHash() {
103         return cacheable.getHash();
104     }
105
106     @Override
107     public void inc() {
108         cacheable.inc();
109     }
110
111     @Override
112     public int count() {
113         return cacheable.count();
114     }
115
116     /* (non-Javadoc)
117      * @see org.onap.aaf.cadi.oauth.Persistable#clearCount()
118      */
119     @Override
120     public void clearCount() {
121         cacheable.clearCount();
122     }
123
124     /* (non-Javadoc)
125      * @see org.onap.aaf.cadi.persist.Persistable#path()
126      */
127     @Override
128     public Path path() {
129         return cacheable.path();
130     }
131
132 }