Skip to content

Commit

Permalink
Minor fcrepo-transform code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ajs6f authored and Andrew Woods committed Jun 30, 2015
1 parent 5edb21c commit 1a4cf67
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 127 deletions.
Expand Up @@ -17,7 +17,6 @@

import org.fcrepo.kernel.utils.iterators.RdfStream;

import java.io.InputStream;
import java.util.function.Function;

/**
Expand All @@ -28,24 +27,4 @@
*/
public interface Transformation<T> extends Function<RdfStream, T> {

/**
* Execute a transform on an rdf stream
* @param stream the stream
* @return transformed output
*/
@Override
T apply(final RdfStream stream);

/**
* Get the Query the transformation is using
* @return query used by the transformation as an input stream
*/
InputStream getQuery();

/**
* @param query the query
* @return a new Transform of this type, for use as a factory
*/
Transformation<T> newTransform(InputStream query);

}
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

import static org.apache.jena.riot.WebContent.contentTypeSPARQLQuery;
import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH;
Expand All @@ -31,18 +32,20 @@
* Get a Transformation from a MediaType
*
* @author cbeer
* @author ajs6f
*/
public class TransformationFactory {

private Map<String, Transformation<?>> mimeToTransform = new HashMap<>();
@SuppressWarnings("rawtypes")
private final Map<String, Function<InputStream, Transformation>> mimeToTransform = new HashMap<>();

/**
* Get a new TransformationFactory with the default classes
* @throws SecurityException if security exception occurred
*/
public TransformationFactory() {
mimeToTransform.put(contentTypeSPARQLQuery, new SparqlQueryTransform(null));
mimeToTransform.put(APPLICATION_RDF_LDPATH, new LDPathTransform(null));
mimeToTransform.put(contentTypeSPARQLQuery, SparqlQueryTransform::new);
mimeToTransform.put(APPLICATION_RDF_LDPATH, LDPathTransform::new);
}

/**
Expand All @@ -56,7 +59,7 @@ public TransformationFactory() {
public <T> Transformation<T> getTransform(final MediaType contentType, final InputStream inputStream) {
final String mimeType = contentType.toString();
if (mimeToTransform.containsKey(mimeType)) {
return (Transformation<T>) mimeToTransform.get(contentType.toString()).newTransform(inputStream);
return mimeToTransform.get(contentType.toString()).apply(inputStream);
}
throw new UnsupportedOperationException(
"No transform type exists for media type " + mimeType + "!");
Expand Down
Expand Up @@ -60,7 +60,7 @@ public void writeTo(final QueryExecution qexec, final Class<?> type,
mediaType);

// add standard headers
httpHeaders.put("Content-type", singletonList((Object) mediaType.toString()));
httpHeaders.put("Content-type", singletonList(mediaType.toString()));

try {
final ResultSet resultSet = qexec.execSelect();
Expand Down
Expand Up @@ -22,9 +22,11 @@
import org.apache.marmotta.ldpath.LDPath;
import org.apache.marmotta.ldpath.backend.jena.GenericJenaBackend;
import org.apache.marmotta.ldpath.exception.LDPathParseException;

import org.fcrepo.kernel.exception.RepositoryRuntimeException;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.fcrepo.transform.Transformation;

import org.slf4j.Logger;

import javax.jcr.Node;
Expand All @@ -41,6 +43,7 @@
import java.util.Objects;
import java.util.Set;

import static com.google.common.collect.ImmutableList.builder;
import static com.google.common.collect.ImmutableSortedSet.orderedBy;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createResource;
import static org.apache.http.HttpStatus.SC_BAD_REQUEST;
Expand All @@ -57,22 +60,15 @@ public class LDPathTransform implements Transformation<List<Map<String, Collecti

public static final String CONFIGURATION_FOLDER = "/fedora:system/fedora:transform/fedora:ldpath/";

private static final Comparator<NodeType> BY_NAME =
(final NodeType o1, final NodeType o2) -> o1.getName().compareTo(o2.getName());

// TODO: this mime type was made up
public static final String APPLICATION_RDF_LDPATH = "application/rdf+ldpath";
private final InputStream query;

private static final Logger LOGGER = getLogger(LDPathTransform.class);

private static final Comparator<NodeType> nodeTypeComp = new Comparator<NodeType>() {

@Override
public int compare(final NodeType o1, final NodeType o2) {
return o1.getName().compareTo(o2.getName());

}
};

/**
* Construct a new Transform from the InputStream
* @param query the query
Expand All @@ -97,27 +93,20 @@ public static LDPathTransform getNodeTypeTransform(final Node node,

final NodeType primaryNodeType = node.getPrimaryNodeType();

final Set<NodeType> supertypes =
orderedBy(nodeTypeComp).add(primaryNodeType.getSupertypes())
.build();
final Set<NodeType> mixinTypes =
orderedBy(nodeTypeComp).add(node.getMixinNodeTypes()).build();
final Set<NodeType> supertypes = orderedBy(BY_NAME).add(primaryNodeType.getSupertypes()).build();
final Set<NodeType> mixinTypes = orderedBy(BY_NAME).add(node.getMixinNodeTypes()).build();

// start with mixins, primary type, and supertypes of primary type
final ImmutableList.Builder<NodeType> nodeTypesB =
new ImmutableList.Builder<NodeType>().addAll(mixinTypes).add(
primaryNodeType).addAll(supertypes);
final ImmutableList.Builder<NodeType> nodeTypesB = builder();
nodeTypesB.addAll(mixinTypes).add(primaryNodeType).addAll(supertypes);

// add supertypes of mixins
for (final NodeType mixin : mixinTypes) {
nodeTypesB.addAll(orderedBy(nodeTypeComp).add(
mixin.getDeclaredSupertypes()).build());
}
mixinTypes.stream().map(mixin -> orderedBy(BY_NAME).add(mixin.getDeclaredSupertypes()).build())
.forEach(nodeTypesB::addAll);

final List<NodeType> nodeTypes = nodeTypesB.build();

LOGGER.debug("Discovered node types: {}", nodeTypes);

for (final NodeType nodeType : nodeTypes) {
if (programNode.hasNode(nodeType.toString())) {
return new LDPathTransform(programNode.getNode(nodeType.toString())
Expand Down Expand Up @@ -152,20 +141,14 @@ private static <F, T> T unsafeCast(final F from) {
return (T) from;
}

@Override
public InputStream getQuery() {
return query;
}

@Override
public boolean equals(final Object other) {
return other instanceof LDPathTransform &&
query.equals(((LDPathTransform)other).getQuery());
return other instanceof LDPathTransform && ((LDPathTransform) other).query.equals(query);
}

@Override
public int hashCode() {
return Objects.hashCode(getQuery());
return Objects.hashCode(query);
}

/**
Expand All @@ -178,9 +161,4 @@ private static LDPath<RDFNode> getLdpathResource(final RdfStream rdfStream) {
return new LDPath<>(new GenericJenaBackend(rdfStream.asModel()));

}

@Override
public LDPathTransform newTransform(final InputStream query) {
return new LDPathTransform(query);
}
}
Expand Up @@ -61,25 +61,13 @@ public QueryExecution apply(final RdfStream rdfStream) {
}
}

@Override
public InputStream getQuery() {
return query;
}

@Override
public boolean equals(final Object other) {
return other instanceof SparqlQueryTransform &&
query.equals(((SparqlQueryTransform)other).getQuery());
return other instanceof SparqlQueryTransform && query.equals(((SparqlQueryTransform)other).query);
}

@Override
public int hashCode() {
return Objects.hashCode(getQuery());
return Objects.hashCode(query);
}

@Override
public SparqlQueryTransform newTransform(final InputStream query) {
return new SparqlQueryTransform(query);
}

}
Expand Up @@ -16,55 +16,35 @@
package org.fcrepo.integration;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.HttpClientBuilder;
import org.fcrepo.http.commons.test.util.SpringContextSingleton;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.jcr.Repository;
import java.io.IOException;
import java.util.UUID;

import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.parseInt;
import static java.lang.System.getProperty;
import static javax.ws.rs.core.Response.Status.CREATED;
import static org.junit.Assert.assertEquals;
import static org.slf4j.LoggerFactory.getLogger;

/**
* <p>Abstract AbstractResourceIT class.</p>
*
* @author cbeer
* @author ajs6f
*/
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class AbstractResourceIT {

protected Repository repo;

protected Logger logger;

@Before
public void setRepo() {
repo =
SpringContextSingleton.getApplicationContext().getBean(
Repository.class);
}

@Before
public void setLogger() {
logger = LoggerFactory.getLogger(this.getClass());
}
protected static Logger logger = getLogger(AbstractResourceIT.class);

protected static final int SERVER_PORT = parseInt(getProperty("fcrepo.dynamic.test.port",
"8080"));

protected static final String HOSTNAME = "localhost";

protected static final String serverAddress = "http://" + HOSTNAME + ":"
Expand All @@ -79,24 +59,6 @@ public AbstractResourceIT() {
client = b.build();
}

protected int getStatus(final HttpUriRequest method)
throws ClientProtocolException, IOException {
logger.debug("Executing: " + method.getMethod() + " to " +
method.getURI());
return client.execute(method).getStatusLine().getStatusCode();
}

/**
* Gets a random (but valid) pid for use in testing. This pid
* is guaranteed to be unique within runs of this application.
*
* @return string containing new random unique Pid
*/
protected static String getRandomUniquePid() {
return UUID.randomUUID().toString();
}


protected static HttpPost postObjMethod(final String pid) {
return new HttpPost(serverAddress + pid);
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package org.fcrepo.integration;

import static java.util.UUID.randomUUID;
import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH;
import static org.junit.Assert.assertEquals;

Expand All @@ -23,6 +24,7 @@
import java.util.UUID;

import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
Expand All @@ -49,7 +51,7 @@ public class FedoraTransformIT extends AbstractResourceIT {
@Test
public void testLdpathWithConfiguredProgram() throws IOException {

final String pid = UUID.randomUUID().toString();
final String pid = "testLdpathWithConfiguredProgram-" + randomUUID();
createObject(pid);
final HttpGet postLdpathProgramRequest
= new HttpGet(serverAddress + "/" + pid + "/fcr:transform/default");
Expand All @@ -66,7 +68,7 @@ public void testLdpathWithConfiguredProgram() throws IOException {
}

@Test
public void testLdpathWithProgramBody() throws Exception {
public void testLdpathWithProgramBody() throws ParseException, IOException {

final String pid = UUID.randomUUID().toString();
createObject(pid);
Expand Down
Expand Up @@ -17,12 +17,14 @@

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.ResultSet;

import org.fcrepo.kernel.models.Container;
import org.fcrepo.kernel.impl.rdf.impl.DefaultIdentifierTranslator;
import org.fcrepo.kernel.impl.rdf.impl.PropertiesRdfContext;
import org.fcrepo.kernel.services.ContainerService;
import org.fcrepo.kernel.utils.iterators.RdfStream;
import org.fcrepo.transform.transformations.SparqlQueryTransform;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.DirtiesContext;
Expand All @@ -31,8 +33,10 @@
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.inject.Inject;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

Expand All @@ -44,14 +48,19 @@
* <p>SparqlQueryTransformIT class.</p>
*
* @author cbeer
* @author ajs6f
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/spring-test/master.xml"})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class SparqlQueryTransformIT extends AbstractResourceIT {

@Inject
ContainerService containerService;
private ContainerService containerService;

@Inject
private Repository repo;

private SparqlQueryTransform testObj;

@Test
Expand Down

0 comments on commit 1a4cf67

Please sign in to comment.