Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.restlet.status;
- import java.io.IOException;
- import org.restlet.*;
- import org.restlet.data.*;
- import org.restlet.representation.*;
- import org.restlet.resource.*;
- import org.restlet.routing.*;
- import org.restlet.service.*;
- import org.junit.*;
- import static org.junit.Assert.*;
- /**
- * Demonstrates that error status description is not passed through the
- * client resource proxy mechanism when the server side throws a resource
- * exception, although status code and reason phrase are. Also demonstrates
- * workaround using custom StatusService and client-side handling.
- */
- public class ResourceExceptionTest extends Application {
- /**
- * Expect largeGet() and largePost() test methods to fail when
- * this is false. Setting it true works around the limitation,
- * and no tests should fail.
- */
- static final boolean USE_ENTITY_FOR_DESCRIPTION = true;
- /**
- * Just runs the server, so you can use things like curl
- * to see what is being returned.
- */
- public static void main(String... args) throws Exception {
- ResourceExceptionTest re = new ResourceExceptionTest();
- re.startComponent();
- }
- @Before public void startComponent() throws Exception {
- component = new Component();
- component.getServers().add(Protocol.HTTP, 8111);
- component.getDefaultHost().attachDefault(this);
- component.start();
- clientResource = new ClientResource("http://localhost:8111");
- lastResourceException = null;
- }
- @After public void stopComponent() throws Exception {
- component.stop();
- }
- @Test public void smallGet() {
- assertEquals("got 123", getStorageResource("/store/123").getSize());
- }
- @Test public void smallPost() {
- assertEquals("added 123", getStorageResource("/store").addToStore("123"));
- }
- @Test public void badGet() {
- try {
- getStorageResource("/store/badval").getSize();
- fail("Should throw ResourceException");
- } catch (ResourceException ex) {
- Status status = ex.getStatus();
- assertEquals(500, status.getCode());
- assertEquals("Internal Server Error", status.getReasonPhrase());
- assertEquals("Internal Server Error", status.getDescription());
- assertNull(status.getThrowable());
- assertNull(lastResourceException);
- }
- }
- @Test public void badPost() {
- try {
- getStorageResource("/store").addToStore("badval");
- fail("Should throw ResourceException");
- } catch (ResourceException ex) {
- Status status = ex.getStatus();
- assertEquals(500, status.getCode());
- assertEquals("Internal Server Error", status.getReasonPhrase());
- assertEquals("Internal Server Error", status.getDescription());
- assertNull(status.getThrowable());
- assertNull(lastResourceException);
- }
- }
- @Test public void largeGet() {
- try {
- getStorageResource("/store/1234").getSize();
- fail("Should throw ResourceException");
- } catch (ResourceException ex) {
- Status status = ex.getStatus();
- assertEquals(507, status.getCode());
- assertEquals("Insufficient Storage", status.getReasonPhrase());
- assertEquals("Insufficient Storage", status.getDescription());
- assertNull(status.getThrowable());
- assertNotNull(lastResourceException);
- status = lastResourceException.getStatus();
- assertEquals(507, status.getCode());
- assertEquals("Insufficient Storage", status.getReasonPhrase());
- assertEquals("Size 1234 too large", getDescription(status));
- assertNotNull(status.getThrowable());
- }
- }
- @Test public void largePost() {
- try {
- getStorageResource("/store").addToStore("1234");
- fail("Should throw ResourceException");
- } catch (ResourceException ex) {
- Status status = ex.getStatus();
- assertEquals(507, status.getCode());
- assertEquals("Insufficient Storage", status.getReasonPhrase());
- assertEquals("Insufficient Storage", status.getDescription());
- assertNull(status.getThrowable());
- assertNotNull(lastResourceException);
- status = lastResourceException.getStatus();
- assertEquals(507, status.getCode());
- assertEquals("Insufficient Storage", status.getReasonPhrase());
- assertEquals("Size 1234 too large", getDescription(status));
- assertNotNull(status.getThrowable());
- }
- }
- StorageResource getStorageResource(String uri) {
- // XXX Stash storageResource for use by getDescription. Not good practice!
- return storageResource = clientResource.getChild(uri, StorageResource.class);
- }
- String getDescription(Status status) {
- if (USE_ENTITY_FOR_DESCRIPTION) {
- try {
- // XXX Retrieve stashed storageResource. Not good practice!
- ClientResource clientResource =
- ((ClientProxy) storageResource).getClientResource();
- Representation rep = clientResource.getResponse().getEntity();
- return rep.getText();
- } catch (IOException ex) {
- return "IOException getting error description from response entity";
- }
- } else {
- return status.getDescription();
- }
- }
- @Override public Restlet createInboundRoot() {
- Router router = new Router(getContext());
- router.attach("/store", StorageServerResource.class);
- router.attach("/store/{size}", StorageServerResource.class);
- return router;
- }
- public static class StorageException extends Exception {
- StorageException(int n) { super("Size " + n + " too large"); }
- }
- public interface StorageResource {
- @Get String getSize();
- @Post String addToStore(String size);
- }
- public static class StorageServerResource extends ServerResource
- implements StorageResource {
- public String getSize() {
- return doMethod(getRequestAttributes().get("size").toString(), "got");
- }
- public String addToStore(String size) {
- return doMethod(size, "added");
- }
- String doMethod(String size, String action) {
- Integer n = Integer.valueOf(size);
- try {
- checkSize(n);
- return action + " " + n;
- } catch (StorageException ex) {
- throw lastResourceException = new ResourceException(
- new Status(Status.SERVER_ERROR_INSUFFICIENT_STORAGE, ex));
- }
- }
- void checkSize(int n) throws StorageException {
- if (n > 1000) {
- throw new StorageException(n);
- }
- }
- }
- public ResourceExceptionTest() {
- setStatusService(new StatusService() {
- @Override public Status getStatus(
- Throwable throwable, Request request, Response response) {
- if (throwable instanceof ResourceException) {
- Throwable cause = ((ResourceException) throwable).getCause();
- if (cause instanceof StorageException) {
- // Custom handling when caused by a StorageException.
- response.setEntity(new StringRepresentation(cause.getMessage()));
- return Status.SERVER_ERROR_INSUFFICIENT_STORAGE;
- }
- }
- // Otherwise use standard handling.
- return super.getStatus(throwable, request, response);
- }
- @Override public Representation getRepresentation(
- Status status, Request request, Response response) {
- return response.getEntity();
- }
- });
- }
- Component component;
- ClientResource clientResource;
- StorageResource storageResource;
- static volatile ResourceException lastResourceException;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement