| 1 |
/* |
| 2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 |
* |
| 4 |
* Copyright (c) 2010-2011 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* The contents of this file are subject to the terms of either the GNU |
| 7 |
* General Public License Version 2 only ("GPL") or the Common Development |
| 8 |
* and Distribution License("CDDL") (collectively, the "License"). You |
| 9 |
* may not use this file except in compliance with the License. You can |
| 10 |
* obtain a copy of the License at |
| 11 |
* http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 |
* or packager/legal/LICENSE.txt. See the License for the specific |
| 13 |
* language governing permissions and limitations under the License. |
| 14 |
* |
| 15 |
* When distributing the software, include this License Header Notice in each |
| 16 |
* file and include the License file at packager/legal/LICENSE.txt. |
| 17 |
* |
| 18 |
* GPL Classpath Exception: |
| 19 |
* Oracle designates this particular file as subject to the "Classpath" |
| 20 |
* exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 |
* file that accompanied this code. |
| 22 |
* |
| 23 |
* Modifications: |
| 24 |
* If applicable, add the following below the License Header, with the fields |
| 25 |
* enclosed by brackets [] replaced by your own identifying information: |
| 26 |
* "Portions Copyright [year] [name of copyright owner]" |
| 27 |
* |
| 28 |
* Contributor(s): |
| 29 |
* If you wish your version of this file to be governed by only the CDDL or |
| 30 |
* only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 |
* elects to include this software in this distribution under the [CDDL or GPL |
| 32 |
* Version 2] license." If you don't indicate a single choice of license, a |
| 33 |
* recipient has the option to distribute your version of this file under |
| 34 |
* either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 |
* its licensees as provided above. However, if you add GPL Version 2 code |
| 36 |
* and therefore, elected the GPL Version 2 license, then the option applies |
| 37 |
* only if the new code is made subject to such option by the copyright |
| 38 |
* holder. |
| 39 |
*/ |
| 40 |
|
| 41 |
package com.sun.jersey.impl.client; |
| 42 |
|
| 43 |
import com.sun.jersey.api.client.async.TypeListener; |
| 44 |
import com.sun.jersey.api.client.AsyncWebResource; |
| 45 |
import com.sun.jersey.api.client.Client; |
| 46 |
import com.sun.jersey.api.client.ClientResponse; |
| 47 |
import com.sun.jersey.api.client.UniformInterfaceException; |
| 48 |
import com.sun.jersey.impl.container.grizzly2.AbstractGrizzlyServerTester; |
| 49 |
import java.util.ArrayList; |
| 50 |
import java.util.Collections; |
| 51 |
import java.util.List; |
| 52 |
import java.util.concurrent.CancellationException; |
| 53 |
import java.util.concurrent.CountDownLatch; |
| 54 |
import java.util.concurrent.ExecutionException; |
| 55 |
import java.util.concurrent.ExecutorService; |
| 56 |
import java.util.concurrent.Future; |
| 57 |
import java.util.concurrent.TimeUnit; |
| 58 |
import javax.ws.rs.Path; |
| 59 |
import javax.ws.rs.DELETE; |
| 60 |
import javax.ws.rs.GET; |
| 61 |
import javax.ws.rs.POST; |
| 62 |
import javax.ws.rs.PUT; |
| 63 |
|
| 64 |
/** |
| 65 |
* |
| 66 |
* @author [email protected] |
| 67 |
*/ |
| 68 |
public class AsyncHttpMethodTest extends AbstractGrizzlyServerTester { |
| 69 |
@Path("/test") |
| 70 |
public static class HttpMethodResource { |
| 71 |
@GET |
| 72 |
public String get() { |
| 73 |
sleep(); |
| 74 |
return "GET"; |
| 75 |
} |
| 76 |
|
| 77 |
@POST |
| 78 |
public String post(String entity) { |
| 79 |
sleep(); |
| 80 |
return entity; |
| 81 |
} |
| 82 |
|
| 83 |
@PUT |
| 84 |
public String put(String entity) { |
| 85 |
sleep(); |
| 86 |
return entity; |
| 87 |
} |
| 88 |
|
| 89 |
@DELETE |
| 90 |
public String delete() { |
| 91 |
sleep(); |
| 92 |
return "DELETE"; |
| 93 |
} |
| 94 |
|
| 95 |
@Path("wait") |
| 96 |
@GET |
| 97 |
public String getWait() throws InterruptedException { |
| 98 |
sleep(); |
| 99 |
sleep(); |
| 100 |
sleep(); |
| 101 |
sleep(); |
| 102 |
return "GET"; |
| 103 |
} |
| 104 |
|
| 105 |
private void sleep() { |
| 106 |
try { |
| 107 |
Thread.sleep(1000); |
| 108 |
} catch (InterruptedException ex) { |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
abstract class X<T> extends TypeListener<T> { |
| 114 |
final List<String> l = new ArrayList<String>(); |
| 115 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 116 |
|
| 117 |
X(Class<T> c) { |
| 118 |
super(c); |
| 119 |
} |
| 120 |
|
| 121 |
public void check(String s) throws InterruptedException { |
| 122 |
cdl.await(); |
| 123 |
assertEquals(1, l.size()); |
| 124 |
assertEquals(s, l.get(0)); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
class StringListener extends X<String> { |
| 129 |
StringListener() { |
| 130 |
super(String.class); |
| 131 |
} |
| 132 |
|
| 133 |
public void onComplete(Future<String> f) throws InterruptedException { |
| 134 |
try { |
| 135 |
l.add(f.get()); |
| 136 |
} catch (ExecutionException ex) { |
| 137 |
throw new IllegalStateException(); |
| 138 |
} finally { |
| 139 |
cdl.countDown(); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
class ClientResponseListener extends X<ClientResponse> { |
| 145 |
ClientResponseListener() { |
| 146 |
super(ClientResponse.class); |
| 147 |
} |
| 148 |
|
| 149 |
public void onComplete(Future<ClientResponse> f) throws InterruptedException { |
| 150 |
try { |
| 151 |
l.add(f.get().getEntity(String.class)); |
| 152 |
} catch (ExecutionException ex) { |
| 153 |
throw new IllegalStateException(); |
| 154 |
} finally { |
| 155 |
cdl.countDown(); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public AsyncHttpMethodTest(String testName) { |
| 161 |
super(testName); |
| 162 |
} |
| 163 |
|
| 164 |
public void testGet() throws Exception { |
| 165 |
startServer(HttpMethodResource.class); |
| 166 |
Client c = Client.create(); |
| 167 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 168 |
assertEquals("GET", r.get(String.class).get()); |
| 169 |
|
| 170 |
terminate(c.getExecutorService()); |
| 171 |
} |
| 172 |
|
| 173 |
public void testGetNotFound() throws Exception { |
| 174 |
startServer(HttpMethodResource.class); |
| 175 |
Client c = Client.create(); |
| 176 |
AsyncWebResource r = c.asyncResource(getUri().path("404").build()); |
| 177 |
|
| 178 |
boolean caught = false; |
| 179 |
try { |
| 180 |
r.get(String.class).get(); |
| 181 |
} catch (ExecutionException ex) { |
| 182 |
caught = ex.getCause() instanceof UniformInterfaceException; |
| 183 |
} |
| 184 |
assertTrue(caught); |
| 185 |
|
| 186 |
terminate(c.getExecutorService()); |
| 187 |
} |
| 188 |
|
| 189 |
public void testGetListener() throws Exception { |
| 190 |
startServer(HttpMethodResource.class); |
| 191 |
|
| 192 |
Client c = Client.create(); |
| 193 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 194 |
|
| 195 |
StringListener l = new StringListener(); |
| 196 |
Future<?> f = r.get(l); |
| 197 |
|
| 198 |
assertEquals("GET", f.get()); |
| 199 |
l.check("GET"); |
| 200 |
|
| 201 |
terminate(c.getExecutorService()); |
| 202 |
} |
| 203 |
|
| 204 |
public void testGetClientResponseListener() throws Exception { |
| 205 |
startServer(HttpMethodResource.class); |
| 206 |
|
| 207 |
Client c = Client.create(); |
| 208 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 209 |
|
| 210 |
ClientResponseListener l = new ClientResponseListener(); |
| 211 |
Future<?> f = r.get(l); |
| 212 |
|
| 213 |
assertTrue(ClientResponse.class.isAssignableFrom(f.get().getClass())); |
| 214 |
l.check("GET"); |
| 215 |
|
| 216 |
terminate(c.getExecutorService()); |
| 217 |
} |
| 218 |
|
| 219 |
public void testGetListenerNotFound() throws Exception { |
| 220 |
startServer(HttpMethodResource.class); |
| 221 |
|
| 222 |
Client c = Client.create(); |
| 223 |
AsyncWebResource r = c.asyncResource(getUri().path("404").build()); |
| 224 |
|
| 225 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 226 |
final List<String> l = new ArrayList<String>(); |
| 227 |
Future<?> f = r.get(new TypeListener<String>(String.class) { |
| 228 |
public void onComplete(Future<String> t) throws InterruptedException { |
| 229 |
try { |
| 230 |
l.add(t.get()); |
| 231 |
} catch (ExecutionException ex) { |
| 232 |
if (ex.getCause() instanceof UniformInterfaceException) { |
| 233 |
UniformInterfaceException uie = (UniformInterfaceException)ex.getCause(); |
| 234 |
|
| 235 |
l.add("" + uie.getResponse().getStatus()); |
| 236 |
} |
| 237 |
cdl.countDown(); |
| 238 |
} |
| 239 |
} |
| 240 |
}); |
| 241 |
|
| 242 |
boolean caught = false; |
| 243 |
try { |
| 244 |
f.get(); |
| 245 |
} catch (ExecutionException ex) { |
| 246 |
caught = ex.getCause() instanceof UniformInterfaceException; |
| 247 |
} |
| 248 |
assertTrue(caught); |
| 249 |
|
| 250 |
cdl.await(); |
| 251 |
assertEquals(1, l.size()); |
| 252 |
assertEquals("404", l.get(0)); |
| 253 |
|
| 254 |
terminate(c.getExecutorService()); |
| 255 |
} |
| 256 |
|
| 257 |
public void testGetClientResponseListenerNotFound() throws Exception { |
| 258 |
startServer(HttpMethodResource.class); |
| 259 |
|
| 260 |
Client c = Client.create(); |
| 261 |
AsyncWebResource r = c.asyncResource(getUri().path("404").build()); |
| 262 |
|
| 263 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 264 |
final List<String> l = new ArrayList<String>(); |
| 265 |
Future<?> f = r.get(new TypeListener<ClientResponse>(ClientResponse.class) { |
| 266 |
public void onComplete(Future<ClientResponse> t) throws InterruptedException { |
| 267 |
try { |
| 268 |
l.add("" + t.get().getStatus()); |
| 269 |
} catch (ExecutionException ex) { |
| 270 |
throw new IllegalStateException(); |
| 271 |
} finally { |
| 272 |
cdl.countDown(); |
| 273 |
} |
| 274 |
} |
| 275 |
}); |
| 276 |
|
| 277 |
boolean caught = false; |
| 278 |
try { |
| 279 |
f.get(); |
| 280 |
} catch (ExecutionException ex) { |
| 281 |
caught = true; |
| 282 |
} |
| 283 |
assertFalse(caught); |
| 284 |
|
| 285 |
cdl.await(); |
| 286 |
assertEquals(1, l.size()); |
| 287 |
assertEquals("404", l.get(0)); |
| 288 |
|
| 289 |
terminate(c.getExecutorService()); |
| 290 |
} |
| 291 |
|
| 292 |
public void testPost() throws Exception { |
| 293 |
startServer(HttpMethodResource.class); |
| 294 |
Client c = Client.create(); |
| 295 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 296 |
assertEquals("POST", r.post(String.class, "POST").get()); |
| 297 |
|
| 298 |
terminate(c.getExecutorService()); |
| 299 |
} |
| 300 |
|
| 301 |
public void testPostListener() throws Exception { |
| 302 |
startServer(HttpMethodResource.class); |
| 303 |
|
| 304 |
Client c = Client.create(); |
| 305 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 306 |
|
| 307 |
StringListener l = new StringListener(); |
| 308 |
Future<?> f = r.post(l, "POST"); |
| 309 |
|
| 310 |
assertEquals("POST", f.get()); |
| 311 |
l.check("POST"); |
| 312 |
|
| 313 |
terminate(c.getExecutorService()); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
public void testPut() throws Exception { |
| 318 |
startServer(HttpMethodResource.class); |
| 319 |
Client c = Client.create(); |
| 320 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 321 |
assertEquals("PUT", r.put(String.class, "PUT").get()); |
| 322 |
|
| 323 |
terminate(c.getExecutorService()); |
| 324 |
} |
| 325 |
|
| 326 |
public void testPutListener() throws Exception { |
| 327 |
startServer(HttpMethodResource.class); |
| 328 |
|
| 329 |
Client c = Client.create(); |
| 330 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 331 |
|
| 332 |
StringListener l = new StringListener(); |
| 333 |
Future<?> f = r.put(l, "PUT"); |
| 334 |
|
| 335 |
assertEquals("PUT", f.get()); |
| 336 |
l.check("PUT"); |
| 337 |
|
| 338 |
terminate(c.getExecutorService()); |
| 339 |
} |
| 340 |
|
| 341 |
public void testDelete() throws Exception { |
| 342 |
startServer(HttpMethodResource.class); |
| 343 |
Client c = Client.create(); |
| 344 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 345 |
assertEquals("DELETE", r.delete(String.class).get()); |
| 346 |
|
| 347 |
terminate(c.getExecutorService()); |
| 348 |
} |
| 349 |
|
| 350 |
public void testDeleteListener() throws Exception { |
| 351 |
startServer(HttpMethodResource.class); |
| 352 |
|
| 353 |
Client c = Client.create(); |
| 354 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 355 |
|
| 356 |
StringListener l = new StringListener(); |
| 357 |
Future<?> f = r.delete(l); |
| 358 |
|
| 359 |
assertEquals("DELETE", f.get()); |
| 360 |
l.check("DELETE"); |
| 361 |
|
| 362 |
terminate(c.getExecutorService()); |
| 363 |
} |
| 364 |
|
| 365 |
public void testAllSequential() throws Exception { |
| 366 |
startServer(HttpMethodResource.class); |
| 367 |
Client c = Client.create(); |
| 368 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 369 |
assertEquals("GET", r.get(String.class).get()); |
| 370 |
|
| 371 |
r = Client.create().asyncResource(getUri().path("test").build()); |
| 372 |
assertEquals("POST", r.post(String.class, "POST").get()); |
| 373 |
|
| 374 |
r = Client.create().asyncResource(getUri().path("test").build()); |
| 375 |
assertEquals("PUT", r.put(String.class, "PUT").get()); |
| 376 |
|
| 377 |
r = Client.create().asyncResource(getUri().path("test").build()); |
| 378 |
assertEquals("DELETE", r.delete(String.class).get()); |
| 379 |
|
| 380 |
terminate(c.getExecutorService()); |
| 381 |
} |
| 382 |
|
| 383 |
public void testAllParallel() throws Exception { |
| 384 |
startServer(HttpMethodResource.class); |
| 385 |
Client c = Client.create(); |
| 386 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 387 |
|
| 388 |
Future<String> get = r.get(String.class); |
| 389 |
Future<String> post = r.post(String.class, "POST"); |
| 390 |
Future<String> put = r.put(String.class, "PUT"); |
| 391 |
Future<String> delete = r.delete(String.class); |
| 392 |
|
| 393 |
assertEquals("GET", get.get()); |
| 394 |
assertEquals("POST", post.get()); |
| 395 |
assertEquals("PUT", put.get()); |
| 396 |
assertEquals("DELETE", delete.get()); |
| 397 |
|
| 398 |
terminate(c.getExecutorService()); |
| 399 |
} |
| 400 |
|
| 401 |
public void testAllParallelListener() throws Exception { |
| 402 |
startServer(HttpMethodResource.class); |
| 403 |
Client c = Client.create(); |
| 404 |
AsyncWebResource r = c.asyncResource(getUri().path("test").build()); |
| 405 |
|
| 406 |
final CountDownLatch cdl = new CountDownLatch(4); |
| 407 |
final List<String> l = Collections.synchronizedList(new ArrayList<String>()); |
| 408 |
TypeListener<String> al = new TypeListener<String>(String.class) { |
| 409 |
|
| 410 |
public void onComplete(Future<String> t) throws InterruptedException { |
| 411 |
try { |
| 412 |
l.add(t.get()); |
| 413 |
cdl.countDown(); |
| 414 |
} catch (ExecutionException ex) { |
| 415 |
throw new IllegalStateException(); |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
}; |
| 420 |
|
| 421 |
List<Future<String>> lf = new ArrayList<Future<String>>(); |
| 422 |
|
| 423 |
lf.add(r.get(al)); |
| 424 |
lf.add(r.post(al, "POST")); |
| 425 |
lf.add(r.put(al, "PUT")); |
| 426 |
lf.add(r.delete(al)); |
| 427 |
|
| 428 |
cdl.await(); |
| 429 |
|
| 430 |
assertEquals(4, l.size()); |
| 431 |
assertTrue(l.contains("GET")); |
| 432 |
assertTrue(l.contains("POST")); |
| 433 |
assertTrue(l.contains("PUT")); |
| 434 |
assertTrue(l.contains("DELETE")); |
| 435 |
|
| 436 |
assertEquals("GET", lf.get(0).get()); |
| 437 |
assertEquals("POST", lf.get(1).get()); |
| 438 |
assertEquals("PUT", lf.get(2).get()); |
| 439 |
assertEquals("DELETE", lf.get(3).get()); |
| 440 |
|
| 441 |
terminate(c.getExecutorService()); |
| 442 |
} |
| 443 |
|
| 444 |
public void testCancelListener() throws Exception { |
| 445 |
startServer(HttpMethodResource.class); |
| 446 |
|
| 447 |
Client c = Client.create(); |
| 448 |
AsyncWebResource r = c.asyncResource(getUri().path("wait").build()); |
| 449 |
|
| 450 |
final CountDownLatch cdl = new CountDownLatch(1); |
| 451 |
|
| 452 |
final List<Boolean> l = new ArrayList<Boolean>(1); |
| 453 |
Future<?> f = r.get(new TypeListener<String>(String.class) { |
| 454 |
public void onComplete(Future<String> t) throws InterruptedException { |
| 455 |
try { |
| 456 |
t.get(); |
| 457 |
} catch (CancellationException ex) { |
| 458 |
l.add(true); |
| 459 |
} catch (ExecutionException ex) { |
| 460 |
throw new IllegalStateException(); |
| 461 |
} finally { |
| 462 |
cdl.countDown(); |
| 463 |
} |
| 464 |
} |
| 465 |
}); |
| 466 |
|
| 467 |
f.cancel(true); |
| 468 |
|
| 469 |
cdl.await(); |
| 470 |
assertTrue(f.isCancelled()); |
| 471 |
assertEquals(1, l.size()); |
| 472 |
assertTrue(l.get(0)); |
| 473 |
|
| 474 |
terminate(c.getExecutorService()); |
| 475 |
} |
| 476 |
|
| 477 |
private void terminate(ExecutorService es) throws Exception { |
| 478 |
es.shutdown(); |
| 479 |
assertTrue(es.awaitTermination(100, TimeUnit.MILLISECONDS)); |
| 480 |
assertTrue(es.isTerminated()); |
| 481 |
} |
| 482 |
} |