Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pure code reformatting
  • Loading branch information
ajs6f committed Dec 3, 2013
1 parent ef7ce03 commit 3790f9c
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 219 deletions.
Expand Up @@ -16,7 +16,6 @@

package org.fcrepo.transform;

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

Expand All @@ -27,6 +26,8 @@
import java.util.Map;

import static com.google.common.base.Throwables.propagate;
import static org.apache.jena.riot.WebContent.contentTypeSPARQLQuery;
import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH;

/**
* Get a Transformation from a MediaType
Expand All @@ -39,9 +40,9 @@ public class TransformationFactory {
* 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);
mimeToTransform = new HashMap<>();
mimeToTransform.put(contentTypeSPARQLQuery, SparqlQueryTransform.class);
mimeToTransform.put(APPLICATION_RDF_LDPATH, LDPathTransform.class);

}

Expand Down
Expand Up @@ -19,14 +19,12 @@
import com.codahale.metrics.annotation.Timed;
import com.hp.hpl.jena.query.ResultSet;
import org.apache.commons.io.IOUtils;
import org.apache.jena.riot.WebContent;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.fcrepo.http.api.FedoraNodes;
import org.fcrepo.http.commons.AbstractResource;
import org.fcrepo.http.commons.api.rdf.HttpGraphSubjects;
import org.fcrepo.http.commons.responses.BaseHtmlProvider;
import org.fcrepo.http.commons.responses.ViewHelpers;
import org.fcrepo.http.commons.session.InjectedSession;
import org.fcrepo.kernel.rdf.GraphSubjects;
Expand Down Expand Up @@ -63,10 +61,23 @@

import static com.google.common.util.concurrent.Futures.addCallback;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.Response.ok;
import static org.apache.jena.riot.WebContent.contentTypeN3;
import static org.apache.jena.riot.WebContent.contentTypeNTriples;
import static org.apache.jena.riot.WebContent.contentTypeRDFXML;
import static org.apache.jena.riot.WebContent.contentTypeResultsBIO;
import static org.apache.jena.riot.WebContent.contentTypeResultsJSON;
import static org.apache.jena.riot.WebContent.contentTypeResultsXML;
import static org.apache.jena.riot.WebContent.contentTypeSPARQLQuery;
import static org.apache.jena.riot.WebContent.contentTypeSSE;
import static org.apache.jena.riot.WebContent.contentTypeTextCSV;
import static org.apache.jena.riot.WebContent.contentTypeTextPlain;
import static org.apache.jena.riot.WebContent.contentTypeTextTSV;
import static org.apache.jena.riot.WebContent.contentTypeTurtle;
import static org.fcrepo.http.commons.domain.RDFMediaType.POSSIBLE_SPARQL_RDF_VARIANTS;
import static org.fcrepo.http.commons.responses.BaseHtmlProvider.templateFilenameExtension;
import static org.fcrepo.http.commons.responses.BaseHtmlProvider.templatesLocation;
import static org.fcrepo.http.commons.responses.BaseHtmlProvider.velocityPropertiesLocation;
import static org.slf4j.LoggerFactory.getLogger;

/**
Expand Down Expand Up @@ -99,16 +110,18 @@ public Response sparqlQueryForm() throws IOException, RepositoryException {

final Properties properties = new Properties();
final URL propertiesUrl =
getClass().getResource(BaseHtmlProvider.velocityPropertiesLocation);
VelocityEngine velocity = new VelocityEngine();
getClass().getResource(velocityPropertiesLocation);
final VelocityEngine velocity = new VelocityEngine();
LOGGER.debug("Using Velocity configuration from {}", propertiesUrl);
try (final InputStream propertiesStream = propertiesUrl.openStream()) {
properties.load(propertiesStream);
}
velocity.init(properties);
final Template template =
velocity.getTemplate(templatesLocation + "/search-sparql" + templateFilenameExtension);
final org.apache.velocity.context.Context context = new VelocityContext();
velocity.getTemplate(templatesLocation + "/search-sparql"
+ templateFilenameExtension);
final org.apache.velocity.context.Context context =
new VelocityContext();
context.put("uriInfo", uriInfo);
context.put("model", new NamespaceRdfContext(session).asModel());
context.put("helpers", ViewHelpers.getInstance());
Expand All @@ -123,7 +136,7 @@ public void write(final OutputStream output) throws IOException, WebApplicationE
}
};

return Response.ok(stream).build();
return ok(stream).build();
}

/**
Expand All @@ -136,17 +149,13 @@ public void write(final OutputStream output) throws IOException, WebApplicationE
*/
@POST
@Consumes({contentTypeSPARQLQuery})
@Produces({WebContent.contentTypeTextTSV,
WebContent.contentTypeTextCSV, WebContent.contentTypeSSE,
WebContent.contentTypeTextPlain,
WebContent.contentTypeResultsJSON,
WebContent.contentTypeResultsXML,
WebContent.contentTypeResultsBIO,
WebContent.contentTypeTurtle, WebContent.contentTypeN3,
WebContent.contentTypeNTriples, WebContent.contentTypeRDFXML})
@Produces({contentTypeTextTSV, contentTypeTextCSV, contentTypeSSE,
contentTypeTextPlain, contentTypeResultsJSON,
contentTypeResultsXML, contentTypeResultsBIO, contentTypeTurtle,
contentTypeN3, contentTypeNTriples, contentTypeRDFXML})
public Response runSparqlQuery(final InputStream requestBodyStream,
@Context final Request request,
@Context final UriInfo uriInfo) throws IOException, RepositoryException {
@Context final Request request, @Context final UriInfo uriInfo)
throws IOException, RepositoryException {

final GraphSubjects graphSubjects = new HttpGraphSubjects(session, FedoraNodes.class, uriInfo);

Expand All @@ -163,11 +172,12 @@ public Response runSparqlQuery(final InputStream requestBodyStream,

final ResultSet resultSet = jqlConverter.execute();

ResultSetStreamingOutput streamingOutput = new ResultSetStreamingOutput(resultSet,
bestPossibleResponse.getMediaType());
final ResultSetStreamingOutput streamingOutput =
new ResultSetStreamingOutput(resultSet, bestPossibleResponse
.getMediaType());

addCallback(streamingOutput, new LogoutCallback(session));

return Response.ok(streamingOutput).build();
return ok(streamingOutput).build();
}
}
Expand Up @@ -16,8 +16,24 @@

