Skip to content

Commit

Permalink
add test to prove we registered a webhook succesfully
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Mar 12, 2013
1 parent c23bdaa commit 66aaaeb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 9 deletions.
3 changes: 2 additions & 1 deletion fcrepo-webhooks/pom.xml
Expand Up @@ -33,9 +33,10 @@

<dependency>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo-kernel</artifactId>
<artifactId>fcrepo-jms</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
Expand Down
Expand Up @@ -5,9 +5,13 @@
import com.google.common.eventbus.Subscribe;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.fcrepo.AbstractResource;
import org.fcrepo.messaging.legacy.LegacyMethod;
import org.fcrepo.observer.FedoraEvent;
import org.fcrepo.utils.FedoraTypesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -21,16 +25,19 @@
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.Event;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import java.io.IOException;
import java.io.StringWriter;
import java.util.concurrent.TimeUnit;

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

@Path("/webhooks")
public class FedoraWebhooks extends AbstractResource {
Expand Down Expand Up @@ -73,13 +80,23 @@ public void initialize() throws LoginException, NoSuchWorkspaceException,
session.logout();
}

public static void runHooks(final Node resource) throws RepositoryException {
final NodeIterator webhooksIterator = resource.getSession().getRootNode().getNodes("webhooks:*");
public static void runHooks(final Node resource, final FedoraEvent event) throws RepositoryException {
final NodeIterator webhooksIterator = resource.getSession().getRootNode().getNodes("webhook:*");

while(webhooksIterator.hasNext()) {
final Node hook = webhooksIterator.nextNode();
final String callbackUrl = hook.getProperty("webhook:callbackUrl").getString();
HttpPost method = new HttpPost(callbackUrl);
LegacyMethod eventSerialization = new LegacyMethod(event, resource);
StringWriter writer = new StringWriter();

try {
eventSerialization.writeTo(writer);
method.setEntity(new StringEntity(writer.toString()));
} catch (IOException e) {
e.printStackTrace();
}

try {
logger.debug("Firing callback for" + hook.getName());
client.execute(method);
Expand All @@ -90,14 +107,28 @@ public static void runHooks(final Node resource) throws RepositoryException {
}
}

@GET
public Response showWebhooks() throws RepositoryException {

final NodeIterator webhooksIterator = readOnlySession.getRootNode().getNodes("webhook:*");
StringBuilder str = new StringBuilder();

while(webhooksIterator.hasNext()) {
final Node hook = webhooksIterator.nextNode();
final String callbackUrl = hook.getProperty("webhook:callbackUrl").getString();
str.append(callbackUrl + ", ");
}

return ok(str.toString()).build();
}

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

final Session session = repo.login();

Node n = jcrTools.findOrCreateChild(session.getRootNode(), "webhook:" + id);
n.addMixin("webhook:callback");
Node n = jcrTools.findOrCreateChild(session.getRootNode(), "webhook:" + id, "webhook:callback");

n.setProperty("webhook:callbackUrl", callbackUrl);

Expand All @@ -112,7 +143,16 @@ public Response registerWebhook(@PathParam("id") final String id, @QueryParam("c
@Subscribe
public void newEvent(Event event) {
try {
runHooks(jcrTools.findOrCreateNode(readOnlySession, event.getPath()));
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));
}
} catch (RepositoryException e) {
e.printStackTrace();
}
Expand Down
4 changes: 2 additions & 2 deletions fcrepo-webhooks/src/main/resources/webhooks.cnd
Expand Up @@ -8,5 +8,5 @@
*/
[webhook:resource] mixin abstract

[webhook:callback] mixin
- webhook:callbackUrl (STRING) multiple COPY
[webhook:callback]
- webhook:callbackUrl (STRING) COPY
@@ -1,13 +1,18 @@
package org.fcrepo.webhooks;


import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

import java.io.IOException;

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.assertTrue;

@ContextConfiguration({"/spring-test/repo.xml", "/spring-test/rest.xml",
"/spring-test/eventing.xml"})
Expand All @@ -16,7 +21,21 @@ public class FedoraWebhooksTest extends AbstractResourceTest {
@Test
public void registerWebhookCallbackTest() throws IOException {
assertEquals(201, getStatus(new HttpPost(serverAddress +
"/webhooks/callback_id?callbackUrl=info:fedora")));
"/webhooks/callback_id?callbackUrl=info:fedora/fake:url")));


final HttpGet displayWebhooks =
new HttpGet(serverAddress + "/webhooks");

String content = EntityUtils.toString(client.execute(
displayWebhooks).getEntity());

logger.info("Got content: ");
logger.info(content);
assertTrue("Our webhook wasn't registered!", compile(
"info:fedora/fake:url", DOTALL).matcher(content)
.find());


}
}

0 comments on commit 66aaaeb

Please sign in to comment.