Skip to content

Commit

Permalink
prove we're receiving webhook callbacks; serialize the body as Legacy…
Browse files Browse the repository at this point in the history
… JMS ATOM.
  • Loading branch information
cbeer committed Mar 12, 2013
1 parent 66aaaeb commit 24157f1
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
6 changes: 6 additions & 0 deletions fcrepo-webhooks/pom.xml
Expand Up @@ -31,6 +31,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-http-api</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-jms</artifactId>
Expand Down
Expand Up @@ -25,6 +25,7 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -124,7 +125,7 @@ public Response showWebhooks() throws RepositoryException {

@POST
@Path("{id}")
public Response registerWebhook(@PathParam("id") final String id, @QueryParam("callbackUrl") final String callbackUrl) throws RepositoryException {
public Response registerWebhook(@PathParam("id") final String id, @FormParam("callbackUrl") final String callbackUrl) throws RepositoryException {

final Session session = repo.login();

Expand All @@ -141,18 +142,11 @@ public Response registerWebhook(@PathParam("id") final String id, @QueryParam("c


@Subscribe
public void newEvent(Event event) {
public void newEvent(FedoraEvent event) {
try {
final Node resource = jcrTools.findOrCreateNode(readOnlySession, event.getPath());
final boolean isDatastreamNode =
FedoraTypesUtils.isFedoraDatastream.apply(resource);
final boolean isObjectNode =
FedoraTypesUtils.isFedoraObject.apply(resource) &&
!isDatastreamNode;

if(isDatastreamNode || isObjectNode) {
runHooks(resource, new FedoraEvent(event));
}

runHooks(resource, event);
} catch (RepositoryException e) {
e.printStackTrace();
}
Expand Down
@@ -1,17 +1,24 @@
package org.fcrepo.webhooks;


import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.util.regex.Pattern.DOTALL;
import static java.util.regex.Pattern.compile;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@ContextConfiguration({"/spring-test/repo.xml", "/spring-test/rest.xml",
Expand All @@ -20,8 +27,16 @@ public class FedoraWebhooksTest extends AbstractResourceTest {

@Test
public void registerWebhookCallbackTest() throws IOException {
assertEquals(201, getStatus(new HttpPost(serverAddress +
"/webhooks/callback_id?callbackUrl=info:fedora/fake:url")));
HttpPost method = new HttpPost(serverAddress +
"/webhooks/callback_id");

List<NameValuePair> formparams = new ArrayList<NameValuePair>();

formparams.add(new BasicNameValuePair("callbackUrl", "info:fedora/fake:url"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
method.setEntity(entity);

assertEquals(201, getStatus(method));


final HttpGet displayWebhooks =
Expand All @@ -37,5 +52,46 @@ public void registerWebhookCallbackTest() throws IOException {
.find());


}

@Test
public void FireWebhooksTest() throws IOException {

HttpPost method = new HttpPost(serverAddress +
"/webhooks/callback_id");

List<NameValuePair> formparams = new ArrayList<NameValuePair>();

formparams.add(new BasicNameValuePair("callbackUrl", serverAddress + "/dummy"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
method.setEntity(entity);

assertEquals(201, getStatus(method));

HttpPost create_method = new HttpPost(serverAddress +
"/objects/new");
assertEquals(201, getStatus(create_method));

try {
for(int i = 0; i < 5; i++) {

Thread.sleep(200);

if(TestEndpoint.lastBody != null) {
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println(TestEndpoint.lastBody);

assertNotNull("Our webhook wasn't called!", TestEndpoint.lastBody);
assertTrue("Our webhook didn't have the content we expected!", compile(
"ingest", DOTALL).matcher(TestEndpoint.lastBody)
.find());


}
}
@@ -0,0 +1,21 @@
package org.fcrepo.webhooks;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import static javax.ws.rs.core.Response.ok;

@Path("/dummy")
public class TestEndpoint {

public static String lastBody;

@POST
public Response dummyWebhookEndpoint(String body) {

TestEndpoint.lastBody = body;
return ok().build();
}

}
2 changes: 2 additions & 0 deletions fcrepo-webhooks/src/test/resources/spring-test/rest.xml
Expand Up @@ -21,7 +21,9 @@

<jaxrs:server address="http://localhost:${test.port:8080}">
<jaxrs:serviceBeans>
<bean class="org.fcrepo.api.FedoraObjects"/>
<bean class="org.fcrepo.webhooks.FedoraWebhooks"/>
<bean class="org.fcrepo.webhooks.TestEndpoint"/>
</jaxrs:serviceBeans>
</jaxrs:server>

Expand Down
3 changes: 2 additions & 1 deletion fcrepo-webhooks/src/test/resources/test_repository.json
Expand Up @@ -14,5 +14,6 @@
"providers" : [
{ "classname" : "servlet" }
]
}
},
"node-types" : ["fedora-node-types.cnd"]
}

0 comments on commit 24157f1

Please sign in to comment.