-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathIdempotentSequence.java
More file actions
353 lines (316 loc) · 9 KB
/
IdempotentSequence.java
File metadata and controls
353 lines (316 loc) · 9 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* @(#)IdempotentSequence.java 0.3-3 06/05/2001
*
* This file is part of the HTTPClient package
* Copyright (C) 1996-2001 Ronald Tschal�r
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307, USA
*
* For questions, suggestions, bug-reports, enhancement-requests etc.
* I may be contacted at:
*
* ronald@innovation.ch
*
* The HTTPClient's home page is located at:
*
* http://www.innovation.ch/java/HTTPClient/
*
*/
package HTTPClient;
import java.util.Hashtable;
import java.util.Enumeration;
/**
* <P>This class checks whether a sequence of requests is idempotent. This
* is used to determine which requests may be automatically retried. This
* class also serves as a central place to record which methods have side
* effects and which methods are idempotent.
*
* <P>Note: unknown methods (i.e. a method which is not HEAD, GET, POST, PUT,
* DELETE, OPTIONS, TRACE, PROPFIND, PROPPATCH, MKCOL, COPY, MOVE, LOCK, or
* UNLOCK) are treated conservatively, meaning they are assumed to have side
* effects and are not idempotent.
*
* <P>Usage:
* <PRE>
* IdempotentSequence seq = new IdempotentSequence();
* seq.add(r1);
* ...
* if (seq.isIdempotent(r1)) ...
* ...
* </PRE>
*
* @version 0.3-3 06/05/2001
* @author Ronald Tschal�r
*/
class IdempotentSequence
{
/** method number definitions */
private static final int UNKNOWN = 0,
HEAD = 1,
GET = 2,
POST = 3,
PUT = 4,
DELETE = 5,
OPTIONS = 6,
TRACE = 7,
// DAV methods
PROPFIND = 8,
PROPPATCH = 9,
MKCOL = 10,
COPY = 11,
MOVE = 12,
LOCK = 13,
UNLOCK = 14;
/** these are the history of previous requests */
private int[] m_history;
private String[] r_history;
private int m_len, r_len;
/** trigger analysis of threads */
private boolean analysis_done = false;
private Hashtable threads = new Hashtable();
// Constructors
/**
* Start a new sequence of requests.
*/
public IdempotentSequence()
{
m_history = new int[10];
r_history = new String[10];
m_len = 0;
r_len = 0;
}
// Methods
/**
* Add the request to the end of the list of requests. This is used
* to build the complete sequence of requests before determining
* whether the sequence is idempotent.
*
* @param req the next request
*/
public void add(Request req)
{
if (m_len >= m_history.length)
m_history = Util.resizeArray(m_history, m_history.length+10);
m_history[m_len++] = methodNum(req.getMethod());
if (r_len >= r_history.length)
r_history = Util.resizeArray(r_history, r_history.length+10);
r_history[r_len++] = req.getRequestURI();
}
/**
* Is this request part of an idempotent sequence? This method <em>must
* not</em> be called before all requests have been added to this
* sequence; similarly, <var>add()</var> <em>must not</em> be called
* after this method was invoked.
*
* <P>We split up the sequence of requests into individual sub-sequences,
* or threads, with all requests in a thread having the same request-URI
* and no two threads having the same request-URI. Each thread is then
* marked as idempotent or not according to the following rules:
*
* <OL>
* <LI>If any method is UNKNOWN then the thread is not idempotent;
* <LI>else, if no method has side effects then the thread is idempotent;
* <LI>else, if the first method has side effects and is complete then
* the thread is idempotent;
* <LI>else, if the first method has side effects, is not complete,
* and no other method has side effects then the thread is idempotent;
* <LI>else the thread is not idempotent.
* </OL>
*
* <P>The major assumption here is that the side effects of any method
* only apply to resource specified. E.g. a <tt>"PUT /barbara.html"</tt>
* will only affect the resource "/barbara.html" and nothing else.
* This assumption is violated by POST of course; however, POSTs are
* not pipelined and will therefore never show up here.
*
* @param req the request
*/
public boolean isIdempotent(Request req)
{
if (!analysis_done)
do_analysis();
return ((Boolean)threads.get(req.getRequestURI())).booleanValue();
}
private static final Object INDET = new Object();
@SuppressWarnings("unchecked")
private void do_analysis()
{
for (int idx=0; idx<r_len; idx++)
{
Object t_state = threads.get(r_history[idx]);
if (m_history[idx] == UNKNOWN)
threads.put(r_history[idx], Boolean.FALSE);
else if (t_state == null) // new thread
{
if (methodHasSideEffects(m_history[idx]))
{
if (methodIsComplete(m_history[idx])) // is idempotent
threads.put(r_history[idx], Boolean.TRUE);
else
threads.put(r_history[idx], Boolean.FALSE);
}
else // indeterminate
threads.put(r_history[idx], INDET);
}
else // update thread
{
if (t_state == INDET && methodHasSideEffects(m_history[idx]))
threads.put(r_history[idx], Boolean.FALSE);
}
}
// any thread still indeterminate must be idempotent
Enumeration te = threads.keys();
while (te.hasMoreElements())
{
String res = (String) te.nextElement();
if (threads.get(res) == INDET)
threads.put(res, Boolean.TRUE);
}
}
/**
* A method is idempotent if the side effects of N identical
* requests is the same as for a single request (Section 9.1.2
* of RFC-????).
*
* @return true if method is idempotent
*/
public static boolean methodIsIdempotent(String method)
{
return methodIsIdempotent(methodNum(method));
}
private static boolean methodIsIdempotent(int method)
{
switch (method)
{
case HEAD:
case GET:
case PUT:
case DELETE:
case OPTIONS:
case TRACE:
case PROPFIND:
case PROPPATCH:
case COPY:
case MOVE:
return true;
case UNKNOWN:
case POST:
case MKCOL:
case LOCK:
case UNLOCK:
default:
return false;
}
}
/**
* A method is complete if any side effects of the request affect
* the complete resource. For example, a PUT is complete but a
* PUT with byte-ranges wouldn't be. In essence, if a request uses
* a method which has side effects and is complete then the state
* of the resource after the request is independent of the state of
* the resource before the request.
*
* @return true if method is complete
*/
public static boolean methodIsComplete(String method)
{
return methodIsComplete(methodNum(method));
}
private static boolean methodIsComplete(int method)
{
switch (method)
{
case HEAD:
case GET:
case PUT:
case DELETE:
case OPTIONS:
case TRACE:
case PROPFIND:
case COPY:
case MOVE:
case LOCK:
case UNLOCK:
return true;
case UNKNOWN:
case POST:
case PROPPATCH:
case MKCOL:
default:
return false;
}
}
public static boolean methodHasSideEffects(String method)
{
return methodHasSideEffects(methodNum(method));
}
private static boolean methodHasSideEffects(int method)
{
switch (method)
{
case HEAD:
case GET:
case OPTIONS:
case TRACE:
case PROPFIND:
case LOCK:
case UNLOCK:
return false;
case UNKNOWN:
case POST:
case PUT:
case DELETE:
case PROPPATCH:
case MKCOL:
case COPY:
case MOVE:
default:
return true;
}
}
private static int methodNum(String method)
{
if (method.equals("GET"))
return GET;
if (method.equals("POST"))
return POST;
if (method.equals("HEAD"))
return HEAD;
if (method.equals("PUT"))
return PUT;
if (method.equals("DELETE"))
return DELETE;
if (method.equals("OPTIONS"))
return OPTIONS;
if (method.equals("TRACE"))
return TRACE;
if (method.equals("PROPFIND"))
return PROPFIND;
if (method.equals("PROPPATCH"))
return PROPPATCH;
if (method.equals("MKCOL"))
return MKCOL;
if (method.equals("COPY"))
return COPY;
if (method.equals("MOVE"))
return MOVE;
if (method.equals("LOCK"))
return LOCK;
if (method.equals("UNLOCK"))
return UNLOCK;
return UNKNOWN;
}
}