1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tuscany.sca.binding.rest;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
/**
* A class to store cache settings for Atom and HTTP requests and responses.
*
* Predicates are statements that work in conjunction with
* ETags and LastModified dates to determine if a precondition
* or postcondition is satisfied.
* See HTTP specification for how predicates wrk:
* http://tools.ietf.org/html/rfc2616
* Example predicates in HTTP include If-Match, If-None-Match,
* If-Modified-Since, If-Unmodified-Since, If-Range.
*/
public class RESTCacheContext {
public static final SimpleDateFormat RFC822DateFormat = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss Z" ); // RFC 822 date time
public boolean enabled;
public String eTag;
public String lastModified;
public Date lastModifiedDate;
public boolean ifModifiedSince;
public boolean ifUnmodifiedSince;
public boolean ifMatch;
public boolean ifNoneMatch;
public boolean ifRange;
/**
* An ETag is a unique ID for an item. It changes when
* a field in the item or the update date changes.
* See HTTP specification for how ETags work:
* http://tools.ietf.org/html/rfc2616
* @return the eTag
*/
public String getETag() {
return eTag;
}
/**
* @param tag the eTag to set
*/
public void setETag(String tag) {
eTag = tag;
enabled = true;
}
/**
* The LastModified date is the time the item was last
* changed. See HTTP specification for how ETags work:
* http://tools.ietf.org/html/rfc2616
* @return the lastModified
*/
public String getLastModified() {
return lastModified;
}
/**
* The LastModified date is the time the item was last
* changed. See HTTP specification for how ETags work:
* http://tools.ietf.org/html/rfc2616
* @return the lastModified
*/
public Date getLastModifiedAsDate() {
return lastModifiedDate;
}
/**
* @param lastModified the lastModified to set
*/
public void setLastModified(String lastModified) throws java.text.ParseException {
this.lastModified = lastModified;
// Catch date formatting on input to help debugging.
lastModifiedDate = RFC822DateFormat.parse( lastModified );
enabled = true;
}
/**
* @param lastModified the lastModified to set
*/
public void setLastModified(Date updated) {
this.lastModified = RFC822DateFormat.format( updated );
lastModifiedDate = updated;
enabled = true;
}
/**
* @return the ifModifedSince
*/
public boolean isIfModifiedSince() {
return ifModifiedSince;
}
/**
* @param ifModifedSince the ifModifedSince to set
*/
public void setIfModifiedSince(boolean ifModifiedSince) {
this.ifModifiedSince = ifModifiedSince;
if ( ifModifiedSince )
enabled = true;
}
/**
* @return the ifUnModifiedSince
*/
public boolean isIfUnmodifiedSince() {
return ifUnmodifiedSince;
}
/**
* @param ifUnModifiedSince the ifUnModifiedSince to set
*/
public void setIfUnmodifiedSince(boolean ifUnmodifiedSince) {
this.ifUnmodifiedSince = ifUnmodifiedSince;
if ( ifUnmodifiedSince )
enabled = true;
}
/**
* @return the ifMatch
*/
public boolean isIfMatch() {
return ifMatch;
}
/**
* @param ifMatch the ifMatch to set
*/
public void setIfMatch(boolean ifMatch) {
this.ifMatch = ifMatch;
if ( ifMatch )
enabled = true;
}
/**
* @return the ifNoneMatch
*/
public boolean isIfNoneMatch() {
return ifNoneMatch;
}
/**
* @param ifNoneMatch the ifNoneMatch to set
*/
public void setIfNoneMatch(boolean ifNoneMatch) {
this.ifNoneMatch = ifNoneMatch;
if ( ifNoneMatch )
enabled = true;
}
/**
* @return the ifRange
*/
public boolean isIfRange() {
return ifRange;
}
/**
* @param ifRange the ifRange to set
*/
public void setIfRange(boolean ifRange) {
this.ifRange = ifRange;
if ( ifRange )
enabled = true;
}
public String toString() {
final String PREDPREFIX = ", predicates=";
StringBuffer sb = new StringBuffer(PREDPREFIX);
if ( ifMatch || ifNoneMatch || ifModifiedSince || ifUnmodifiedSince || ifRange ) {
if ( ifMatch ) {
if ( sb.length() > PREDPREFIX.length() ) sb.append( ", ");
sb.append("If-Match");
}
if ( ifNoneMatch ) {
if ( sb.length() > PREDPREFIX.length() ) sb.append( ", ");
sb.append("If-None-Match");
}
if ( ifModifiedSince ) {
if ( sb.length() > PREDPREFIX.length() ) sb.append( ", ");
sb.append("If-Modified-Since");
}
if ( ifUnmodifiedSince ) {
if ( sb.length() > PREDPREFIX.length() ) sb.append( ", ");
sb.append("If-UnModified-Since");
}
if ( ifRange ) {
if ( sb.length() > PREDPREFIX.length() ) sb.append( ", ");
sb.append("If-Range");
}
} else {
sb.append("null");
}
return "eTag=" + eTag + ", lastModified=" + lastModified
+ sb.toString();
}
/**
* Gets the cache context information (ETag, LastModified, predicates) from the Http request.
* @param request
* @return
*/
public static RESTCacheContext getCacheContextFromRequest( HttpServletRequest request ) throws java.text.ParseException {
RESTCacheContext context = new RESTCacheContext();
String eTag = request.getHeader( "If-Match" );
if ( eTag != null ) {
context.setETag( eTag );
context.setIfMatch( true );
}
eTag = request.getHeader( "If-None-Match" );
if ( eTag != null ) {
context.setETag( eTag );
context.setIfNoneMatch( true );
}
String lastModifiedString = request.getHeader( "If-Modified-Since" );
if ( lastModifiedString != null ) {
context.setLastModified( lastModifiedString );
context.setIfModifiedSince( true );
}
lastModifiedString = request.getHeader( "If-Unmodified-Since" );
if ( lastModifiedString != null ) {
context.setLastModified( lastModifiedString );
context.setIfUnmodifiedSince( true );
}
lastModifiedString = request.getHeader( "If-Range" );
if ( lastModifiedString != null ) {
context.setLastModified( lastModifiedString );
context.setIfRange( true );
}
return context;
}
/**
* Enabled is true whenever ETag, LastModified, or predicate is set.
* @return the enabled
*/
public boolean isEnabled() {
return enabled;
}
/**
* @param enabled the enabled to set
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
|