2 * Copyright 2016 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openo.nfvo.jujuvnfmadapter.common.servicetoken.module;
19 import net.sf.json.JSONObject;
21 import org.apache.commons.lang3.StringUtils;
22 import org.openo.nfvo.jujuvnfmadapter.common.VNFJsonUtil;
26 * Access tokens class.<br>
31 * @version NFVO 0.5 Sep 12, 2016
33 public class AccessTokens {
35 private String accesTokens;
39 private long createTime;
49 public AccessTokens() {
50 throw new UnsupportedOperationException();
63 public AccessTokens(String token, int ttl) {
64 this.accesTokens = token;
66 this.createTime = System.currentTimeMillis();
80 public AccessTokens(String accessToken, Integer expire, Long createTime) {
81 this.accesTokens = accessToken;
82 this.expire = expire == null ? 0 : expire;
83 this.createTime = createTime == null ? 0 : createTime;
86 public String getAccessToken() {
87 return this.accesTokens;
90 public void setAccessToken(String token) {
91 this.accesTokens = token;
94 public int getExpire() {
98 public long getCreateTime() {
99 return this.createTime;
104 * Check time validity.<br>
109 public boolean valid() {
110 if(this.expire == 0) {
113 return System.currentTimeMillis() - this.createTime <= 1000L * this.expire;
117 public String toString() {
118 return '{'+ StringUtils.trimToEmpty(this.getAccessToken()) + '\'' + ",'expire': '" + this.getExpire() + '\'' +
119 ",'createTime': '" + this.getCreateTime() + '\'' + '}';
130 public static AccessTokens toEntity(JSONObject jsonObject) {
131 String token = VNFJsonUtil.getJsonFieldStr(jsonObject, "accessToken");
132 Integer expire = VNFJsonUtil.getJsonFieldInt(jsonObject, "expire");
133 Long createTime = VNFJsonUtil.getJsonFieldLong(jsonObject, "createTime");
134 return new AccessTokens(token, expire, createTime);