Skip to content

Commit

Permalink
Made all methods in [Object|Datastream]Service static to prevent need…
Browse files Browse the repository at this point in the history
… to use actual [Object|Datastream]Service instances
  • Loading branch information
ajs6f committed Feb 21, 2013
1 parent 158f169 commit 2e7fe72
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 51 deletions.
5 changes: 0 additions & 5 deletions fcrepo-dc/src/main/java/org/fcrepo/generator/DublinCore.java
Expand Up @@ -9,7 +9,6 @@
import java.util.List;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
Expand All @@ -21,17 +20,13 @@

import org.fcrepo.AbstractResource;
import org.fcrepo.generator.dublincore.AbstractIndexer;
import org.fcrepo.services.ObjectService;

@Path("/objects/{pid}/oai_dc")
public class DublinCore extends AbstractResource {

@Resource
List<AbstractIndexer> indexers;

@Inject
ObjectService objectService;

@GET
@Produces(TEXT_XML)
public Response getObjectAsDublinCore(@PathParam("pid")
Expand Down
Expand Up @@ -29,7 +29,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DatastreamService extends JcrTools {
public class DatastreamService {

private static final Logger logger = LoggerFactory
.getLogger(DatastreamService.class);
Expand All @@ -39,16 +39,18 @@ public class DatastreamService extends JcrTools {

private Session readOnlySession;

public Node createDatastreamNode(final Session session,
private static JcrTools jcrTools = new JcrTools(false);

public static Node createDatastreamNode(final Session session,
final String dsPath, final String contentType,
final InputStream requestBodyStream) throws RepositoryException,
IOException {

final Node ds = findOrCreateNode(session, dsPath, NT_FILE);
final Node ds = jcrTools.findOrCreateNode(session, dsPath, NT_FILE);
ds.addMixin(FEDORA_DATASTREAM);

final Node contentNode =
findOrCreateChild(ds, JCR_CONTENT, NT_RESOURCE);
jcrTools.findOrCreateChild(ds, JCR_CONTENT, NT_RESOURCE);
logger.debug("Created content node at path: " + contentNode.getPath());
/*
* This next line of code deserves explanation. If we chose for the
Expand Down Expand Up @@ -97,29 +99,6 @@ public static Datastream getDatastream(final String pid, final String dsId)
return new Datastream(getObjectNode(pid).getNode(dsId));
}

public static 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 String dsPath)
throws RepositoryException {
return readOnlySession.getNode(dsPath).getNode(JCR_CONTENT)
.getProperty(JCR_DATA).getBinary().getStream();
}

public static InputStream getDatastreamContentInputStream(final Node node)
throws RepositoryException {
return new Datastream(node).getContent();
}

public static InputStream getDatastreamContentInputStream(
final Datastream ds) throws RepositoryException {
return ds.getContent();
}

@PostConstruct
public void getSession() {
try {
Expand Down
Expand Up @@ -30,6 +30,11 @@ public class ObjectService {
return new FedoraObject(session, path).getNode();
}

public static Node createObjectNodeByName(final Session session,
final String name) throws RepositoryException {
return new FedoraObject(session, "/objects/" + name).getNode();
}

public static Node getObjectNode(final String name)
throws PathNotFoundException, RepositoryException {
return readOnlySession.getNode("/objects/" + name);
Expand Down
@@ -1,7 +1,9 @@

package org.fcrepo.services;

import static org.fcrepo.services.DatastreamService.getDatastreamContentInputStream;
import static org.fcrepo.services.DatastreamService.createDatastreamNode;
import static org.fcrepo.services.DatastreamService.getDatastream;
import static org.fcrepo.services.ObjectService.createObjectNodeByName;
import static org.jgroups.util.Util.assertEquals;
import static org.jgroups.util.Util.assertTrue;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
Expand All @@ -16,6 +18,7 @@

import org.apache.tika.io.IOUtils;
import org.fcrepo.AbstractTest;
import org.fcrepo.Datastream;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;

Expand All @@ -25,15 +28,12 @@ public class DatastreamServiceTest extends AbstractTest {
@Inject
private Repository repository;

@Inject
private DatastreamService datastreamService;

@Test
public void testCreateDatastreamNode() throws Exception {
Session session = repository.login();
InputStream is = new ByteArrayInputStream("asdf".getBytes());

datastreamService.createDatastreamNode(session, "testDatastreamNode",
createDatastreamNode(session, "testDatastreamNode",
"application/octet-stream", is);
session.save();
session.logout();
Expand All @@ -49,18 +49,15 @@ public void testCreateDatastreamNode() throws Exception {
public void testGetDatastreamContentInputStream() throws Exception {
Session session = repository.login();
InputStream is = new ByteArrayInputStream("asdf".getBytes());

datastreamService.createDatastreamNode(session, "testDatastreamNode",
createObjectNodeByName(session, "testObject");
createDatastreamNode(session, "/objects/testObject/testDatastreamNode",
"application/octet-stream", is);

session.save();
session.logout();
session = repository.login();

InputStream contentInputStream =
getDatastreamContentInputStream(session, "/testDatastreamNode");

assertEquals("asdf", IOUtils.toString(contentInputStream, "UTF-8"));
final Datastream ds = getDatastream("testObject", "testDatastreamNode");
assertEquals("asdf", IOUtils.toString(ds.getContent(), "UTF-8"));
session.logout();
}
}
Expand Up @@ -10,6 +10,7 @@
import static javax.ws.rs.core.Response.ok;
import static org.fcrepo.api.legacy.FedoraObjects.getObjectSize;
import static org.fcrepo.jaxb.responses.DatastreamProfile.DatastreamStates.A;
import static org.fcrepo.services.DatastreamService.createDatastreamNode;
import static org.fcrepo.services.DatastreamService.getDatastreamNode;
import static org.fcrepo.services.ObjectService.getObjectNode;
import static org.modeshape.jcr.api.JcrConstants.JCR_CONTENT;
Expand Down Expand Up @@ -103,9 +104,8 @@ public Response addDatastreams(@PathParam("pid")
final String dsid =
a.getContentDisposition().getParameter("name");
final String dsPath = "/objects/" + pid + "/" + dsid;
new DatastreamService().createDatastreamNode(session, dsPath, a
.getDataHandler().getContentType(), a.getDataHandler()
.getInputStream());
createDatastreamNode(session, dsPath, a.getDataHandler()
.getContentType(), a.getDataHandler().getInputStream());

}
session.save();
Expand Down Expand Up @@ -212,10 +212,8 @@ private URI addDatastreamNode(final String pid, final String dsPath,
logger.debug("Attempting to add datastream node at path: " + dsPath);
try {
boolean created = session.nodeExists(dsPath);

new DatastreamService().createDatastreamNode(session, dsPath,
contentType.toString(), requestBodyStream);

createDatastreamNode(session, dsPath, contentType.toString(),
requestBodyStream);
session.save();
if (created) {
/*
Expand Down

0 comments on commit 2e7fe72

Please sign in to comment.