Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #79 from futures/NewInjectedSessions
Field injection of JCR Sessions into JAX-RS endpoints, except FedoraTransactions
  • Loading branch information
cbeer committed Jun 12, 2013
2 parents a35afee + a1f4ed7 commit d684999
Show file tree
Hide file tree
Showing 58 changed files with 1,204 additions and 662 deletions.
2 changes: 1 addition & 1 deletion fcrepo-generator-dc/src/test/resources/web.xml
Expand Up @@ -38,7 +38,7 @@
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.fcrepo.api, org.fcrepo.generator, org.fcrepo.exceptionhandlers</param-value>
<param-value>org.fcrepo.api, org.fcrepo.generator, org.fcrepo.exceptionhandlers, org.fcrepo.session</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
Expand Down
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;
}

}
31 changes: 21 additions & 10 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraDatastreams.java
Expand Up @@ -38,8 +38,10 @@
import org.fcrepo.AbstractResource;
import org.fcrepo.Datastream;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.session.InjectedSession;
import org.fcrepo.utils.ContentDigest;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.codahale.metrics.annotation.Timed;
Expand All @@ -49,9 +51,13 @@
import com.sun.jersey.multipart.MultiPart;

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

@InjectedSession
protected Session session;

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

/**
Expand All @@ -74,7 +80,6 @@ public Response modifyDatastreams(@PathParam("path")
final List<String> dsidList, final MultiPart multipart)
throws RepositoryException, IOException, InvalidChecksumException {

final Session session = getAuthenticatedSession();
final String path = toPath(pathList);
try {
for (final String dsid : dsidList) {
Expand Down Expand Up @@ -102,7 +107,9 @@ public Response modifyDatastreams(@PathParam("path")
}

session.save();
return created(uriInfo.getAbsolutePathBuilder().path(FedoraNodes.class).build(path.substring(1))).build();
return created(
uriInfo.getAbsolutePathBuilder().path(FedoraNodes.class)
.build(path.substring(1))).build();
} finally {
session.logout();
}
Expand All @@ -121,7 +128,6 @@ public Response modifyDatastreams(@PathParam("path")
public Response deleteDatastreams(@PathParam("path")
final List<PathSegment> pathList, @QueryParam("dsid")
final List<String> dsidList) throws RepositoryException {
final Session session = getAuthenticatedSession();
try {
final String path = toPath(pathList);
for (final String dsid : dsidList) {
Expand Down Expand Up @@ -156,8 +162,6 @@ public Response getDatastreamsContents(@PathParam("path")
final Request request) throws RepositoryException, IOException,
NoSuchAlgorithmException {

final Session session = getAuthenticatedSession();

final ArrayList<Datastream> datastreams = new ArrayList<Datastream>();

try {
Expand Down Expand Up @@ -218,11 +222,14 @@ public Response getDatastreamsContents(@PathParam("path")
final MultiPart multipart = new MultiPart();

for (final Datastream ds : datastreams) {
final BodyPart bodyPart = new BodyPart(ds.getContent(), MediaType.valueOf(ds.getMimeType()));
bodyPart.setContentDisposition(ContentDisposition.type("attachment").fileName(ds.getPath())
.creationDate(ds.getCreatedDate())
.modificationDate(ds.getLastModifiedDate())
.size(ds.getContentSize()).build());
final BodyPart bodyPart =
new BodyPart(ds.getContent(), MediaType.valueOf(ds
.getMimeType()));
bodyPart.setContentDisposition(ContentDisposition.type(
"attachment").fileName(ds.getPath()).creationDate(
ds.getCreatedDate()).modificationDate(
ds.getLastModifiedDate()).size(ds.getContentSize())
.build());
multipart.bodyPart(bodyPart);
}

Expand All @@ -236,4 +243,8 @@ public Response getDatastreamsContents(@PathParam("path")
session.logout();
}
}

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

0 comments on commit d684999

Please sign in to comment.