Skip to content

Commit

Permalink
parse timestamps as ZonedDateTime internally
Browse files Browse the repository at this point in the history
Timestamps have been stored with their String representation from the
API with convenience methods to convert them into ZonedDateTime.
We now use the Jackson JavaTimeModule to parse them directly and swap
the real and convenience getters.
  • Loading branch information
stklcode committed Jun 15, 2023
1 parent 1195b44 commit f8bddf7
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 77 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
## unreleased

### Deprecations
* `get...TimeString()` methods on various model classes are now deprecated

### Fix
* Fixed JSON type conversion in `SecretResponse#get(String, Class)` (#67)

### Improvements
* Parse timestamps as `ZonedDateTime` instead of `String` representation


## 1.1.4 (2023-06-15)

Expand Down
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>de.stklcode.jvault</groupId>
<artifactId>jvault-connector</artifactId>
<version>1.1.5-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down Expand Up @@ -119,6 +119,7 @@
--add-opens de.stklcode.jvault.connector/de.stklcode.jvault.connector.model.response=ALL-UNNAMED
--add-opens de.stklcode.jvault.connector/de.stklcode.jvault.connector.model.response.embedded=ALL-UNNAMED
--add-opens de.stklcode.jvault.connector/de.stklcode.jvault.connector.test=com.fasterxml.jackson.databind
--add-opens de.stklcode.jvault.connector/de.stklcode.jvault.connector.test=com.fasterxml.jackson.datatype.jsr310
</argLine>
</configuration>
</plugin>
Expand All @@ -143,6 +144,12 @@
<version>2.15.2</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.15.2</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package de.stklcode.jvault.connector.internal;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import de.stklcode.jvault.connector.exception.*;
import de.stklcode.jvault.connector.model.response.ErrorResponse;

Expand Down Expand Up @@ -62,7 +65,10 @@ public RequestHelper(final String baseURL,
this.timeout = timeout;
this.tlsVersion = tlsVersion;
this.trustedCaCert = trustedCaCert;
this.jsonMapper = new ObjectMapper();
this.jsonMapper = new ObjectMapper()
.registerModule(new JavaTimeModule())
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package de.stklcode.jvault.connector.model.response;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import de.stklcode.jvault.connector.exception.InvalidResponseException;
import de.stklcode.jvault.connector.model.response.embedded.VersionMetadata;

Expand Down Expand Up @@ -82,7 +85,11 @@ public final <C> C get(final String key, final Class<C> type) throws InvalidResp
} else if (type.isInstance(rawValue)) {
return type.cast(rawValue);
} else {
var om = new ObjectMapper();
var om = new ObjectMapper()
.registerModule(new JavaTimeModule())
.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);

if (rawValue instanceof String) {
return om.readValue((String) rawValue, type);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.Serializable;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Map;
import java.util.Objects;

Expand All @@ -35,12 +34,12 @@
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public final class SecretMetadata implements Serializable {
private static final long serialVersionUID = 1684891108903409038L;
private static final long serialVersionUID = -4967896264361344676L;

private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX");

@JsonProperty("created_time")
private String createdTimeString;
private ZonedDateTime createdTime;

@JsonProperty("current_version")
private Integer currentVersion;
Expand All @@ -52,31 +51,29 @@ public final class SecretMetadata implements Serializable {
private Integer oldestVersion;

@JsonProperty("updated_time")
private String updatedTime;
private ZonedDateTime updatedTime;

@JsonProperty("versions")
private Map<Integer, VersionMetadata> versions;

/**
* @return Time of secret creation as raw string representation.
* @deprecated Method left for backwards compatibility only. Use {@link #getCreatedTime()} instead.
*/
@Deprecated(since = "1.2", forRemoval = true)
public String getCreatedTimeString() {
return createdTimeString;
if (createdTime != null) {
return TIME_FORMAT.format(createdTime);
}

return null;
}

/**
* @return Time of secret creation.
*/
public ZonedDateTime getCreatedTime() {
if (createdTimeString != null && !createdTimeString.isEmpty()) {
try {
return ZonedDateTime.parse(createdTimeString, TIME_FORMAT);
} catch (DateTimeParseException e) {
// Ignore.
}
}

return null;
return createdTime;
}

/**
Expand All @@ -102,24 +99,22 @@ public Integer getOldestVersion() {

/**
* @return Time of secret update as raw string representation.
* @deprecated Method left for backwards compatibility only. Use {@link #getUpdatedTime()} instead.
*/
@Deprecated(since = "1.2", forRemoval = true)
public String getUpdatedTimeString() {
return updatedTime;
if (updatedTime != null) {
return TIME_FORMAT.format(updatedTime);
}

return null;
}

/**
* @return Time of secret update..
* @return Time of secret update.
*/
public ZonedDateTime getUpdatedTime() {
if (updatedTime != null && !updatedTime.isEmpty()) {
try {
return ZonedDateTime.parse(updatedTime, TIME_FORMAT);
} catch (DateTimeParseException e) {
// Ignore.
}
}

return null;
return updatedTime;
}

/**
Expand All @@ -137,7 +132,7 @@ public boolean equals(Object o) {
return false;
}
SecretMetadata that = (SecretMetadata) o;
return Objects.equals(createdTimeString, that.createdTimeString) &&
return Objects.equals(createdTime, that.createdTime) &&
Objects.equals(currentVersion, that.currentVersion) &&
Objects.equals(maxVersions, that.maxVersions) &&
Objects.equals(oldestVersion, that.oldestVersion) &&
Expand All @@ -147,6 +142,6 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return Objects.hash(createdTimeString, currentVersion, maxVersions, oldestVersion, updatedTime, versions);
return Objects.hash(createdTime, currentVersion, maxVersions, oldestVersion, updatedTime, versions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.Serializable;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -34,7 +35,9 @@
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public final class TokenData implements Serializable {
private static final long serialVersionUID = 2915180734313753649L;
private static final long serialVersionUID = -5749716740973138916L;

private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSSXXX");

@JsonProperty("accessor")
private String accessor;
Expand All @@ -52,7 +55,7 @@ public final class TokenData implements Serializable {
private String entityId;

@JsonProperty("expire_time")
private String expireTime;
private ZonedDateTime expireTime;

@JsonProperty("explicit_max_ttl")
private Integer explicitMaxTtl;
Expand All @@ -61,7 +64,7 @@ public final class TokenData implements Serializable {
private String id;

@JsonProperty("issue_time")
private String issueTime;
private ZonedDateTime issueTime;

@JsonProperty("meta")
private Map<String, Object> meta;
Expand Down Expand Up @@ -126,21 +129,23 @@ public String getEntityId() {
/**
* @return Expire time as raw string value
* @since 0.9
* @deprecated Method left for backwards compatibility only. Use {@link #getExpireTime()} instead.
*/
@Deprecated(since = "1.2", forRemoval = true)
public String getExpireTimeString() {
return expireTime;
if (expireTime != null) {
return TIME_FORMAT.format(expireTime);
}

return null;
}

/**
* @return Expire time (parsed)
* @since 0.9
*/
public ZonedDateTime getExpireTime() {
if (expireTime == null) {
return null;
} else {
return ZonedDateTime.parse(expireTime);
}
return expireTime;
}

/**
Expand All @@ -161,21 +166,23 @@ public String getId() {
/**
* @return Issue time as raw string value
* @since 0.9
* @deprecated Method left for backwards compatibility only. Use {@link #getIssueTime()} instead.
*/
@Deprecated(since = "1.2", forRemoval = true)
public String getIssueTimeString() {
return issueTime;
if (issueTime != null) {
return TIME_FORMAT.format(issueTime);
}

return null;
}

/**
* @return Expire time (parsed)
* @since 0.9
*/
public ZonedDateTime getIssueTime() {
if (issueTime == null) {
return null;
} else {
return ZonedDateTime.parse(issueTime);
}
return issueTime;
}

/**
Expand Down
Loading

0 comments on commit f8bddf7

Please sign in to comment.