Skip to content

Commit

Permalink
Moved RDF transformation to RDFMessageBodyWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
stain committed May 23, 2013
1 parent 7201062 commit 3691fa3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 205 deletions.
5 changes: 5 additions & 0 deletions orcid-api-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
<build>
<finalName>orcid-api-common</finalName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@

import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import org.orcid.jaxb.model.message.OrcidMessage;
import org.orcid.jaxb.model.message.OrcidProfile;

import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.Ontology;
import com.hp.hpl.jena.rdf.model.ModelFactory;

/**
* 2011-2013 ORCID
Expand All @@ -41,6 +50,8 @@
@Provider
@Produces( { APPLICATION_RDFXML })
public class RDFMessageBodyWriter implements MessageBodyWriter<OrcidMessage> {

private static final String FOAF_0_1 = "http://xmlns.com/foaf/0.1/";

/**
* Ascertain if the MessageBodyWriter supports a particular type.
Expand Down Expand Up @@ -125,12 +136,31 @@ public long getSize(OrcidMessage message, Class<?> type, Type genericType, Annot
* effective if thrown prior to the response being committed.
*/
@Override
public void writeTo(OrcidMessage message, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
public void writeTo(OrcidMessage xml, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
OutputStream entityStream) throws IOException, WebApplicationException {
System.err.println("RDF Oh noooooo");
if (true) {
throw new RuntimeException("X Yay");
}
entityStream.write(message.toString().getBytes());

// Create RDF model
OntModel m = ModelFactory.createOntologyModel();
// TODO: Load FOAF locally, and cached
m.setDynamicImports(true);
OrcidProfile orcidProfile = xml.getOrcidProfile();
String profileUri = orcidProfile.getOrcid().getValue();
Ontology ont = m.createOntology(profileUri + "#");
ont.addImport(m.createResource(FOAF_0_1));
m.setNsPrefix("foaf", FOAF_0_1);

Individual person = m.createIndividual(profileUri, m.getOntClass(FOAF_0_1 + "Person"));

// AnnotationProperty foafName = m.getAnnotationProperty(FOAF_0_1 + "name");
DatatypeProperty foafName = m.getDatatypeProperty(FOAF_0_1 + "name");
person.addProperty(foafName, orcidProfile.getOrcidBio().getPersonalDetails().getCreditName().getContent());

DatatypeProperty foafGivenName = m.getDatatypeProperty(FOAF_0_1 + "givenName");
person.addProperty(foafGivenName, orcidProfile.getOrcidBio().getPersonalDetails().getGivenNames().getContent());

DatatypeProperty familyName = m.getDatatypeProperty(FOAF_0_1 + "familyName");
person.addProperty(familyName, orcidProfile.getOrcidBio().getPersonalDetails().getFamilyName().getContent());

m.write(entityStream);
}
}
5 changes: 0 additions & 5 deletions orcid-pub-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@
<version>${project.parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import static org.orcid.api.common.OrcidApiConstants.VND_ORCID_XML;
import static org.orcid.api.common.OrcidApiConstants.WORKS_PATH;

import java.io.StringWriter;
import java.util.List;
import java.util.Map;

Expand All @@ -47,16 +46,8 @@
import org.orcid.api.common.OrcidApiService;
import org.orcid.api.common.delegator.OrcidApiServiceDelegator;
import org.orcid.jaxb.model.message.OrcidMessage;
import org.orcid.jaxb.model.message.OrcidProfile;
import org.springframework.stereotype.Component;

import com.hp.hpl.jena.ontology.AnnotationProperty;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.Ontology;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Counter;

Expand All @@ -69,7 +60,6 @@
@Path("/")
public class T1OrcidApiServiceImpl implements OrcidApiService<Response> {

private static final String FOAF_0_1 = "http://xmlns.com/foaf/0.1/";
final static Counter T1_GET_REQUESTS = Metrics.newCounter(T1OrcidApiServiceImpl.class, "T1-GET-REQUESTS");
final static Counter T1_SEARCH_REQUESTS = Metrics.newCounter(T1OrcidApiServiceImpl.class, "T1-SEARCH-REQUESTS");

Expand Down Expand Up @@ -148,36 +138,9 @@ public Response viewBioDetailsXml(@PathParam("orcid") String orcid) {
@Path(BIO_PATH)
public Response viewBioDetailsRdf(@PathParam("orcid") String orcid) {
T1_GET_REQUESTS.inc();
Response xmlResp = serviceDelegator.findBioDetails(orcid);
return xmlToRdf(xmlResp, APPLICATION_RDFXML);
return serviceDelegator.findBioDetails(orcid);
}

protected Response xmlToRdf(Response xmlResp, String mediaType) {
OrcidMessage xml = (OrcidMessage) xmlResp.getEntity();

// Create RDF model
OntModel m = ModelFactory.createOntologyModel();
// TODO: Load FOAF locally, and cached
m.setDynamicImports(true);
OrcidProfile orcidProfile = xml.getOrcidProfile();
String profileUri = orcidProfile.getOrcid().getValue();
Ontology ont = m.createOntology(profileUri + "#");
ont.addImport(m.createResource(FOAF_0_1));
m.setNsPrefix("foaf", FOAF_0_1);

Individual person = m.createIndividual(profileUri, m.getOntClass(FOAF_0_1 + "Person"));

// AnnotationProperty foafName = m.getAnnotationProperty(FOAF_0_1 + "name");
DatatypeProperty foafName = m.getDatatypeProperty(FOAF_0_1 + "name");
person.addProperty(foafName, orcidProfile.getOrcidBio().getPersonalDetails().getCreditName().getContent());

// TOOD: Do a stream to Response
StringWriter writer = new StringWriter();
m.write(writer);

String rdf = writer.toString();
return Response.ok(rdf, mediaType).build();
}

/**
* GETs the RDF Turtle representation of the ORCID record containing only the
Expand All @@ -192,7 +155,7 @@ protected Response xmlToRdf(Response xmlResp, String mediaType) {
@Path(BIO_PATH)
public Response viewBioDetailsTurtle(@PathParam("orcid") String orcid) {
T1_GET_REQUESTS.inc();
return xmlToRdf(serviceDelegator.findBioDetails(orcid), TEXT_TURTLE);
return serviceDelegator.findBioDetails(orcid);
}

/**
Expand Down

This file was deleted.

0 comments on commit 3691fa3

Please sign in to comment.