package org.fcrepo.transform.http;

import static javax.jcr.nodetype.NodeType.NT_BASE;
import static javax.jcr.nodetype.NodeType.NT_FILE;
import static javax.jcr.nodetype.NodeType.NT_FOLDER;
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.apache.jena.riot.WebContent.contentTypeN3;
import static org.apache.jena.riot.WebContent.contentTypeNTriples;
import static org.apache.jena.riot.WebContent.contentTypeRDFXML;
import static org.apache.jena.riot.WebContent.contentTypeResultsBIO;
import static org.apache.jena.riot.WebContent.contentTypeResultsJSON;
import static org.apache.jena.riot.WebContent.contentTypeResultsXML;
import static org.apache.jena.riot.WebContent.contentTypeSPARQLQuery;
import static org.apache.jena.riot.WebContent.contentTypeSSE;
import static org.apache.jena.riot.WebContent.contentTypeTextCSV;
import static org.apache.jena.riot.WebContent.contentTypeTextPlain;
import static org.apache.jena.riot.WebContent.contentTypeTextTSV;
import static org.apache.jena.riot.WebContent.contentTypeTurtle;
import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH;
import static org.fcrepo.transform.transformations.LDPathTransform.CONFIGURATION_FOLDER;
import static org.fcrepo.transform.transformations.LDPathTransform.getNodeTypeTransform;
import static org.slf4j.LoggerFactory.getLogger;

Expand All @@ -29,7 +45,6 @@
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
Expand All @@ -40,7 +55,6 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.PathSegment;

