Browse Source

Add accessors for expirationTime in FlashMap

FlashMap now has a single field reflecting the expiration time and
also provides accessors that can be used for serialization purposes.

Issue: SPR-12757
pull/754/head
Rossen Stoyanchev 10 years ago
parent
commit
83ff0adc2e
  1. 40
      spring-webmvc/src/main/java/org/springframework/web/servlet/FlashMap.java

40
spring-webmvc/src/main/java/org/springframework/web/servlet/FlashMap.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import org.springframework.util.MultiValueMap; @@ -23,6 +23,7 @@ import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
/**
* A FlashMap provides a way for one request to store attributes intended for
* use in another. This is most commonly needed when redirecting from one URL
@ -50,11 +51,9 @@ public final class FlashMap extends HashMap<String, Object> implements Comparabl @@ -50,11 +51,9 @@ public final class FlashMap extends HashMap<String, Object> implements Comparabl
private String targetRequestPath;
private final MultiValueMap<String, String> targetRequestParams = new LinkedMultiValueMap<String, String>();
private long expirationStartTime;
private final MultiValueMap<String, String> targetRequestParams = new LinkedMultiValueMap<String, String>(4);
private int timeToLive;
private long expirationTime = -1;
/**
@ -112,8 +111,25 @@ public final class FlashMap extends HashMap<String, Object> implements Comparabl @@ -112,8 +111,25 @@ public final class FlashMap extends HashMap<String, Object> implements Comparabl
* @param timeToLive the number of seconds before expiration
*/
public void startExpirationPeriod(int timeToLive) {
this.expirationStartTime = System.currentTimeMillis();
this.timeToLive = timeToLive;
this.expirationTime = System.currentTimeMillis() + timeToLive * 1000;
}
/**
* Set the expiration time for the FlashMap. This is provided for serialization
* purposes but can also be used instead {@link #startExpirationPeriod(int)}.
* @since 4.2
*/
public void setExpirationTime(long expirationTime) {
this.expirationTime = expirationTime;
}
/**
* Return the expiration time for the FlashMap or -1 if the expiration
* period has not started.
* @since 4.2
*/
public long getExpirationTime() {
return this.expirationTime;
}
/**
@ -121,8 +137,7 @@ public final class FlashMap extends HashMap<String, Object> implements Comparabl @@ -121,8 +137,7 @@ public final class FlashMap extends HashMap<String, Object> implements Comparabl
* elapsed time since the call to {@link #startExpirationPeriod}.
*/
public boolean isExpired() {
return (this.expirationStartTime != 0 &&
(System.currentTimeMillis() - this.expirationStartTime > this.timeToLive * 1000));
return (this.expirationTime != -1 && System.currentTimeMillis() > this.expirationTime);
}
@ -167,11 +182,8 @@ public final class FlashMap extends HashMap<String, Object> implements Comparabl @@ -167,11 +182,8 @@ public final class FlashMap extends HashMap<String, Object> implements Comparabl
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("FlashMap [attributes=").append(super.toString());
sb.append(", targetRequestPath=").append(this.targetRequestPath);
sb.append(", targetRequestParams=").append(this.targetRequestParams).append("]");
return sb.toString();
return "FlashMap [attributes=" + super.toString() + ", targetRequestPath=" +
this.targetRequestPath + ", targetRequestParams=" + this.targetRequestParams + "]";
}
}

Loading…
Cancel
Save