Skip to content

Commit

Permalink
Getting baseURL for JMS messages from event user data field, which is…
Browse files Browse the repository at this point in the history
… set using by HTTP API from UriInfo baseURL
  • Loading branch information
escowles committed Jul 17, 2014
1 parent e12bc00 commit 629cf3b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
28 changes: 28 additions & 0 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -69,6 +69,7 @@
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.observation.ObservationManager;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
Expand Down Expand Up @@ -136,6 +137,26 @@ public class FedoraNodes extends AbstractResource {
protected Session session;

private static final Logger LOGGER = getLogger(FedoraNodes.class);
private boolean baseURLSet = false;

/**
* Set the baseURL for JMS events.
**/
private void init( final UriInfo uriInfo ) {
if ( !baseURLSet ) {
baseURLSet = true;
try {
final URI baseURL = uriInfo.getBaseUri();
LOGGER.warn("FedoraNodes.init(): baseURL = " + baseURL.toString());
final ObservationManager obs = session.getWorkspace().getObservationManager();
final String json = "{\"baseURL\":\"" + baseURL.toString() + "\"}";
obs.setUserData(json);
LOGGER.warn("FedoraNodes.init(): done");
} catch ( Exception ex ) {
LOGGER.warn("Error setting baseURL", ex);
}
}
}

/**
* Retrieve the node headers
Expand Down Expand Up @@ -353,6 +374,7 @@ public Response updateSparql(@PathParam("path")
throws RepositoryException, IOException {
throwIfPathIncludesJcr(pathList, "PATCH");

init(uriInfo);
final String path = toPath(pathList);
LOGGER.debug("Attempting to update path: {}", path);

Expand Down Expand Up @@ -424,6 +446,7 @@ public Response createOrReplaceObjectRdf(
@Context final HttpServletResponse servletResponse) throws RepositoryException, ParseException,
IOException, InvalidChecksumException, URISyntaxException {
throwIfPathIncludesJcr(pathList, "PUT");
init(uriInfo);

final String path = toPath(pathList);
LOGGER.debug("Attempting to replace path: {}", path);
Expand Down Expand Up @@ -509,6 +532,7 @@ public Response createObject(@PathParam("path")
throws RepositoryException, ParseException, IOException,
InvalidChecksumException, URISyntaxException {
throwIfPathIncludesJcr(pathList, "POST");
init(uriInfo);

String pid;
final String newObjectPath;
Expand Down Expand Up @@ -688,6 +712,7 @@ public Response createObjectFromFormPost(
@FormDataParam("file") final InputStream file
) throws RepositoryException, URISyntaxException, InvalidChecksumException, ParseException, IOException {
throwIfPathIncludesJcr(pathList, "POST with multipart attachment");
init(uriInfo);

final MediaType effectiveContentType = file == null ? null : MediaType.APPLICATION_OCTET_STREAM_TYPE;
return createObject(pathList, mixin, null, null, effectiveContentType, slug, servletResponse, uriInfo, file);
Expand All @@ -707,6 +732,7 @@ public Response deleteObject(@PathParam("path")
final List<PathSegment> pathList,
@Context final Request request) throws RepositoryException {
throwIfPathIncludesJcr(pathList, "DELETE");
init(uriInfo);

try {

Expand Down Expand Up @@ -735,6 +761,7 @@ public Response copyObject(@PathParam("path") final List<PathSegment> path,
@HeaderParam("Destination") final String destinationUri)
throws RepositoryException, URISyntaxException {
throwIfPathIncludesJcr(path, "COPY");
init(uriInfo);

try {

Expand Down Expand Up @@ -782,6 +809,7 @@ public Response moveObject(@PathParam("path") final List<PathSegment> pathList,
@Context final Request request)
throws RepositoryException, URISyntaxException {
throwIfPathIncludesJcr(pathList, "MOVE");
init(uriInfo);

try {

Expand Down
7 changes: 6 additions & 1 deletion fcrepo-jms/pom.xml
Expand Up @@ -81,6 +81,11 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<!-- axiom-api is a dependency of abdera-parser. It is only explicitly declared
here because abdera-parser depends on an older version of jaxen (1.1.1) than axiom-api.
If/when abdera-parser catches up, we can remove this explicit dependency on axiom-api -->
Expand Down Expand Up @@ -125,4 +130,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Expand Up @@ -33,6 +33,8 @@
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
* Generates JMS {@link Message}s composed entirely of headers, based entirely
Expand Down Expand Up @@ -61,17 +63,27 @@ public class DefaultMessageFactory implements JMSEventMessageFactory {
private String baseURL;

/**
* @param baseURL indicating the repository server host/port/etc
* Get baseURL.
*/
public DefaultMessageFactory(final String baseURL) {
this.baseURL = baseURL;
log.debug("MessageFactory baseURL: {}", baseURL);
public void setBaseURL(final FedoraEvent event) {
try {
final JsonObject json = new JsonParser().parse(event.getUserData()).getAsJsonObject();
this.baseURL = json.get("baseURL").getAsString();
log.debug("MessageFactory baseURL: {}", baseURL);
} catch ( Exception ex ) {
log.warn("Error setting baseURL", ex);
}
}

@Override
public Message getMessage(final FedoraEvent jcrEvent,
final javax.jms.Session jmsSession) throws RepositoryException,
IOException, JMSException {

if ( baseURL == null ) {
setBaseURL(jcrEvent);
}

final Message message = jmsSession.createMessage();
message.setLongProperty(TIMESTAMP_HEADER_NAME, jcrEvent.getDate());
message.setStringProperty(IDENTIFIER_HEADER_NAME, jcrEvent.getPath());
Expand Down
Expand Up @@ -27,8 +27,6 @@
import static org.fcrepo.jms.headers.DefaultMessageFactory.PROPERTIES_HEADER_NAME;
import static org.fcrepo.jms.headers.DefaultMessageFactory.TIMESTAMP_HEADER_NAME;
import static org.fcrepo.kernel.RdfLexicon.REPOSITORY_NAMESPACE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.slf4j.LoggerFactory.getLogger;

Expand Down Expand Up @@ -117,10 +115,6 @@ public void testIngestion() throws RepositoryException,
success = true;
}
}

final String baseUrl = messages.iterator().next().getStringProperty(BASE_URL_HEADER_NAME);
assertNotNull("BaseUrl should not be null!", baseUrl);
assertEquals("Defined in spring-test/headers-jms.xml", "http://localhost:8080/rest", baseUrl);
}
LOGGER.debug("Waiting for next message...");
wait(1000);
Expand Down
Expand Up @@ -62,14 +62,15 @@ public void setUp() throws JMSException {
initMocks(this);
when(mockSession.createMessage()).thenReturn(
new ActiveMQObjectMessage());
testDefaultMessageFactory = new DefaultMessageFactory("base-url");
testDefaultMessageFactory = new DefaultMessageFactory();
}

