Skip to content

Commit

Permalink
javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed May 24, 2013
1 parent f1fd960 commit 045b1c5
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 59 deletions.
76 changes: 38 additions & 38 deletions fcrepo-http-commons/src/main/java/org/fcrepo/AbstractResource.java
Expand Up @@ -107,6 +107,32 @@ public void initialize() throws RepositoryException {
session.logout();
}

/**
* Convert a JAX-RS list of PathSegments to a JCR path
* @param paths
* @return
*/
public static final String toPath(final List<PathSegment> paths) {
final StringBuffer result = new StringBuffer();

for (final PathSegment path : paths) {
final String p = path.getPath();

if (!p.equals("")) {
result.append('/');
result.append(p);
}
}

final String path = result.toString();

if (path.isEmpty()) {
return "/";
} else {
return path;
}
}

protected Session getAuthenticatedSession() {
return sessions.getSession(securityContext, servletRequest);
}
Expand Down Expand Up @@ -157,23 +183,6 @@ protected FedoraResource createObjectOrDatastreamFromRequestContent(
return result;
}

protected synchronized Response deleteResource(final Node resource)
throws RepositoryException {

LOGGER.debug("Attempting to delete resource at path: " +
resource.getPath());
final Session session = resource.getSession();

try {
resource.remove();
session.save();
} finally {
session.logout();
}
return noContent().build();

}

/**
* A testing convenience setter for otherwise injected resources
* @param repo
Expand Down Expand Up @@ -214,35 +223,26 @@ public void setHttpServletRequest(final HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}

public static final String toPath(final List<PathSegment> paths) {
final StringBuffer result = new StringBuffer();

for (final PathSegment path : paths) {
final String p = path.getPath();

if (!p.equals("")) {
result.append('/');
result.append(p);
}
}

final String path = result.toString();

if (path.isEmpty()) {
return "/";
} else {
return path;
}
}

/**
* Set the NodeService, used primary for testing without spring
* @param nodeService
*/
public void setNodeService(final NodeService nodeService) {
this.nodeService = nodeService;
}

/**
* Set the ObjectService, used primary for testing without spring
* @param objectService
*/
public void setObjectService(final ObjectService objectService) {
this.objectService = objectService;
}

/**
* Set the DatastreamService, used primary for testing without spring
* @param datastreamService
*/
public void setDatastreamService(final DatastreamService datastreamService) {
this.datastreamService = datastreamService;
}
Expand Down
Expand Up @@ -107,15 +107,4 @@ public void testToPathEmpty() {
String actual = AbstractResource.toPath(pathList);
assertEquals(expected, actual);
}

