|
1 | 1 | /* |
2 | | - * Copyright 2002-2014 the original author or authors. |
| 2 | + * Copyright 2002-2015 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
23 | 23 | import org.springframework.util.ObjectUtils; |
24 | 24 | import org.springframework.util.StringUtils; |
25 | 25 |
|
| 26 | + |
26 | 27 | /** |
27 | 28 | * A FlashMap provides a way for one request to store attributes intended for |
28 | 29 | * 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 | 51 |
|
51 | 52 | private String targetRequestPath; |
52 | 53 |
|
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); |
56 | 55 |
|
57 | | - private int timeToLive; |
| 56 | + private long expirationTime = -1; |
58 | 57 |
|
59 | 58 |
|
60 | 59 | /** |
@@ -112,17 +111,33 @@ public MultiValueMap<String, String> getTargetRequestParams() { |
112 | 111 | * @param timeToLive the number of seconds before expiration |
113 | 112 | */ |
114 | 113 | 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; |
117 | 133 | } |
118 | 134 |
|
119 | 135 | /** |
120 | 136 | * Return whether this instance has expired depending on the amount of |
121 | 137 | * elapsed time since the call to {@link #startExpirationPeriod}. |
122 | 138 | */ |
123 | 139 | 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); |
126 | 141 | } |
127 | 142 |
|
128 | 143 |
|
@@ -167,11 +182,8 @@ public int hashCode() { |
167 | 182 |
|
168 | 183 | @Override |
169 | 184 | 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 + "]"; |
175 | 187 | } |
176 | 188 |
|
177 | 189 | } |
0 commit comments