Skip to content

Commit

Permalink
Initialize JMS base URIs in all request-scoped resources that update …
Browse files Browse the repository at this point in the history
…objects
  • Loading branch information
cbeer committed Oct 6, 2014
1 parent 7fda27e commit 255f7ec
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 23 deletions.
Expand Up @@ -24,18 +24,26 @@
import org.fcrepo.kernel.impl.DatastreamImpl;
import org.fcrepo.kernel.impl.FedoraBinaryImpl;
import org.fcrepo.kernel.impl.FedoraObjectImpl;
import org.slf4j.Logger;

import javax.jcr.Node;
import javax.jcr.Session;
import javax.jcr.observation.ObservationManager;
import javax.ws.rs.core.UriInfo;

import java.net.URI;

import static org.fcrepo.jcr.FedoraJcrTypes.FCR_METADATA;
import static org.slf4j.LoggerFactory.getLogger;

/**
* @author cabeer
* @since 10/5/14
*/
abstract public class FedoraBaseResource extends AbstractResource {

private static final Logger LOGGER = getLogger(FedoraBaseResource.class);

protected IdentifierConverter<Resource,Node> identifierTranslator;

protected abstract Session session();
Expand Down Expand Up @@ -78,4 +86,30 @@ public FedoraResource getResourceFromPath(final String externalPath) {
return resource;
}



private static boolean baseURLSet = false;
/**
* Set the baseURL for JMS events.
**/
protected void setUpJMSBaseURIs(final UriInfo uriInfo) {
if ( !baseURLSet ) {
// set to true the first time this is run. if there is an exception the first time, there
// will likely be an exception every time. since this is run on each repository update,
// we should fail fast rather than retrying over and over.
baseURLSet = true;
try {
final URI baseURL = uriInfo.getBaseUri();
final Class<? extends FedoraBaseResource> klass = this.getClass();
LOGGER.debug(klass + ".init(): baseURL = " + baseURL.toString());
final ObservationManager obs = session().getWorkspace().getObservationManager();
final String json = "{\"baseURL\":\"" + baseURL.toString() + "\"}";
obs.setUserData(json);
LOGGER.trace(klass + ".init(): done");
} catch ( Exception ex ) {
LOGGER.warn("Error setting baseURL", ex);
}
}
}

}
Expand Up @@ -39,6 +39,7 @@
import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
Expand Down Expand Up @@ -103,6 +104,13 @@ public class FedoraBatch extends ContentExposingResource {

@PathParam("path") protected String externalPath;

/**
* Run these actions after initializing this resource
*/
@PostConstruct
public void postConstruct() {
setUpJMSBaseURIs(uriInfo);
}

/**
* Default JAX-RS entry point
Expand Down
Expand Up @@ -39,6 +39,7 @@
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
Expand Down Expand Up @@ -140,6 +141,14 @@ public FedoraLdp(final String externalPath) {
this.externalPath = externalPath;
}

/**
* Run these actions after initializing this resource
*/
@PostConstruct
public void postConstruct() {
setUpJMSBaseURIs(uriInfo);
}

/**
* Retrieve the node headers
* @return response
Expand Down
30 changes: 7 additions & 23 deletions fcrepo-http-api/src/main/java/org/fcrepo/http/api/FedoraNodes.java
Expand Up @@ -24,12 +24,12 @@
import java.net.URI;
import java.net.URISyntaxException;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.jcr.ItemExistsException;
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.ClientErrorException;
import javax.ws.rs.HeaderParam;
Expand Down Expand Up @@ -65,7 +65,6 @@ public class FedoraNodes extends ContentExposingResource {
protected Session session;

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

@Context protected Request request;
@Context protected HttpServletResponse servletResponse;
Expand All @@ -91,26 +90,13 @@ public FedoraNodes(final String externalPath) {
this.externalPath = externalPath;
}


/**
* Set the baseURL for JMS events.
**/
private void init( final UriInfo uriInfo ) {
if ( !baseURLSet ) {
// set to true the first time this is run. if there is an exception the first time, there
// will likely be an exception every time. since this is run on each repository update,
// we should fail fast rather than retrying over and over.
baseURLSet = true;
try {
final URI baseURL = uriInfo.getBaseUri();
LOGGER.debug("FedoraNodes.init(): baseURL = " + baseURL.toString());
final ObservationManager obs = session.getWorkspace().getObservationManager();
final String json = "{\"baseURL\":\"" + baseURL.toString() + "\"}";
obs.setUserData(json);
LOGGER.trace("FedoraNodes.init(): done");
} catch ( Exception ex ) {
LOGGER.warn("Error setting baseURL", ex);
}
}
* Run these actions after initializing this resource
*/
@PostConstruct
public void postConstruct() {
setUpJMSBaseURIs(uriInfo);
}

/**
Expand All @@ -120,7 +106,6 @@ private void init( final UriInfo uriInfo ) {
@Timed
public Response copyObject(@HeaderParam("Destination") final String destinationUri)
throws URISyntaxException {
init(uriInfo);

try {

Expand Down Expand Up @@ -173,7 +158,6 @@ public Response copyObject(@HeaderParam("Destination") final String destinationU
@Timed
public Response moveObject(@HeaderParam("Destination") final String destinationUri)
throws URISyntaxException {
init(uriInfo);

try {

Expand Down

0 comments on commit 255f7ec

Please sign in to comment.