Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Changed to use the fact that non-mutating methods do not need to crea…
…te new sessions
  • Loading branch information
ajs6f committed Feb 12, 2013
1 parent a4328c5 commit 54a087b
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 161 deletions.
Expand Up @@ -15,7 +15,6 @@

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.Property;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
Expand All @@ -35,6 +34,8 @@ public class DatastreamService {

private JcrTools jcrTools = new JcrTools();

private Session readOnlySession;

public Node createDatastreamNode(final Session session,
final String dsPath, final String contentType,
final InputStream requestBodyStream) throws RepositoryException,
Expand All @@ -52,13 +53,13 @@ public Node createDatastreamNode(final Session session,
* Property dataProperty = contentNode.setProperty(JCR_DATA,
* requestBodyStream);
* then the JCR would not block on the stream's completion, and we would
* return to the requestor before the mutation to the repo had actually
* return to the requester before the mutation to the repo had actually
* completed. So instead we use createBinary(requestBodyStream), because
* its contract specifies:
* "The passed InputStream is closed before this method returns either
* normally or because of an exception."
* which lets us block and not return until the job is done! The simpler
* code may still be useful to us for an asychronous method that we
* code may still be useful to us for an asynchronous method that we
* develop later.
*/
Property dataProperty =
Expand All @@ -83,8 +84,9 @@ public Node createDatastreamNode(final Session session,
return ds;
}

public InputStream getDatastreamContentInputStream(final Session session, final String dsPath) throws RepositoryException {
return session.getNode(dsPath).getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getStream();
public InputStream getDatastreamContentInputStream(final Session session,
final String dsPath) throws RepositoryException {
return session.getNode(dsPath).getNode(JCR_CONTENT).getProperty(
JCR_DATA).getBinary().getStream();
}
}
Expand Up @@ -7,7 +7,6 @@
<!-- Master context for the application. -->

<import resource="classpath:/META-INF/spring/repo.xml"/>
<import resource="classpath:/META-INF/spring/rest.xml"/>
<import resource="classpath:/META-INF/spring/eventing.xml"/>

</beans>
1 change: 0 additions & 1 deletion fcrepo-kernel/src/test/resources/spring-test/master.xml
Expand Up @@ -7,7 +7,6 @@
<!-- Master context for the test application. -->

<import resource="classpath:/spring-test/repo.xml"/>
<import resource="classpath:/spring-test/rest.xml"/>
<import resource="classpath:/spring-test/eventing.xml"/>

</beans>
Expand Up @@ -18,6 +18,9 @@
import java.io.InputStream;
import java.net.URI;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.jcr.LoginException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
Expand Down Expand Up @@ -53,6 +56,19 @@ public class FedoraDatastreams extends AbstractResource {
final private Logger logger = LoggerFactory
.getLogger(FedoraDatastreams.class);

private Session readOnlySession;

@PostConstruct
public void loginReadOnlySession() throws LoginException,
RepositoryException {
readOnlySession = repo.login();
}

@PreDestroy
public void logoutReadOnlySession() {
readOnlySession.logout();
}

/**
* Returns a list of datastreams for the object
*
Expand All @@ -70,23 +86,20 @@ public class FedoraDatastreams extends AbstractResource {
public Response getDatastreams(@PathParam("pid")
final String pid) throws RepositoryException, IOException {

final Session session = repo.login();

if (session.nodeExists("/objects/" + pid)) {
if (readOnlySession.nodeExists("/objects/" + pid)) {
final ObjectDatastreams objectDatastreams = new ObjectDatastreams();
final Builder<Datastream> datastreams = builder();

NodeIterator i = session.getNode("/objects/" + pid).getNodes();
NodeIterator i =
readOnlySession.getNode("/objects/" + pid).getNodes();
while (i.hasNext()) {
final Node ds = i.nextNode();
datastreams.add(new Datastream(ds.getName(), ds.getName(),
getDSMimeType(ds)));
}
objectDatastreams.datastreams = datastreams.build();
session.logout();
return ok(objectDatastreams).build();
} else {
session.logout();
return four04;
}
}
Expand Down Expand Up @@ -238,21 +251,17 @@ public Response getDatastream(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException, IOException {

Session session = repo.login();

if (!session.nodeExists("/objects/" + pid)) {
if (!readOnlySession.nodeExists("/objects/" + pid)) {
return four04;
}

final Node obj = session.getNode("/objects/" + pid);
final Node obj = readOnlySession.getNode("/objects/" + pid);

if (obj.hasNode(dsid)) {
final Node ds = obj.getNode(dsid);
final DatastreamProfile dsProfile = getDSProfile(ds);
session.logout();
return ok(dsProfile).build();
} else {
session.logout();
return four04;
}
}
Expand All @@ -273,22 +282,19 @@ public Response getDatastreamContent(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException {

final Session session = repo.login();
final String dsPath = "/objects/" + pid + "/" + dsid;

if (session.nodeExists(dsPath)) {
final Node ds = session.getNode(dsPath);
if (readOnlySession.nodeExists(dsPath)) {
final Node ds = readOnlySession.getNode(dsPath);
final String mimeType =
ds.hasProperty("fedora:contentType") ? ds.getProperty(
"fedora:contentType").getString()
: "application/octet-stream";
final InputStream responseStream =
ds.getNode(JCR_CONTENT).getProperty(JCR_DATA).getBinary()
.getStream();
session.logout();
return ok(responseStream, mimeType).build();
} else {
session.logout();
return four04;
}
}
Expand All @@ -314,19 +320,16 @@ Response getDatastreamHistory(@PathParam("pid")
final String pid, @PathParam("dsid")
final String dsid) throws RepositoryException, IOException {

final Session session = repo.login();
final String dsPath = "/objects/" + pid + "/" + dsid;

if (session.nodeExists(dsPath)) {
final Node ds = session.getNode(dsPath);
if (readOnlySession.nodeExists(dsPath)) {
final Node ds = readOnlySession.getNode(dsPath);
final DatastreamHistory dsHistory =
new DatastreamHistory(singletonList(getDSProfile(ds)));
dsHistory.dsID = dsid;
dsHistory.pid = pid;
session.logout();
return ok(dsHistory).build();
} else {
session.logout();
return four04;
}
}
Expand Down

0 comments on commit 54a087b

Please sign in to comment.