@Test
public void testDeleteResource() throws RepositoryException {
Node mockNode = mock(Node.class);
Session mockSession = mock(Session.class);
when(mockNode.getSession()).thenReturn(mockSession);
testObj.deleteResource(mockNode);
verify(mockNode).remove();
verify(mockSession).save();
verify(mockSession).logout();
}
}
38 changes: 36 additions & 2 deletions fcrepo-kernel/src/main/java/org/fcrepo/services/NodeService.java
Expand Up @@ -23,15 +23,36 @@ public class NodeService extends RepositoryService implements FedoraJcrTypes {

private static final Logger logger = getLogger(NodeService.class);

/**
* Find or create a new Fedora resource at the given path
* @param session a JCR session
* @param path a JCR path
* @return
* @throws RepositoryException
*/
public FedoraResource findOrCreateObject(final Session session, final String path) throws RepositoryException {
return new FedoraResource(findOrCreateNode(session, path));
}

/**
* Retrieve an existing Fedora resource at the given path
* @param session a JCR session
* @param path a JCR path
* @return
* @throws RepositoryException
*/
public FedoraResource getObject(final Session session, final String path) throws RepositoryException {
return new FedoraResource(session.getNode(path));
}


/**
* Get an existing Fedora resource at the given path with the given version label
* @param session a JCR session
* @param path a JCR path
* @param versionId a JCR version label
* @return
* @throws RepositoryException
*/
public FedoraResource getObject(Session session, String path, String versionId) throws RepositoryException {
final VersionHistory versionHistory = FedoraTypesUtils.getVersionHistory(session, path);

Expand All @@ -55,6 +76,14 @@ public Set<String> getObjectNames(final Session session, String path) throws Rep
return getObjectNames(session, path, null);
}

/**
* Get the list of child nodes at the given path filtered by the given mixin
* @param session
* @param path
* @param mixin
* @return
* @throws RepositoryException
*/
public Set<String> getObjectNames(final Session session, String path, String mixin) throws RepositoryException {

final Node objects = session.getNode(path);
Expand All @@ -73,11 +102,16 @@ public Set<String> getObjectNames(final Session session, String path, String mix
return b.build();
}

/**
* Delete an existing object from the repository at the given path
* @param session
* @param path
* @throws RepositoryException
*/
public void deleteObject(final Session session, final String path)
throws RepositoryException {
final Node obj = session.getNode(path);
obj.remove();
session.save();
}

}
26 changes: 22 additions & 4 deletions fcrepo-kernel/src/main/java/org/fcrepo/utils/ContentDigest.java
Expand Up @@ -23,6 +23,12 @@ public abstract class ContentDigest {
public static final Map<String, String> schemeToAlgorithm = ImmutableMap
.of("urn:sha1", "SHA-1");

/**
* Convert a MessageDigest algorithm and checksum value to a URN
* @param algorithm
* @param value
* @return
*/
public static URI asURI(final String algorithm, final String value) {
try {
final String scheme = algorithmToScheme.get(algorithm);
Expand All @@ -35,15 +41,27 @@ public static URI asURI(final String algorithm, final String value) {
}
}

/**
* Convert a MessageDigest algorithm and checksum byte-array data to a URN
* @param algorithm
* @param data
* @return
*/
public static URI asURI(final String algorithm, final byte[] data) {
return asURI(algorithm, asString(data));
}

public static String asString(final byte[] data) {
return encodeHexString(data);
}

/**
* Given a digest URI, get the corresponding MessageDigest algorithm
* @param digestUri
* @return
*/
public static String getAlgorithm(URI digestUri) {
return schemeToAlgorithm.get(digestUri.getScheme() + ":" + digestUri.getSchemeSpecificPart().split(":", 2)[0]);
}

private static String asString(final byte[] data) {
return encodeHexString(data);
}

}
33 changes: 33 additions & 0 deletions fcrepo-kernel/src/main/java/org/fcrepo/utils/FedoraTypesUtils.java
Expand Up @@ -131,6 +131,9 @@ public String apply(final Value v) {
}
};

/**
* Check if a JCR property is a multivalued property or not
*/
public static Predicate<Property> isMultipleValuedProperty =
new Predicate<Property>() {

Expand All @@ -147,6 +150,9 @@ public boolean apply(final Property p) {
};


/**
* Check if a node is "internal" and should not be exposed e.g. via the REST API
*/
public static Predicate<Node> isInternalNode =
new Predicate<Node>() {

Expand Down Expand Up @@ -247,9 +253,16 @@ public static Binary getBinary(final Node n, final InputStream i, final String h
}


/**
* Get the JCR Node Type manager
* @param node
* @return
* @throws RepositoryException
*/
public static NodeTypeManager getNodeTypeManager(final Node node) throws RepositoryException {
return node.getSession().getWorkspace().getNodeTypeManager();
}

/**
* Get the property definition information (containing type and multi-value information)
* @param node the node to use for inferring the property definition
Expand Down Expand Up @@ -291,14 +304,34 @@ public static String convertDateToXSDString(final long date) {
return FMT.print(dt);
}

/**
* Get the JCR Base version for a node
* @param node
* @return
* @throws RepositoryException
*/
public static Version getBaseVersion(final Node node) throws RepositoryException {
return node.getSession().getWorkspace().getVersionManager().getBaseVersion(node.getPath());
}

/**
* Get the JCR VersionHistory for an existing node
* @param node
* @return
* @throws RepositoryException
*/

public static VersionHistory getVersionHistory(final Node node) throws RepositoryException {
return getVersionHistory(node.getSession(), node.getPath());
}

/**
* Get the JCR VersionHistory for a node at a given JCR path
* @param session
* @param path
* @return
* @throws RepositoryException
*/
public static VersionHistory getVersionHistory(final Session session, final String path) throws RepositoryException {
return session.getWorkspace().getVersionManager().getVersionHistory(path);
}
Expand Down

0 comments on commit 045b1c5

Please sign in to comment.