TokenUtils.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.pdd.core.token.parser;

  21. import java.math.BigDecimal;
  22. import java.util.Date;

  23. /**    
  24.  * TokenUtils
  25.  *
  26.  * @author Poli Andrea (poli@link.it)
  27.  * @author $Author$
  28.  * @version $Rev$, $Date$
  29.  */
  30. public class TokenUtils {
  31.    
  32.     private TokenUtils() {}

  33.     public static Date convertLifeTimeInSeconds(Date now, String exp) {
  34.         if(exp!=null) {
  35.             /**System.out.println("PARSE ["+exp+"]");*/
  36.            
  37.             // The lifetime in seconds of the access token.  For example, the value "3600" denotes that the access token will
  38.             // expire in one hour from the time the response was generated.
  39.            
  40.             //BigInteger expIn = new BigInteger(exp); // Fix: per gestire formati con esponenti
  41.             BigDecimal expIn = new BigDecimal(exp);
  42.             long lMs = 0;
  43.             try {
  44.                 @SuppressWarnings("unused")
  45.                 long lSeconds = expIn.longValueExact();
  46.                 expIn = expIn.multiply(BigDecimal.valueOf(1000l)); // trasformo in millisecondi
  47.                 lMs = expIn.longValueExact();
  48.             }catch(ArithmeticException ae) {
  49.                 /**System.out.println("PARSE OVERFLOW ["+exp+"]");*/
  50.                 expIn = BigDecimal.valueOf(Long.MAX_VALUE);
  51.                 lMs = expIn.longValueExact();
  52.                 return new Date(lMs);
  53.             }
  54.            
  55.             if(lMs>0) {
  56.                
  57.                 try {
  58.                     expIn = expIn.add(BigDecimal.valueOf(now.getTime()));
  59.                     lMs = expIn.longValueExact();
  60.                 }catch(ArithmeticException ae) {
  61.                     /**System.out.println("PARSE OVERFLOW 2 ["+exp+"]");*/
  62.                     expIn = BigDecimal.valueOf(Long.MAX_VALUE);
  63.                     lMs = expIn.longValueExact();
  64.                 }
  65.                                
  66.                 return new Date(lMs);
  67.             }
  68.         }
  69.         return null;
  70.     }

  71.     public static Date parseTimeInSecond(String dateS) {
  72.         if(dateS!=null) {
  73.            
  74.             /**System.out.println("PARSE ["+dateS+"]");*/
  75.            
  76.             //BigInteger date = new BigInteger(dateS); // Fix: per gestire formati con esponenti: es. 1.676363172E9
  77.             BigDecimal date = new BigDecimal(dateS);
  78.             long lMs = 0;
  79.             try {
  80.                 @SuppressWarnings("unused")
  81.                 long lSeconds = date.longValueExact();
  82.                 date = date.multiply(BigDecimal.valueOf(1000l)); // trasformo in millisecondi
  83.                 lMs = date.longValueExact();
  84.             }catch(ArithmeticException ae) {
  85.                 /**System.out.println("PARSE OVERFLOW ["+dateS+"]");*/
  86.                 date = BigDecimal.valueOf(Long.MAX_VALUE);
  87.                 lMs = date.longValueExact();
  88.             }
  89.             if(lMs>0) {
  90.                 return new Date(lMs);
  91.             }
  92.         }
  93.         return null;
  94.     }
  95.    
  96. }