Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
start trying to make it pluggable
  • Loading branch information
cbeer committed Jul 17, 2013
1 parent c1abf99 commit ca9f5f3
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 68 deletions.
Expand Up @@ -17,34 +17,66 @@
package org.fcrepo.transform;

import org.apache.jena.riot.WebContent;
import org.fcrepo.transform.services.LDPathTransform;
import org.fcrepo.transform.services.SparqlQueryTransform;
import org.fcrepo.transform.transformations.LDPathTransform;
import org.fcrepo.transform.transformations.SparqlQueryTransform;

import javax.ws.rs.core.MediaType;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.Throwables.propagate;

/**
* Get a Transformation from a MediaType
*/
public class TransformationFactory {

private Map<String, Class> mimeToTransform;

/**
* Get a new TransformationFactory with the default classes
*/
public TransformationFactory() {
mimeToTransform = new HashMap<String, Class>();
mimeToTransform.put(WebContent.contentTypeSPARQLQuery, SparqlQueryTransform.class);
mimeToTransform.put(LDPathTransform.APPLICATION_RDF_LDPATH, LDPathTransform.class);

}

/**
* Get a new TransformationFactory using the provided mapping
* @param mimeToTransform
*/
public TransformationFactory(Map<String, Class> mimeToTransform) {
mimeToTransform = mimeToTransform;
}

/**
* Get a Transformation from a MediaType and an InputStream with
* the transform program
* @param contentType
* @param inputStream
* @return
*/
public static Transformation getTransform(final MediaType contentType,
public Transformation getTransform(final MediaType contentType,
final InputStream inputStream) {
switch (contentType.toString()) {
case LDPathTransform.APPLICATION_RDF_LDPATH:
return new LDPathTransform(inputStream);

case WebContent.contentTypeSPARQLQuery:
return new SparqlQueryTransform(inputStream);
if (mimeToTransform.containsKey(contentType.toString())) {
Class transform = mimeToTransform.get(contentType.toString());

if (Transformation.class.isAssignableFrom(transform)) {
try {
return (Transformation)(transform.getConstructor(InputStream.class).newInstance(inputStream));
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
propagate(e);
}
}

}

return null;

}
}
Expand Up @@ -25,9 +25,10 @@
import org.fcrepo.transform.Transformation;
import org.fcrepo.transform.TransformationFactory;
import org.fcrepo.session.InjectedSession;
import org.fcrepo.transform.services.LDPathTransform;
import org.fcrepo.transform.transformations.LDPathTransform;
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

Expand All @@ -51,8 +52,8 @@
import java.util.List;

import static org.apache.jena.riot.WebContent.contentTypeSPARQLQuery;
import static org.fcrepo.transform.services.LDPathTransform.APPLICATION_RDF_LDPATH;
import static org.fcrepo.transform.services.LDPathTransform.getNodeTypeTransform;
import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH;
import static org.fcrepo.transform.transformations.LDPathTransform.getNodeTypeTransform;
import static org.slf4j.LoggerFactory.getLogger;

@Component
Expand All @@ -65,6 +66,8 @@ public class FedoraTransform extends AbstractResource {

private final Logger logger = getLogger(FedoraTransform.class);

@Autowired(required = false)
private TransformationFactory transformationFactory;

/**
* Register the LDPath configuration tree in JCR
Expand All @@ -74,6 +77,11 @@ public class FedoraTransform extends AbstractResource {
@PostConstruct
public void setUpRepositoryConfiguration()
throws RepositoryException, IOException {

if (transformationFactory == null) {
transformationFactory = new TransformationFactory();
}

final Session session = sessions.getSession();
final JcrTools jcrTools = new JcrTools(true);

Expand Down Expand Up @@ -160,7 +168,7 @@ public Object evaluateTransform(
final Dataset propertiesDataset = object.getPropertiesDataset();

Transformation t =
TransformationFactory.getTransform(contentType, requestBodyStream);
transformationFactory.getTransform(contentType, requestBodyStream);
return t.apply(propertiesDataset);
} finally {
session.logout();
Expand Down
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.fcrepo.transform.services;
package org.fcrepo.transform.transformations;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
Expand Down
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.fcrepo.transform.services;
package org.fcrepo.transform.transformations;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
Expand Down
Expand Up @@ -25,14 +25,13 @@
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.fcrepo.services.ObjectService;
import org.fcrepo.transform.services.LDPathTransform;
import org.fcrepo.transform.transformations.LDPathTransform;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;

import java.io.ByteArrayInputStream;

Expand Down
Expand Up @@ -18,7 +18,7 @@

import org.apache.marmotta.ldpath.exception.LDPathParseException;
import org.fcrepo.FedoraObject;
import org.fcrepo.transform.services.LDPathTransform;
import org.fcrepo.transform.transformations.LDPathTransform;
import org.fcrepo.services.ObjectService;
import org.junit.Before;
import org.junit.Test;
Expand Down
Expand Up @@ -18,11 +18,10 @@

import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import org.apache.marmotta.ldpath.exception.LDPathParseException;
import org.fcrepo.FedoraObject;
import org.fcrepo.services.ObjectService;
import org.fcrepo.transform.services.SparqlQueryTransform;
import org.fcrepo.transform.transformations.SparqlQueryTransform;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down
Expand Up @@ -16,8 +16,8 @@
package org.fcrepo.transform;

import org.apache.jena.riot.WebContent;
import org.fcrepo.transform.services.LDPathTransform;
import org.fcrepo.transform.services.SparqlQueryTransform;
import org.fcrepo.transform.transformations.LDPathTransform;
import org.fcrepo.transform.transformations.SparqlQueryTransform;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -34,14 +34,18 @@ public class TransformationFactoryTest {
@Mock
InputStream mockInputStream;

TransformationFactory transformationFactory;

@Before
public void setUp() {
initMocks(this);
transformationFactory = new TransformationFactory();
}

@Test
public void testLDPathCreation() {

final Transformation transform = TransformationFactory.getTransform(MediaType.valueOf(LDPathTransform.APPLICATION_RDF_LDPATH), mockInputStream);
final Transformation transform = transformationFactory.getTransform(MediaType.valueOf(LDPathTransform.APPLICATION_RDF_LDPATH), mockInputStream);

assertEquals(new LDPathTransform(mockInputStream), transform);

Expand All @@ -50,7 +54,7 @@ public void testLDPathCreation() {
@Test
public void testSparqlCreation() {

final Transformation transform = TransformationFactory.getTransform(MediaType.valueOf(WebContent.contentTypeSPARQLQuery), mockInputStream);
final Transformation transform = transformationFactory.getTransform(MediaType.valueOf(WebContent.contentTypeSPARQLQuery), mockInputStream);

assertEquals(new SparqlQueryTransform(mockInputStream), transform);

Expand All @@ -60,7 +64,7 @@ public void testSparqlCreation() {
@Test
public void testOtherCreation() {

final Transformation transform = TransformationFactory.getTransform(MediaType.valueOf("some/mime-type"), mockInputStream);
final Transformation transform = transformationFactory.getTransform(MediaType.valueOf("some/mime-type"), mockInputStream);

assertNull(transform);

Expand Down
Expand Up @@ -18,18 +18,15 @@

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.sparql.util.Symbol;
import org.apache.jena.riot.WebContent;
import org.fcrepo.FedoraResource;
import org.fcrepo.services.NodeService;
import org.fcrepo.test.util.TestHelpers;
import org.fcrepo.transform.services.LDPathTransform;
import org.fcrepo.transform.Transformation;
import org.fcrepo.transform.TransformationFactory;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mock;

Expand All @@ -41,15 +38,11 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static org.fcrepo.test.util.PathSegmentImpl.createPathList;
import static org.fcrepo.test.util.TestHelpers.getUriInfoImpl;
import static org.fcrepo.test.util.TestHelpers.mockSession;
import static org.fcrepo.utils.TestHelpers.setField;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

Expand All @@ -70,11 +63,18 @@ public class FedoraTransformTest {
private Session mockSession;
private UriInfo uriInfo;

@Mock
private TransformationFactory mockTransformationFactory;

@Mock
Transformation mockTransform;

@Before
public void setUp() throws NoSuchFieldException, RepositoryException {
initMocks(this);
testObj = new FedoraTransform();
setField(testObj, "nodeService", mockNodeService);
TestHelpers.setField(testObj, "nodeService", mockNodeService);
TestHelpers.setField(testObj, "transformationFactory", mockTransformationFactory);

this.uriInfo = getUriInfoImpl();
TestHelpers.setField(testObj, "uriInfo", uriInfo);
Expand All @@ -85,7 +85,7 @@ public void setUp() throws NoSuchFieldException, RepositoryException {
}

@Test
public void testEvaluateSparqlTransform() throws Exception {
public void testEvaluateTransform() throws Exception {
when(mockNodeService.getObject(mockSession, "/testObject")).thenReturn(mockResource);
final Model model = ModelFactory.createDefaultModel();
model.add(model.createResource("http://example.org/book/book1"),
Expand All @@ -98,36 +98,13 @@ public void testEvaluateSparqlTransform() throws Exception {
"{\n" +
" <http://example.org/book/book1> <http://purl.org/dc/elements/1.1/title> ?title .\n" +
"} ").getBytes());
final Object testObject = testObj.evaluateTransform(createPathList("testObject"), MediaType.valueOf(WebContent.contentTypeSPARQLQuery), query);
assert(testObject instanceof QueryExecution);

QueryExecution qexec = (QueryExecution)testObject;

try {
final ResultSet resultSet = qexec.execSelect();
assertTrue(resultSet.hasNext());
} finally {
qexec.close();
}
}

@Test
public void testEvaluateLDPathTransform() throws Exception {
when(mockNodeService.getObject(mockSession, "/testObject")).thenReturn(mockResource);
final Model model = ModelFactory.createDefaultModel();
model.add(model.createResource("http://example.org/book/book1"),
model.createProperty("http://purl.org/dc/elements/1.1/title"),
model.createLiteral("some-title"));
final Dataset dataset = DatasetFactory.create(model);
dataset.getContext().set(Symbol.create("uri"), "http://example.org/book/book1");
when(mockResource.getPropertiesDataset()).thenReturn(dataset);
when(mockTransformationFactory.getTransform(MediaType.valueOf(WebContent.contentTypeSPARQLQuery), query)).thenReturn(mockTransform);

InputStream query = new ByteArrayInputStream(("title = dc:title :: xsd:string ;").getBytes());
final Object testObject = testObj.evaluateTransform(createPathList("testObject"), MediaType.valueOf(LDPathTransform.APPLICATION_RDF_LDPATH), query);
assert(testObject instanceof List);
List<Map<String, Collection<Object>>> results = (List<Map<String, Collection<Object>>>) testObject;
testObj.evaluateTransform(createPathList("testObject"), MediaType.valueOf(WebContent.contentTypeSPARQLQuery), query);

assertTrue(results.get(0).get("title").contains("some-title"));
verify(mockTransform).apply(dataset);
}


}
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.fcrepo.transform.services;
package org.fcrepo.transform.transformations;

import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.DatasetFactory;
Expand Down
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fcrepo.transform.services;
package org.fcrepo.transform.transformations;

import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.QueryExecution;
Expand Down
2 changes: 1 addition & 1 deletion fcrepo-transform/src/test/resources/spring-test/rest.xml
Expand Up @@ -15,7 +15,7 @@

<context:annotation-config/>

<context:component-scan base-package="org.fcrepo.services, org.fcrepo.session, org.fcrepo.api, org.fcrepo.transform.http, org.fcrepo.transform.services, org.fcrepo.serialization, org.fcrepo.exceptionhandlers"/>
<context:component-scan base-package="org.fcrepo.services, org.fcrepo.session, org.fcrepo.api, org.fcrepo.transform.http, org.fcrepo.serialization, org.fcrepo.exceptionhandlers"/>

<bean class="org.fcrepo.session.SessionFactory" />

Expand Down
4 changes: 2 additions & 2 deletions fcrepo-transform/src/test/resources/web.xml
Expand Up @@ -21,8 +21,8 @@
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>org.fcrepo.services, org.fcrepo.session, org.fcrepo.transform.http,
org.fcrepo.transform.services, org.fcrepo.exceptionhandlers</param-value>
<param-value>org.fcrepo.services, org.fcrepo.session,
org.fcrepo.transform.http,org.fcrepo.exceptionhandlers</param-value>

</init-param>
<init-param>
Expand Down

0 comments on commit ca9f5f3

Please sign in to comment.