xref: /OpenGrok/plugins/src/main/java/opengrok/auth/plugin/util/Timestamp.java (revision 0e7ff20bf1329caee73b111b78d073fce0110bb8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
22  */
23 package opengrok.auth.plugin.util;
24 
25 import java.util.Date;
26 
27 /**
28  * Implementation of timestamp decoding.
29  *
30  * @see <a href="https://docs.oracle.com/cd/B28196_01/idmanage.1014/b15997/mod_osso.htm">mod_osso documentation</a>
31  * chapter 9.5
32  *
33  * @author Krystof Tulinger
34  */
35 public class Timestamp {
36 
Timestamp()37     private Timestamp() {
38     }
39 
40     /**
41      * Converts OSSO timestamp cookie into java Date.
42      *
43      * @param cookie string representing the timestamp cookie
44      * @return java date object
45      * @throws NumberFormatException number format exception
46      */
decodeTimeCookie(String cookie)47     public static Date decodeTimeCookie(String cookie) throws NumberFormatException {
48         return new Date(Long.parseLong(cookie, 16) * 1000);
49     }
50 
51     /**
52      * Converts Date into OSSO cookie.
53      *
54      * @param date date
55      * @return string with the encoded value
56      */
encodeTimeCookie(Date date)57     public static String encodeTimeCookie(Date date) {
58         return Long.toHexString(date.getTime() / 1000);
59     }
60 }
61