Example usage for java.time Instant getEpochSecond

List of usage examples for java.time Instant getEpochSecond

Introduction

In this page you can find the example usage for java.time Instant getEpochSecond.

Prototype

public long getEpochSecond() 

Source Link

Document

Gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.

Usage

From source file:org.hyperledger.fabric.sdk.transaction.ProtoUtils.java

public static Timestamp getCurrentFabricTimestamp() {
    Instant time = Instant.now();
    return Timestamp.newBuilder().setSeconds(time.getEpochSecond()).setNanos(time.getNano()).build();
}

From source file:org.trellisldp.file.FileUtils.java

/**
 * Get the nquads file for a given moment in time.
 * @param dir the directory/*from w  w  w .j a va  2 s .co m*/
 * @param time the time
 * @return the file
 */
public static File getNquadsFile(final File dir, final Instant time) {
    return new File(dir, Long.toString(time.getEpochSecond()) + ".nq");
}

From source file:org.trellisldp.test.TestUtils.java

/**
 * Check that it is now really later than the provided instant.
 * @param time an instant/* w ww  . j av  a 2s.  com*/
 * @return true if it is now later than the provided instant; false otherwise
 */
public static boolean isReallyLaterThan(final Instant time) {
    final Instant t = now();
    return t.isAfter(time) && t.getEpochSecond() > time.getEpochSecond();
}

From source file:se.curity.examples.oauth.jwt.AbstractJwtValidator.java

@Override
public Optional<Map<String, Object>> validateAll(String jwt, String audience, String issuer)
        throws JwtValidationException {
    if (this.validate(jwt) == null) {
        return Optional.empty();
    }/*www  . j  a  v a2 s.  co  m*/

    String[] jwtParts = jwt.split("\\.");

    if (jwtParts.length != 3) {
        throw new IllegalArgumentException("Incorrect JWT input");
    }

    String body = jwtParts[1];

    Base64 base64 = new Base64(true);

    @SuppressWarnings("unchecked")
    Map<String, Object> bodyMap = _gson.fromJson(new String(base64.decode(body), Charsets.UTF_8), Map.class);

    try {

        Double expD = (Double) bodyMap.get("exp");
        Double iatD = (Double) bodyMap.get("iat");

        long exp = Math.round(expD);
        long iat = Math.round(iatD);

        String aud = (String) bodyMap.get("aud");
        String iss = (String) bodyMap.get("iss");

        Preconditions.checkArgument(!isNullOrEmpty(aud), "Aud is not present in JWT");
        Preconditions.checkArgument(!isNullOrEmpty(iss), "Iss is not present in JWT");

        if (!aud.equals(audience)) {
            return Optional.empty();
        }
        if (!iss.equals(issuer)) {
            return Optional.empty();
        }
        Instant now = Instant.now();

        if (now.getEpochSecond() > exp) {
            return Optional.empty();
        }
        if (now.getEpochSecond() < iat) {
            return Optional.empty();
        }

    } catch (Exception e) {
        throw new JwtValidationException("Failed to extract data from Token");
    }

    Map<String, Object> result = new HashMap<>();

    for (String key : bodyMap.keySet()) {
        //This can be a bit more elaborate filter...
        result.put(key, bodyMap.get(key));
    }
    return Optional.of(result);
}