From 6b2ff2b6d55b825a969dc32cc3e77499c4dca1d5 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Fri, 31 Oct 2014 10:23:59 -0700 Subject: [PATCH] Update fcr:fixity to produce asynchronous responses --- .../org/fcrepo/http/api/FedoraFixity.java | 21 +++- .../org/fcrepo/http/api/FedoraFixityTest.java | 102 +++++++++++++++++- 2 files changed, 115 insertions(+), 8 deletions(-) diff --git a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraFixity.java b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraFixity.java index bd7ae26c71..9b888504d0 100644 --- a/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraFixity.java +++ b/fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraFixity.java @@ -34,6 +34,8 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.container.AsyncResponse; +import javax.ws.rs.container.Suspended; import com.google.common.annotations.VisibleForTesting; import org.fcrepo.http.commons.responses.HtmlTemplate; @@ -80,7 +82,6 @@ public FedoraFixity(final String externalPath) { * * GET /path/to/some/datastream/fcr:fixity * - * @return datastream fixity in the given format */ @GET @Timed @@ -88,15 +89,25 @@ public FedoraFixity(final String externalPath) { @Produces({TURTLE + ";qs=10", JSON_LD + ";qs=8", N3, N3_ALT2, RDF_XML, NTRIPLES, APPLICATION_XML, TEXT_PLAIN, TURTLE_X, TEXT_HTML, APPLICATION_XHTML_XML, "*/*"}) - public RdfStream getDatastreamFixity() { + public void getDatastreamFixity(@Suspended final AsyncResponse asyncResponse) { if (!(resource() instanceof FedoraBinary)) { throw new NotFoundException(resource() + " is not a binary"); } - return ((FedoraBinary)resource()).getFixity(translator()) - .topic(translator().reverse().convert(resource()).asNode()) - .session(session); + new Thread(new Runnable() { + @Override + public void run() { + final RdfStream result = getFixityStream(); + asyncResponse.resume(result); + } + + private RdfStream getFixityStream() { + return ((FedoraBinary)resource()).getFixity(translator()) + .topic(translator().reverse().convert(resource()).asNode()) + .session(session); + } + }).start(); } diff --git a/fcrepo-http-api/src/test/java/org/fcrepo/http/api/FedoraFixityTest.java b/fcrepo-http-api/src/test/java/org/fcrepo/http/api/FedoraFixityTest.java index 0706c9954e..1bd1eca8f1 100644 --- a/fcrepo-http-api/src/test/java/org/fcrepo/http/api/FedoraFixityTest.java +++ b/fcrepo-http-api/src/test/java/org/fcrepo/http/api/FedoraFixityTest.java @@ -28,6 +28,8 @@ import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.jcr.Session; +import javax.ws.rs.container.AsyncResponse; +import javax.ws.rs.container.TimeoutHandler; import javax.ws.rs.core.Request; import javax.ws.rs.core.UriInfo; @@ -38,6 +40,11 @@ import org.junit.Test; import org.mockito.Mock; +import java.util.Collection; +import java.util.Date; +import java.util.Map; +import java.util.concurrent.TimeUnit; + /** *

FedoraFixityTest class.

* @@ -76,13 +83,102 @@ public void setUp() throws RepositoryException { } @Test - public void testGetDatastreamFixity() throws RepositoryException { + public void testGetDatastreamFixity() throws RepositoryException, InterruptedException { final RdfStream expected = new RdfStream(); when(mockBinary.getFixity(any(IdentifierConverter.class))).thenReturn(expected); - final RdfStream actual = testObj.getDatastreamFixity(); + final TestAsyncResponse asyncResponse = new TestAsyncResponse(); + + + synchronized (asyncResponse) { + testObj.getDatastreamFixity(asyncResponse); + asyncResponse.wait(1000); + } + + assertEquals(expected, asyncResponse.getResponse()); + } + - assertEquals(expected, actual); + private class TestAsyncResponse implements AsyncResponse { + private Object response; + + public Object getResponse() { + return response; + } + + @Override + public boolean resume(final Object o) { + synchronized (this) { + this.response = o; + notify(); + } + return true; + } + + @Override + public boolean resume(final Throwable throwable) { + return false; + } + + @Override + public boolean cancel() { + return false; + } + + @Override + public boolean cancel(final int i) { + return false; + } + + @Override + public boolean cancel(final Date date) { + return false; + } + + @Override + public boolean isSuspended() { + return false; + } + + @Override + public boolean isCancelled() { + return false; + } + + @Override + public boolean isDone() { + return false; + } + + @Override + public boolean setTimeout(final long l, final TimeUnit timeUnit) { + return false; + } + + @Override + public void setTimeoutHandler(final TimeoutHandler timeoutHandler) { + + } + + @Override + public Collection> register(final Class aClass) { + return null; + } + + @Override + public Map, Collection>> register(final Class aClass, final Class... classes) { + return null; + } + + @Override + public Collection> register(final Object o) { + return null; + } + + @Override + public Map, Collection>> register(final Object o, final Object... objects) { + return null; + } } }