Skip to content

Commit

Permalink
Field injection for JCR Sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f committed Jun 12, 2013
1 parent 2514d1b commit ee0cd2b
Show file tree
Hide file tree
Showing 28 changed files with 381 additions and 188 deletions.
6 changes: 6 additions & 0 deletions fcrepo-http-api/pom.xml
Expand Up @@ -90,6 +90,12 @@
<artifactId>jersey-grizzly2-servlet</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-servlet-api</artifactId>
<version>7.0.21</version>
<scope>provided</scope>
</dependency> -->

<dependency>
<groupId>javax.mail</groupId>
Expand Down
128 changes: 72 additions & 56 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraContent.java
Expand Up @@ -34,15 +34,21 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.Datastream;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.session.InjectedSession;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;

@Component
@Scope("prototype")
@Path("/{path: .*}/fcr:content")
public class FedoraContent extends AbstractResource {

@InjectedSession
protected Session session;

private final Logger logger = getLogger(FedoraContent.class);

/**
Expand All @@ -53,12 +59,11 @@ public class FedoraContent extends AbstractResource {
*/
@POST
@Timed
public Response create(
@PathParam("path") final List<PathSegment> pathList,
@QueryParam("checksumType") final String checksumType,
@QueryParam("checksum") final String checksum,
@HeaderParam("Content-Type") final MediaType requestContentType,
final InputStream requestBodyStream)
public Response create(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("checksumType")
final String checksumType, @QueryParam("checksum")
final String checksum, @HeaderParam("Content-Type")
final MediaType requestContentType, final InputStream requestBodyStream)
throws IOException, InvalidChecksumException, RepositoryException {
final MediaType contentType =
requestContentType != null ? requestContentType
Expand All @@ -72,22 +77,22 @@ public Response create(
}

logger.debug("create Datastream {}", path);
final Session session = getAuthenticatedSession();
try {
datastreamService.createDatastreamNode(session, path, contentType
.toString(), requestBodyStream, checksumType, checksum);
} finally {
session.save();
session.logout();
session.logout();
}
return created(uriInfo.getBaseUriBuilder().path(FedoraContent.class).build(path.substring(1))).build();
return created(
uriInfo.getBaseUriBuilder().path(FedoraContent.class).build(
path.substring(1))).build();
}


/**
* Modify an existing datastream's content
*
* @param pathList
* @param pathList
* @param requestContentType
* Content-Type header
* @param requestBodyStream
Expand All @@ -99,41 +104,47 @@ public Response create(
*/
@PUT
@Timed
public Response modifyContent(
@PathParam("path") List<PathSegment> pathList,
@HeaderParam("Content-Type") final MediaType requestContentType,
final InputStream requestBodyStream, @Context final Request request)
throws RepositoryException, IOException, InvalidChecksumException {
final Session session = getAuthenticatedSession();
public Response modifyContent(@PathParam("path")
final List<PathSegment> pathList, @HeaderParam("Content-Type")
final MediaType requestContentType, final InputStream requestBodyStream,
@Context
final Request request) throws RepositoryException, IOException,
InvalidChecksumException {
try {
String path = toPath(pathList);
final String path = toPath(pathList);
final MediaType contentType =
requestContentType != null ? requestContentType
: APPLICATION_OCTET_STREAM_TYPE;

if (nodeService.exists(session, path)) {
if (nodeService.exists(session, path)) {

final Datastream ds = datastreamService.getDatastream(session, path);
final Datastream ds =
datastreamService.getDatastream(session, path);

final EntityTag etag = new EntityTag(ds.getContentDigest().toString());
final EntityTag etag =
new EntityTag(ds.getContentDigest().toString());
final Date date = ds.getLastModifiedDate();
final Date roundedDate = new Date();
roundedDate.setTime(date.getTime() - date.getTime() % 1000);
ResponseBuilder builder = request.evaluatePreconditions(roundedDate, etag);
final ResponseBuilder builder =
request.evaluatePreconditions(roundedDate, etag);

if (builder != null) {
throw new WebApplicationException(builder.build());
}
}

logger.debug("create Datastream {}", path);
final Node datastreamNode = datastreamService.createDatastreamNode(session, path, contentType
.toString(), requestBodyStream);
final Node datastreamNode =
datastreamService.createDatastreamNode(session, path,
contentType.toString(), requestBodyStream);
final boolean isNew = datastreamNode.isNew();
session.save();

if (isNew) {
return created(uriInfo.getBaseUriBuilder().path(FedoraContent.class).build(path.substring(1))).build();
return created(
uriInfo.getBaseUriBuilder().path(FedoraContent.class)
.build(path.substring(1))).build();
} else {
return noContent().build();
}
Expand All @@ -146,41 +157,46 @@ public Response modifyContent(
/**
* Get the binary content of a datastream
*
* @param pathList
* @param pathList
* @return Binary blob
* @throws RepositoryException
*/
@GET
@Timed
public Response getContent(
@PathParam("path") List<PathSegment> pathList,
@Context final Request request
) throws RepositoryException {

final Session session = getAuthenticatedSession();
try {
String path = toPath(pathList);
final Datastream ds = datastreamService.getDatastream(session, path);

final EntityTag etag = new EntityTag(ds.getContentDigest().toString());
final Date date = ds.getLastModifiedDate();
final Date roundedDate = new Date();
roundedDate.setTime(date.getTime() - date.getTime() % 1000);
ResponseBuilder builder =
request.evaluatePreconditions(roundedDate, etag);

final CacheControl cc = new CacheControl();
cc.setMaxAge(0);
cc.setMustRevalidate(true);

if (builder == null) {
builder = Response.ok(ds.getContent(), ds.getMimeType());
}

return builder.cacheControl(cc).lastModified(date).tag(etag).build();
} finally {
session.logout();
}
}
public Response getContent(@PathParam("path")
final List<PathSegment> pathList, @Context
final Request request) throws RepositoryException {

try {
final String path = toPath(pathList);
final Datastream ds =
datastreamService.getDatastream(session, path);

final EntityTag etag =
new EntityTag(ds.getContentDigest().toString());
final Date date = ds.getLastModifiedDate();
final Date roundedDate = new Date();
roundedDate.setTime(date.getTime() - date.getTime() % 1000);
ResponseBuilder builder =
request.evaluatePreconditions(roundedDate, etag);

final CacheControl cc = new CacheControl();
cc.setMaxAge(0);
cc.setMustRevalidate(true);

if (builder == null) {
builder = Response.ok(ds.getContent(), ds.getMimeType());
}

return builder.cacheControl(cc).lastModified(date).tag(etag)
.build();
} finally {
session.logout();
}
}

public void setSession(final Session session) {
this.session = session;
}

}
Expand Up @@ -22,13 +22,13 @@
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import com.hp.hpl.jena.query.Dataset;
import org.fcrepo.AbstractResource;
import org.fcrepo.Datastream;
import org.fcrepo.api.rdf.HttpGraphSubjects;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.query.Dataset;

@Component
@Path("/{path: .*}/fcr:fixity")
Expand Down

0 comments on commit ee0cd2b

Please sign in to comment.