1 28 29 package HTTPClient; 30 31 32 import java.util.Hashtable ; 33 import java.util.Enumeration ; 34 35 57 58 class IdempotentSequence 59 { 60 61 private static final int UNKNOWN = 0, 62 HEAD = 1, 63 GET = 2, 64 POST = 3, 65 PUT = 4, 66 DELETE = 5, 67 OPTIONS = 6, 68 TRACE = 7; 69 70 71 private int[] m_history; 72 private String [] r_history; 73 private int m_len, r_len; 74 75 76 private boolean analysis_done = false; 77 private Hashtable threads = new Hashtable (); 78 79 80 82 85 public IdempotentSequence() 86 { 87 m_history = new int[10]; 88 r_history = new String [10]; 89 m_len = 0; 90 r_len = 0; 91 } 92 93 94 96 103 public void add(Request req) 104 { 105 if (m_len >= m_history.length) 106 m_history = Util.resizeArray(m_history, m_history.length+10); 107 m_history[m_len++] = methodNum(req.getMethod()); 108 109 if (r_len >= r_history.length) 110 r_history = Util.resizeArray(r_history, r_history.length+10); 111 r_history[r_len++] = req.getRequestURI(); 112 } 113 114 115 144 public boolean isIdempotent(Request req) 145 { 146 if (!analysis_done) 147 do_analysis(); 148 149 return ((Boolean ) threads.get(req.getRequestURI())).booleanValue(); 150 } 151 152 153 private static final Object INDET = new Object (); 154 155 private void do_analysis() 156 { 157 for (int idx=0; idx<r_len; idx++) 158 { 159 Object t_state = threads.get(r_history[idx]); 160 161 if (m_history[idx] == UNKNOWN) 162 threads.put(r_history[idx], Boolean.FALSE); 163 else if (t_state == null) { 165 if (methodHasSideEffects(m_history[idx]) && 166 methodIsComplete(m_history[idx])) threads.put(r_history[idx], Boolean.TRUE); 168 else threads.put(r_history[idx], INDET); 170 } 171 else { 173 if (t_state == INDET && methodHasSideEffects(m_history[idx])) 174 threads.put(r_history[idx], Boolean.FALSE); 175 } 176 } 177 178 Enumeration te = threads.keys(); 180 while (te.hasMoreElements()) 181 { 182 String res = (String ) te.nextElement(); 183 if (threads.get(res) == INDET) 184 threads.put(res, Boolean.TRUE); 185 } 186 } 187 188 189 196 public static boolean methodIsIdempotent(String method) 197 { 198 return methodIsIdempotent(methodNum(method)); 199 } 200 201 202 private static boolean methodIsIdempotent(int method) 203 { 204 switch (method) 205 { 206 case HEAD: 207 case GET: 208 case PUT: 209 case DELETE: 210 case OPTIONS: 211 case TRACE: 212 return true; 213 default: 214 return false; 215 } 216 } 217 218 219 229 public static boolean methodIsComplete(String method) 230 { 231 return methodIsComplete(methodNum(method)); 232 } 233 234 235 private static boolean methodIsComplete(int method) 236 { 237 switch (method) 238 { 239 case HEAD: 240 case GET: 241 case PUT: 242 case DELETE: 243 case OPTIONS: 244 case TRACE: 245 return true; 246 default: 247 return false; 248 } 249 } 250 251 252 public static boolean methodHasSideEffects(String method) 253 { 254 return methodHasSideEffects(methodNum(method)); 255 } 256 257 258 private static boolean methodHasSideEffects(int method) 259 { 260 switch (method) 261 { 262 case HEAD: 263 case GET: 264 case OPTIONS: 265 case TRACE: 266 return false; 267 default: 268 return true; 269 } 270 } 271 272 273 private static int methodNum(String method) 274 { 275 if (method.equals("GET")) 276 return GET; 277 if (method.equals("POST")) 278 return POST; 279 if (method.equals("HEAD")) 280 return HEAD; 281 if (method.equals("PUT")) 282 return PUT; 283 if (method.equals("DELETE")) 284 return DELETE; 285 if (method.equals("OPTIONS")) 286 return OPTIONS; 287 if (method.equals("TRACE")) 288 return TRACE; 289 290 return UNKNOWN; 291 } 292 293 294 297 public static void main(String args[]) 298 { 299 IdempotentSequence seq = new IdempotentSequence(); 300 301 seq.add(new Request(null, "GET", "/b1", null, null, null, false)); 302 seq.add(new Request(null, "PUT", "/b2", null, null, null, false)); 303 seq.add(new Request(null, "GET", "/b1", null, null, null, false)); 304 seq.add(new Request(null, "PUT", "/b3", null, null, null, false)); 305 seq.add(new Request(null, "GET", "/b2", null, null, null, false)); 306 seq.add(new Request(null, "PUT", "/b3", null, null, null, false)); 307 seq.add(new Request(null, "GET", "/b1", null, null, null, false)); 308 seq.add(new Request(null, "TRACE", "/b4", null, null, null, false)); 309 seq.add(new Request(null, "LINK", "/b4", null, null, null, false)); 310 seq.add(new Request(null, "GET", "/b4", null, null, null, false)); 311 seq.add(new Request(null, "PUT", "/b5", null, null, null, false)); 312 seq.add(new Request(null, "HEAD", "/b5", null, null, null, false)); 313 seq.add(new Request(null, "PUT", "/b5", null, null, null, false)); 314 seq.add(new Request(null, "GET", "/b6", null, null, null, false)); 315 seq.add(new Request(null, "DELETE", "/b6", null, null, null, false)); 316 seq.add(new Request(null, "HEAD", "/b6", null, null, null, false)); 317 seq.add(new Request(null, "OPTIONS", "/b7", null, null, null, false)); 318 seq.add(new Request(null, "TRACE", "/b7", null, null, null, false)); 319 seq.add(new Request(null, "GET", "/b7", null, null, null, false)); 320 seq.add(new Request(null, "PUT", "/b7", null, null, null, false)); 321 322 if (!seq.isIdempotent(new Request(null, null, "/b1", null, null, null, false))) 323 System.err.println("Sequence b1 failed"); 324 if (!seq.isIdempotent(new Request(null, null, "/b2", null, null, null, false))) 325 System.err.println("Sequence b2 failed"); 326 if (!seq.isIdempotent(new Request(null, null, "/b3", null, null, null, false))) 327 System.err.println("Sequence b3 failed"); 328 if (seq.isIdempotent(new Request(null, null, "/b4", null, null, null, false))) 329 System.err.println("Sequence b4 failed"); 330 if (!seq.isIdempotent(new Request(null, null, "/b5", null, null, null, false))) 331 System.err.println("Sequence b5 failed"); 332 if (seq.isIdempotent(new Request(null, null, "/b6", null, null, null, false))) 333 System.err.println("Sequence b6 failed"); 334 if (seq.isIdempotent(new Request(null, null, "/b7", null, null, null, false))) 335 System.err.println("Sequence b7 failed"); 336 337 System.out.println("Tests finished"); 338 } 339 } 340 341 | Popular Tags |