View Javadoc

1   /*
2    * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 3.0 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17   * MA 02110-1301  USA
18   */
19  package fr.cnes.doi.services;
20  
21  import java.util.List;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  
26  import fr.cnes.doi.db.AbstractTokenDBHelper;
27  import fr.cnes.doi.exception.DOIDbException;
28  import fr.cnes.doi.security.TokenSecurity;
29  import java.util.ArrayList;
30  
31  /**
32   * Updates token database. The service checks if the token is expired. When it
33   * is expired, the token is removed from the database.
34   *
35   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
36   */
37  public class UpdateTokenDataBase implements Runnable {
38  
39      /**
40       * Logger.
41       */
42      private static final Logger LOG = LogManager.getLogger(UpdateTokenDataBase.class.getName());
43  
44      /**
45       * Token database.
46       */
47      private final AbstractTokenDBHelper tokenDB = TokenSecurity.getInstance().getTokenDB();
48  
49      /**
50       * {@inheritDoc}
51       */
52      @Override
53      public void run() {
54          LOG.info("Executing task that remove expired token from database.");
55          List<String> tokenList;
56          try {
57              tokenList = tokenDB.getTokens();
58          } catch (DOIDbException ex) {
59              tokenList = new ArrayList<>();
60          }
61          for (final String token : tokenList) {
62              if (TokenSecurity.getInstance().isExpired(token)) {
63                  LOG.info("Token {} is expired", token);
64              }
65          }
66      }
67  
68  }