import org.apache.jena.riot.WebContent;
import org.apache.marmotta.ldpath.exception.LDPathParseException;
import org.fcrepo.http.api.FedoraNodes;
import org.fcrepo.http.commons.AbstractResource;
Expand All @@ -49,7 +63,6 @@
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.transform.Transformation;
import org.fcrepo.transform.TransformationFactory;
import org.fcrepo.transform.transformations.LDPathTransform;
import org.modeshape.jcr.api.JcrTools;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -80,38 +93,39 @@ public class FedoraTransform extends AbstractResource {
*/
@PostConstruct
public void setUpRepositoryConfiguration() throws RepositoryException,
IOException {
IOException {

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

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

// register our CND
jcrTools.registerNodeTypes(session, "ldpath.cnd");

// create the configuration base path
jcrTools.findOrCreateNode(session,
"/fedora:system/fedora:transform", "fedora:configuration",
"fedora:node_type_configuration");
final Node node =
jcrTools.findOrCreateNode(session,
LDPathTransform.CONFIGURATION_FOLDER + "default",
NodeType.NT_FOLDER, NodeType.NT_FOLDER);
logger.debug("Transforming node: {}", node.getPath());
// register an initial demo program
if (!node.hasNode(NodeType.NT_BASE)) {
final Node base_config =
node.addNode(NodeType.NT_BASE, NodeType.NT_FILE);
jcrTools.uploadFile(session, base_config.getPath(), getClass()
.getResourceAsStream(
"/ldpath/default/nt_base_ldpath_program.txt"));
try {
final JcrTools jcrTools = new JcrTools(true);

// register our CND
jcrTools.registerNodeTypes(session, "ldpath.cnd");

// create the configuration base path
jcrTools.findOrCreateNode(session,
"/fedora:system/fedora:transform", "fedora:configuration",
"fedora:node_type_configuration");
final Node node =
jcrTools.findOrCreateNode(session, CONFIGURATION_FOLDER
+ "default", NT_FOLDER, NT_FOLDER);
logger.debug("Transforming node: {}", node.getPath());
// register an initial demo program
if (!node.hasNode(NT_BASE)) {
final Node base_config = node.addNode(NT_BASE, NT_FILE);
jcrTools.uploadFile(session, base_config.getPath(), getClass()
.getResourceAsStream(
"/ldpath/default/nt_base_ldpath_program.txt"));
}

session.save();
} finally {
session.logout();
}

session.save();
session.logout();
}

/**
Expand All @@ -123,23 +137,22 @@ public void setUpRepositoryConfiguration() throws RepositoryException,
*/
@GET
@Path("{program}")
@Produces({MediaType.APPLICATION_JSON})
@Produces({APPLICATION_JSON})
@Timed
public Object evaluateLdpathProgram(@PathParam("path")
final List<PathSegment> pathList, @PathParam("program")
final String program) throws RepositoryException, LDPathParseException {

try {
final String path = toPath(pathList);
final FedoraResource object =
nodeService.getObject(session, path);
final FedoraResource object = nodeService.getObject(session, path);

final Transformation t =
getNodeTypeTransform(object.getNode(), program);
getNodeTypeTransform(object.getNode(), program);

final Dataset propertiesDataset =
object.getPropertiesDataset(new HttpGraphSubjects(
session, FedoraNodes.class, uriInfo));
object.getPropertiesDataset(new HttpGraphSubjects(session,
FedoraNodes.class, uriInfo));

return t.apply(propertiesDataset);

Expand All @@ -159,14 +172,10 @@ public Object evaluateLdpathProgram(@PathParam("path")
*/
@POST
@Consumes({APPLICATION_RDF_LDPATH, contentTypeSPARQLQuery})
@Produces({MediaType.APPLICATION_JSON, WebContent.contentTypeTextTSV,
WebContent.contentTypeTextCSV, WebContent.contentTypeSSE,
WebContent.contentTypeTextPlain,
WebContent.contentTypeResultsJSON,
WebContent.contentTypeResultsXML,
WebContent.contentTypeResultsBIO,
WebContent.contentTypeTurtle, WebContent.contentTypeN3,
WebContent.contentTypeNTriples, WebContent.contentTypeRDFXML})
@Produces({APPLICATION_JSON, contentTypeTextTSV, contentTypeTextCSV,
contentTypeSSE, contentTypeTextPlain, contentTypeResultsJSON,
contentTypeResultsXML, contentTypeResultsBIO, contentTypeTurtle,
contentTypeN3, contentTypeNTriples, contentTypeRDFXML})
@Timed
public Object evaluateTransform(@PathParam("path")
final List<PathSegment> pathList, @HeaderParam("Content-Type")
Expand All @@ -188,7 +197,5 @@ public Object evaluateTransform(@PathParam("path")
} finally {
session.logout();
}

}

}
Expand Up @@ -15,13 +15,14 @@
*/
package org.fcrepo.transform.http;

import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.jcr.FedoraJcrTypes.ROOT;
import static org.fcrepo.kernel.RdfLexicon.HAS_SPARQL_ENDPOINT;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Resource;
import org.fcrepo.http.commons.api.rdf.UriAwareResourceModelFactory;
import org.fcrepo.jcr.FedoraJcrTypes;
import org.fcrepo.kernel.FedoraResource;
import org.fcrepo.kernel.RdfLexicon;
import org.fcrepo.kernel.rdf.GraphSubjects;
import org.springframework.stereotype.Component;

Expand All @@ -36,19 +37,16 @@
@Component
public class TransformResources implements UriAwareResourceModelFactory {
@Override
public Model createModelForResource(FedoraResource resource,
UriInfo uriInfo,
GraphSubjects graphSubjects) throws RepositoryException {
final Model model = ModelFactory.createDefaultModel();
public Model createModelForResource(final FedoraResource resource,
final UriInfo uriInfo,
final GraphSubjects graphSubjects) throws RepositoryException {
final Model model = createDefaultModel();
final Resource s = graphSubjects.getGraphSubject(resource.getNode());

if (resource.getNode().getPrimaryNodeType().isNodeType(FedoraJcrTypes.ROOT)) {
model.add(s,
RdfLexicon.HAS_SPARQL_ENDPOINT,
model.createResource(uriInfo.getBaseUriBuilder()
.path(FedoraSparql.class)
.build()
.toASCIIString()));
if (resource.getNode().getPrimaryNodeType().isNodeType(ROOT)) {
model.add(s, HAS_SPARQL_ENDPOINT, model.createResource(uriInfo
.getBaseUriBuilder().path(FedoraSparql.class).build()
.toASCIIString()));
}

return model;
Expand Down

0 comments on commit 3790f9c

Please sign in to comment.