Skip to content

Commit 83ff0ad

Browse files
committed
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
1 parent de9c9fe commit 83ff0ad

File tree

1 file changed

+26
-14
lines changed
  • spring-webmvc/src/main/java/org/springframework/web/servlet

1 file changed

+26
-14
lines changed

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import org.springframework.util.ObjectUtils;
2424
import org.springframework.util.StringUtils;
2525

26+
2627
/**
2728
* A FlashMap provides a way for one request to store attributes intended for
2829
* 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
5051

5152
private String targetRequestPath;
5253

53-
private final MultiValueMap<String, String> targetRequestParams = new LinkedMultiValueMap<String, String>();
54-
55-
private long expirationStartTime;
54+
private final MultiValueMap<String, String> targetRequestParams = new LinkedMultiValueMap<String, String>(4);
5655

57-
private int timeToLive;
56+
private long expirationTime = -1;
5857

5958

6059
/**
@@ -112,17 +111,33 @@ public MultiValueMap<String, String> getTargetRequestParams() {
112111
* @param timeToLive the number of seconds before expiration
113112
*/
114113
public void startExpirationPeriod(int timeToLive) {
115-
this.expirationStartTime = System.currentTimeMillis();
116-
this.timeToLive = timeToLive;
114+
this.expirationTime = System.currentTimeMillis() + timeToLive * 1000;
115+
}
116+
117+
/**
118+
* Set the expiration time for the FlashMap. This is provided for serialization
119+
* purposes but can also be used instead {@link #startExpirationPeriod(int)}.
120+
* @since 4.2
121+
*/
122+
public void setExpirationTime(long expirationTime) {
123+
this.expirationTime = expirationTime;
124+
}
125+
126+
/**
127+
* Return the expiration time for the FlashMap or -1 if the expiration
128+
* period has not started.
129+
* @since 4.2
130+
*/
131+
public long getExpirationTime() {
132+
return this.expirationTime;
117133
}
118134

119135
/**
120136
* Return whether this instance has expired depending on the amount of
121137
* elapsed time since the call to {@link #startExpirationPeriod}.
122138
*/
123139
public boolean isExpired() {
124-
return (this.expirationStartTime != 0 &&
125-
(System.currentTimeMillis() - this.expirationStartTime > this.timeToLive * 1000));
140+
return (this.expirationTime != -1 && System.currentTimeMillis() > this.expirationTime);
126141
}
127142

128143

@@ -167,11 +182,8 @@ public int hashCode() {
167182

168183
@Override
169184
public String toString() {
170-
StringBuilder sb = new StringBuilder();
171-
sb.append("FlashMap [attributes=").append(super.toString());
172-
sb.append(", targetRequestPath=").append(this.targetRequestPath);
173-
sb.append(", targetRequestParams=").append(this.targetRequestParams).append("]");
174-
return sb.toString();
185+
return "FlashMap [attributes=" + super.toString() + ", targetRequestPath=" +
186+
this.targetRequestPath + ", targetRequestParams=" + this.targetRequestParams + "]";
175187
}
176188

177189
}

0 commit comments

Comments
 (0)