Last active
August 29, 2015 14:16
-
-
Save cudevmaxwell/254e4d387a15934d97e5 to your computer and use it in GitHub Desktop.
Camel Processor and Router for "Striking Out" blogpost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ca.islandora.sync.routes; | |
import org.apache.camel.Exchange; | |
import org.apache.camel.builder.RouteBuilder; | |
import org.fcrepo.camel.JmsHeaders; | |
import org.fcrepo.camel.RdfNamespaces; | |
import ca.islandora.sync.processors.DrupalNodeCreateJsonTransform; | |
public class DrupalNodeCreate extends RouteBuilder { | |
public void configure() throws Exception { | |
from("activemq:topic:fedora") | |
.routeId("fedoraInAdded") | |
.filter(header(JmsHeaders.EVENT_TYPE).contains(RdfNamespaces.REPOSITORY + "NODE_ADDED")) | |
.to("fcrepo:localhost:8080/fcrepo/rest") | |
.process(new DrupalNodeCreateJsonTransform()) | |
.setHeader(Exchange.HTTP_URI, simple("http://localhost/islandora/node/${header.uuid}")) | |
.to("http4:localhost/unusued") | |
.log("RESPONSE: ${headers} / ${body}") | |
.to("mock:result"); | |
from("activemq:topic:fedora") | |
.routeId("fedoraInChanged") | |
.filter(header(JmsHeaders.EVENT_TYPE).contains(RdfNamespaces.REPOSITORY + "PROPERTY_CHANGED")) | |
.to("fcrepo:localhost:8080/fcrepo/rest") | |
.process(new DrupalNodeCreateJsonTransform()) | |
.setHeader(Exchange.HTTP_URI, simple("http://localhost/islandora/node/${header.uuid}")) | |
.to("http4:localhost/unusued") | |
.log("RESPONSE: ${headers} / ${body}") | |
.to("mock:result"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ca.islandora.sync.processors; | |
import static org.apache.camel.component.http4.HttpMethods.PUT; | |
import org.apache.camel.Exchange; | |
import org.apache.camel.Message; | |
import org.apache.camel.Processor; | |
import org.json.simple.JSONObject; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.DocumentBuilder; | |
import org.w3c.dom.Document; | |
import java.io.InputStream; | |
public class DrupalNodeCreateJsonTransform implements Processor { | |
@SuppressWarnings("unchecked") | |
@Override | |
public void process(Exchange exchange) throws Exception { | |
/* | |
* Make some json that looks like this: | |
* { | |
* "title":"My Fedora Title", | |
* "type":"article", | |
* "uuid":"uuid", | |
* } | |
*/ | |
JSONObject outBody = new JSONObject(); | |
outBody.put("type", "article"); | |
InputStream inBody = exchange.getIn().getBody(InputStream.class); | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
factory.setNamespaceAware(true); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
Document inDocument = builder.parse(inBody); | |
//Get the UUID. Every Fedora Resource should have at least one. | |
String uuid = inDocument.getElementsByTagNameNS("http://fedora.info/definitions/v4/repository#", "uuid").item(0).getTextContent(); | |
//Get the dc:title. If not set, use the UUID. | |
String title = uuid; | |
if (inDocument.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "title").getLength() > 0) { | |
title = inDocument.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/", "title").item(0).getTextContent(); | |
} | |
outBody.put("title", title); | |
outBody.put("uuid", uuid); | |
/* | |
* Set up the out message to be a PUT for the | |
* subsequent call to Drupal's REST service. | |
*/ | |
Message outMessage = exchange.getOut(); | |
outMessage.setHeader(Exchange.HTTP_METHOD, PUT); | |
outMessage.setHeader("uuid", uuid); | |
outMessage.setHeader(Exchange.CONTENT_TYPE, "application/json"); | |
outMessage.setBody(outBody.toJSONString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment