Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial alignment with Fedora 4.5.2-SNAPSHOT #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/*
*.iml
target/
*~
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.fcrepo</groupId>
<artifactId>fcrepo</artifactId>
<version>4.1.2-SNAPSHOT</version>
<version>4.5.2-SNAPSHOT</version>
</parent>
<artifactId>fcrepo-storage-policy</artifactId>
<name>Fedora Repository Storage Strategies Module</name>
Expand Down
48 changes: 15 additions & 33 deletions src/main/java/org/fcrepo/storage/policy/FedoraStoragePolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
import javax.ws.rs.core.UriInfo;

import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.kernel.models.FedoraResource;
import org.fcrepo.kernel.services.ContainerService;
import org.fcrepo.kernel.services.policy.StoragePolicy;
import org.fcrepo.kernel.services.policy.StoragePolicyDecisionPoint;
import org.modeshape.jcr.api.JcrTools;
import org.fcrepo.kernel.api.models.Container;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.services.ContainerService;
import org.fcrepo.kernel.api.services.policy.StoragePolicy;
import org.fcrepo.kernel.api.services.policy.StoragePolicyDecisionPoint;
import org.slf4j.Logger;
import org.springframework.context.annotation.Scope;

Expand Down Expand Up @@ -84,8 +84,6 @@ public class FedoraStoragePolicy extends AbstractResource {
@Inject
protected ContainerService containerService;

private JcrTools jcrTools;

public static final String POLICY_RESOURCE = "policies";

private static final Logger LOGGER = getLogger(FedoraStoragePolicy.class);
Expand Down Expand Up @@ -137,11 +135,11 @@ public Response post(final @PathParam("path") String path,

final String[] str = split(request); // simple split for now
validateArgs(str.length);
final Node node = getJcrTools().findOrCreateNode(session,
FEDORA_STORAGE_POLICY_PATH, "test");
final Container container = containerService.findOrCreate(session,
FEDORA_STORAGE_POLICY_PATH + "/test");
if (isValidNodeTypeProperty(session, str[0]) ||
isValidConfigurationProperty(str[0])) {
node.setProperty(str[0], new String[]{str[1] + ":" + str[2]});
container.setProperty(str[0], new String[]{str[1] + ":" + str[2]});

// TODO (for now) instantiate PolicyType based on mix:mimeType
final StoragePolicy policy = newPolicyInstance(str[0], str[1], str[2]);
Expand Down Expand Up @@ -198,11 +196,11 @@ protected StoragePolicy newPolicyInstance(final String propertyType,
public Response deleteNodeType(@PathParam("path") final String nodeType)
throws RepositoryException {
LOGGER.debug("Deleting node property{}", nodeType);
final Node node =
getJcrTools().findOrCreateNode(session,
FEDORA_STORAGE_POLICY_PATH, "test");
final Container container =
containerService.findOrCreate(session,
FEDORA_STORAGE_POLICY_PATH + "/test");
if (isValidNodeTypeProperty(session, nodeType)) {
node.getProperty(nodeType).remove();
container.getProperty(nodeType).remove();
session.save();

// remove all MimeType intances (since thats only the stored
Expand Down Expand Up @@ -243,10 +241,10 @@ private Response getAllStoragePolicies() {
private Response getStoragePolicy(final String nodeType) throws RepositoryException {
LOGGER.debug("Get storage policy for: {}", nodeType);
Response.ResponseBuilder response;
final Node node =
getJcrTools().findOrCreateNode(session, FEDORA_STORAGE_POLICY_PATH, "test");
final Container container =
containerService.findOrCreate(session, FEDORA_STORAGE_POLICY_PATH + "/test");

final Property prop = node.getProperty(nodeType);
final Property prop = container.getProperty(nodeType);
if (null == prop) {
throw new PathNotFoundException("StoragePolicy not found: " + nodeType);
}
Expand Down Expand Up @@ -316,22 +314,6 @@ private InputPattern(final int l) {
}
}

private JcrTools getJcrTools() {
if (null == jcrTools) {
this.jcrTools = new JcrTools(true);
}
return jcrTools;
}

/**
* Only for UNIT TESTING
* @param jcrTools
*/
@VisibleForTesting
public void setJcrTools(final JcrTools jcrTools) {
this.jcrTools = jcrTools;
}

private UriInfo getUriInfo() {
return this.uriInfo;
}
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/org/fcrepo/storage/policy/MimeTypeStoragePolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package org.fcrepo.storage.policy;

import static org.modeshape.jcr.api.JcrConstants.JCR_MIME_TYPE;
import static org.slf4j.LoggerFactory.getLogger;

import org.fcrepo.kernel.services.policy.StoragePolicy;
import org.fcrepo.kernel.api.models.FedoraBinary;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.services.policy.StoragePolicy;
import org.slf4j.Logger;

import javax.jcr.Node;
Expand Down Expand Up @@ -56,25 +57,24 @@ public String toString() {
* this policy's mime type, return the hint.
*/
@Override
public String evaluatePolicy(final Node n) {
LOGGER.debug("Evaluating MimeTypeStoragePolicy ({} -> {}) for {} ", mimeType,
hint, n);
try {
final String nodeMimeType =
n.getProperty(JCR_MIME_TYPE).getString();

LOGGER.trace("Found mime type {}", nodeMimeType);

if (nodeMimeType.equals(mimeType)) {
LOGGER.trace("{} matched this mime type."
+ "Returning hint {} ", mimeType, hint);
return hint;
}
} catch (final RepositoryException e) {
LOGGER.info("Got Exception evaluating policy: {}", e.getMessage());
public String evaluatePolicy(final FedoraResource resource) {
LOGGER.debug("Evaluating MimeTypeStoragePolicy ({} -> {}) for {} ", mimeType, hint, resource);

if (!(resource instanceof FedoraBinary)) {
LOGGER.warn("Unable to evaluate policy on non-Binary resources! {}", resource);
return null;
}

FedoraBinary binaryResource = (FedoraBinary) resource;
final String nodeMimeType = binaryResource.getMimeType();

LOGGER.trace("Found mime type {}", nodeMimeType);

if (nodeMimeType.equals(mimeType)) {
LOGGER.debug("{} matched this mime type. Returning hint {} ", mimeType, hint);
return hint;
}

return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

import javax.jcr.Node;

import org.fcrepo.kernel.services.policy.StoragePolicy;
import org.fcrepo.kernel.services.policy.StoragePolicyDecisionPoint;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.services.policy.StoragePolicy;
import org.fcrepo.kernel.api.services.policy.StoragePolicyDecisionPoint;
import org.slf4j.Logger;

import com.google.common.base.MoreObjects;
Expand Down Expand Up @@ -54,9 +55,9 @@ public StoragePolicyDecisionPointImpl() {
* @see org.fcrepo.kernel.services.policy.StoragePolicyDecisionPoint#evaluatePolicies(javax.jcr.Node)
*/
@Override
public String evaluatePolicies(final Node n) {
public String evaluatePolicies(final FedoraResource resource) {
for (final StoragePolicy p : this) {
final String h = p.evaluatePolicy(n);
final String h = p.evaluatePolicy(resource);
if (h != null) {
return h;
}
Expand All @@ -66,7 +67,7 @@ public String evaluatePolicies(final Node n) {

/*
* (non-Javadoc)
* @see org.fcrepo.kernel.services.policy.StoragePolicyDecisionPoint#setPolicies(java.util.List)
* @see org.fcrepo.kernel.api.services.policy.StoragePolicyDecisionPoint#setPolicies(java.util.List)
*/
@Override
public void setPolicies(final List<StoragePolicy> policies) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.tika.io.IOUtils;
import org.fcrepo.kernel.services.policy.StoragePolicy;
import org.fcrepo.kernel.api.services.policy.StoragePolicy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package org.fcrepo.storage.policy;

import org.fcrepo.kernel.services.policy.StoragePolicy;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.services.policy.StoragePolicy;
import org.junit.Test;

import javax.jcr.Node;
Expand Down Expand Up @@ -44,11 +45,11 @@ public void shouldEvaluatePolicyAndReturnHint() throws Exception {

final Property mockProperty = mock(Property.class);
when(mockProperty.getString()).thenReturn("image/x-dummy");
final Node mockContentNode = mock(Node.class);
when(mockContentNode.getProperty(JCR_MIME_TYPE)).thenReturn(
final FedoraResource mockFedoraResource = mock(FedoraResource.class);
when(mockFedoraResource.getProperty(JCR_MIME_TYPE)).thenReturn(
mockProperty);

final String receivedHint = policy.evaluatePolicy(mockContentNode);
final String receivedHint = policy.evaluatePolicy(mockFedoraResource);

assertThat(receivedHint, is(hint));
}
Expand All @@ -60,11 +61,11 @@ public void shouldEvaluatePolicyAndReturnNoHint() throws Exception {

final Property mockProperty = mock(Property.class);
when(mockProperty.getString()).thenReturn("application/x-other");
final Node mockContentNode = mock(Node.class);
when(mockContentNode.getProperty(JCR_MIME_TYPE)).thenReturn(
final FedoraResource mockFedoraResource = mock(FedoraResource.class);
when(mockFedoraResource.getProperty(JCR_MIME_TYPE)).thenReturn(
mockProperty);

final String receivedHint = policy.evaluatePolicy(mockContentNode);
final String receivedHint = policy.evaluatePolicy(mockFedoraResource);

assertNull(receivedHint);
}
Expand All @@ -75,12 +76,12 @@ public void shouldEvaluatePolicyAndReturnNoHintOnException()
final String hint = "store-id";
final StoragePolicy policy = new MimeTypeStoragePolicy("image/x-dummy", hint);

final Node mockContentNode = mock(Node.class);
final FedoraResource mockFedoraResource = mock(FedoraResource.class);

when(mockContentNode.getProperty(JCR_MIME_TYPE)).thenThrow(
when(mockFedoraResource.getProperty(JCR_MIME_TYPE)).thenThrow(
new RepositoryException());

final String receivedHint = policy.evaluatePolicy(mockContentNode);
final String receivedHint = policy.evaluatePolicy(mockFedoraResource);

assertNull("Received hint was not null!", receivedHint);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
import javax.jcr.Session;

import com.hp.hpl.jena.rdf.model.Model;
import org.fcrepo.kernel.impl.rdf.impl.DefaultIdentifierTranslator;
import org.fcrepo.kernel.impl.services.BinaryServiceImpl;
import org.fcrepo.kernel.impl.services.ContainerServiceImpl;
import org.fcrepo.kernel.impl.services.functions.GetBinaryKey;
import org.fcrepo.kernel.models.FedoraBinary;
import org.fcrepo.kernel.services.BinaryService;
import org.fcrepo.kernel.services.ContainerService;
import org.fcrepo.kernel.modeshape.rdf.impl.DefaultIdentifierTranslator;
import org.fcrepo.kernel.modeshape.services.BinaryServiceImpl;
import org.fcrepo.kernel.modeshape.services.ContainerServiceImpl;
import org.fcrepo.kernel.api.models.FedoraBinary;
import org.fcrepo.kernel.api.services.BinaryService;
import org.fcrepo.kernel.api.services.ContainerService;
import org.junit.Before;
import org.junit.Test;
import org.modeshape.jcr.JcrRepositoryFactory;
Expand All @@ -63,8 +62,6 @@ public class TiffStoragePolicyStorageIT {

private StoragePolicyDecisionPointImpl pdp;

GetBinaryKey getBinaryKey = new GetBinaryKey();

@Before
public void setLogger() {
logger = LoggerFactory.getLogger(this.getClass());
Expand Down