@Test
public void testBuildMessage() throws RepositoryException, IOException,
JMSException {
final Long testDate = 46647758568747L;
when(mockEvent.getDate()).thenReturn(testDate);
when(mockEvent.getUserData()).thenReturn("{\"baseURL\":\"base-url\"}");
final String testPath = "super/calli/fragi/listic";
when(mockEvent.getPath()).thenReturn(testPath);
final Set<Integer> testTypes = singleton(NODE_ADDED);
Expand Down
4 changes: 1 addition & 3 deletions fcrepo-jms/src/test/resources/spring-test/headers-jms.xml
Expand Up @@ -19,8 +19,6 @@
<amq:connectionFactory id="connectionFactory"
brokerURL="vm://localhost?broker.persistent=false&amp;broker.useJmx=false&amp;broker.enableStatistics=false"/>

<bean class="org.fcrepo.jms.headers.DefaultMessageFactory">
<constructor-arg value="http://localhost:8080/rest"/>
</bean>
<bean class="org.fcrepo.jms.headers.DefaultMessageFactory"/>

</beans>
4 changes: 1 addition & 3 deletions fcrepo-webapp/src/main/resources/spring/jms.xml
Expand Up @@ -19,8 +19,6 @@
p:config="classpath:/config/activemq.xml" p:start="true"/>

<!-- translates events into JMS header-only format-->
<bean class="org.fcrepo.jms.headers.DefaultMessageFactory">
<constructor-arg value="${jms.base-url:http://localhost:8080/rest}"/>
</bean>
<bean class="org.fcrepo.jms.headers.DefaultMessageFactory"/>

</beans>

0 comments on commit 629cf3b

Please sign in to comment.