Advertisement
tpeierls

ResourceException test

Jan 24th, 2012
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 8.25 KB | None | 0 0
  1. package com.example.restlet.status;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.restlet.*;
  6. import org.restlet.data.*;
  7. import org.restlet.representation.*;
  8. import org.restlet.resource.*;
  9. import org.restlet.routing.*;
  10. import org.restlet.service.*;
  11.  
  12. import org.junit.*;
  13. import static org.junit.Assert.*;
  14.  
  15.  
  16. /**
  17.  * Demonstrates that error status description is not passed through the
  18.  * client resource proxy mechanism when the server side throws a resource
  19.  * exception, although status code and reason phrase are. Also demonstrates
  20.  * workaround using custom StatusService and client-side handling.
  21.  */
  22. public class ResourceExceptionTest extends Application {
  23.  
  24.     /**
  25.      * Expect largeGet() and largePost() test methods to fail when
  26.      * this is false. Setting it true works around the limitation,
  27.      * and no tests should fail.
  28.      */
  29.     static final boolean USE_ENTITY_FOR_DESCRIPTION = true;
  30.  
  31.  
  32.     /**
  33.      * Just runs the server, so you can use things like curl
  34.      * to see what is being returned.
  35.      */
  36.     public static void main(String... args) throws Exception {
  37.         ResourceExceptionTest re = new ResourceExceptionTest();
  38.         re.startComponent();
  39.     }
  40.  
  41.     @Before public void startComponent() throws Exception {
  42.         component = new Component();
  43.         component.getServers().add(Protocol.HTTP, 8111);
  44.         component.getDefaultHost().attachDefault(this);
  45.         component.start();
  46.         clientResource = new ClientResource("http://localhost:8111");
  47.         lastResourceException = null;
  48.     }
  49.  
  50.     @After public void stopComponent() throws Exception {
  51.         component.stop();
  52.     }
  53.  
  54.     @Test public void smallGet() {
  55.         assertEquals("got 123", getStorageResource("/store/123").getSize());
  56.     }
  57.  
  58.     @Test public void smallPost() {
  59.         assertEquals("added 123", getStorageResource("/store").addToStore("123"));
  60.     }
  61.  
  62.     @Test public void badGet() {
  63.         try {
  64.             getStorageResource("/store/badval").getSize();
  65.             fail("Should throw ResourceException");
  66.         } catch (ResourceException ex) {
  67.             Status status = ex.getStatus();
  68.             assertEquals(500, status.getCode());
  69.             assertEquals("Internal Server Error", status.getReasonPhrase());
  70.             assertEquals("Internal Server Error", status.getDescription());
  71.             assertNull(status.getThrowable());
  72.             assertNull(lastResourceException);
  73.         }
  74.     }
  75.  
  76.     @Test public void badPost() {
  77.         try {
  78.             getStorageResource("/store").addToStore("badval");
  79.             fail("Should throw ResourceException");
  80.         } catch (ResourceException ex) {
  81.             Status status = ex.getStatus();
  82.             assertEquals(500, status.getCode());
  83.             assertEquals("Internal Server Error", status.getReasonPhrase());
  84.             assertEquals("Internal Server Error", status.getDescription());
  85.             assertNull(status.getThrowable());
  86.             assertNull(lastResourceException);
  87.         }
  88.     }
  89.  
  90.     @Test public void largeGet() {
  91.         try {
  92.             getStorageResource("/store/1234").getSize();
  93.             fail("Should throw ResourceException");
  94.         } catch (ResourceException ex) {
  95.             Status status = ex.getStatus();
  96.  
  97.             assertEquals(507, status.getCode());
  98.             assertEquals("Insufficient Storage", status.getReasonPhrase());
  99.             assertEquals("Insufficient Storage", status.getDescription());
  100.             assertNull(status.getThrowable());
  101.  
  102.             assertNotNull(lastResourceException);
  103.             status = lastResourceException.getStatus();
  104.             assertEquals(507, status.getCode());
  105.             assertEquals("Insufficient Storage", status.getReasonPhrase());
  106.             assertEquals("Size 1234 too large", getDescription(status));
  107.             assertNotNull(status.getThrowable());
  108.         }
  109.     }
  110.  
  111.     @Test public void largePost() {
  112.         try {
  113.             getStorageResource("/store").addToStore("1234");
  114.             fail("Should throw ResourceException");
  115.         } catch (ResourceException ex) {
  116.             Status status = ex.getStatus();
  117.  
  118.             assertEquals(507, status.getCode());
  119.             assertEquals("Insufficient Storage", status.getReasonPhrase());
  120.             assertEquals("Insufficient Storage", status.getDescription());
  121.             assertNull(status.getThrowable());
  122.  
  123.             assertNotNull(lastResourceException);
  124.             status = lastResourceException.getStatus();
  125.             assertEquals(507, status.getCode());
  126.             assertEquals("Insufficient Storage", status.getReasonPhrase());
  127.             assertEquals("Size 1234 too large", getDescription(status));
  128.             assertNotNull(status.getThrowable());
  129.         }
  130.     }
  131.  
  132.  
  133.     StorageResource getStorageResource(String uri) {
  134.         // XXX Stash storageResource for use by getDescription. Not good practice!
  135.         return storageResource = clientResource.getChild(uri, StorageResource.class);
  136.     }
  137.  
  138.     String getDescription(Status status) {
  139.         if (USE_ENTITY_FOR_DESCRIPTION) {
  140.             try {
  141.                 // XXX Retrieve stashed storageResource. Not good practice!
  142.                 ClientResource clientResource =
  143.                     ((ClientProxy) storageResource).getClientResource();
  144.                 Representation rep = clientResource.getResponse().getEntity();
  145.                 return rep.getText();
  146.             } catch (IOException ex) {
  147.                 return "IOException getting error description from response entity";
  148.             }
  149.         } else {
  150.             return status.getDescription();
  151.         }
  152.     }
  153.  
  154.  
  155.     @Override public Restlet createInboundRoot() {
  156.         Router router = new Router(getContext());
  157.         router.attach("/store", StorageServerResource.class);
  158.         router.attach("/store/{size}", StorageServerResource.class);
  159.         return router;
  160.     }
  161.  
  162.     public static class StorageException extends Exception {
  163.         StorageException(int n) { super("Size " + n + " too large"); }
  164.     }
  165.  
  166.     public interface StorageResource {
  167.         @Get String getSize();
  168.         @Post String addToStore(String size);
  169.     }
  170.  
  171.     public static class StorageServerResource extends ServerResource
  172.             implements StorageResource {
  173.  
  174.         public String getSize() {
  175.             return doMethod(getRequestAttributes().get("size").toString(), "got");
  176.         }
  177.  
  178.         public String addToStore(String size) {
  179.             return doMethod(size, "added");
  180.         }
  181.  
  182.         String doMethod(String size, String action) {
  183.             Integer n = Integer.valueOf(size);
  184.             try {
  185.                 checkSize(n);
  186.                 return action + " " + n;
  187.             } catch (StorageException ex) {
  188.                 throw lastResourceException = new ResourceException(
  189.                     new Status(Status.SERVER_ERROR_INSUFFICIENT_STORAGE, ex));
  190.             }
  191.         }
  192.  
  193.         void checkSize(int n) throws StorageException {
  194.             if (n > 1000) {
  195.                 throw new StorageException(n);
  196.             }
  197.         }
  198.     }
  199.  
  200.     public ResourceExceptionTest() {
  201.         setStatusService(new StatusService() {
  202.             @Override public Status getStatus(
  203.                     Throwable throwable, Request request, Response response) {
  204.  
  205.                 if (throwable instanceof ResourceException) {
  206.                     Throwable cause = ((ResourceException) throwable).getCause();
  207.                     if (cause instanceof StorageException) {
  208.                         // Custom handling when caused by a StorageException.
  209.                         response.setEntity(new StringRepresentation(cause.getMessage()));
  210.                         return Status.SERVER_ERROR_INSUFFICIENT_STORAGE;
  211.                     }
  212.                 }
  213.                 // Otherwise use standard handling.
  214.                 return super.getStatus(throwable, request, response);
  215.             }
  216.  
  217.             @Override public Representation getRepresentation(
  218.                     Status status, Request request, Response response) {
  219.  
  220.                 return response.getEntity();
  221.             }
  222.         });
  223.     }
  224.  
  225.     Component component;
  226.     ClientResource clientResource;
  227.     StorageResource storageResource;
  228.     static volatile ResourceException lastResourceException;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement