diff --git a/orcid-api-common/pom.xml b/orcid-api-common/pom.xml index 3a2e1e48399..8b16c55ae43 100644 --- a/orcid-api-common/pom.xml +++ b/orcid-api-common/pom.xml @@ -89,6 +89,11 @@ test + + org.apache.jena + jena-core + 2.10.1 + orcid-api-common diff --git a/orcid-api-common/src/main/java/org/orcid/api/common/writer/RDFMessageBodyWriter.java b/orcid-api-common/src/main/java/org/orcid/api/common/writer/RDFMessageBodyWriter.java index 2d0b31e1032..aa660752645 100644 --- a/orcid-api-common/src/main/java/org/orcid/api/common/writer/RDFMessageBodyWriter.java +++ b/orcid-api-common/src/main/java/org/orcid/api/common/writer/RDFMessageBodyWriter.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.OutputStream; +import java.io.StringWriter; import java.lang.annotation.Annotation; import java.lang.reflect.Type; @@ -27,10 +28,18 @@ 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 @@ -41,6 +50,8 @@ @Provider @Produces( { APPLICATION_RDFXML }) public class RDFMessageBodyWriter implements MessageBodyWriter { + + private static final String FOAF_0_1 = "http://xmlns.com/foaf/0.1/"; /** * Ascertain if the MessageBodyWriter supports a particular type. @@ -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 httpHeaders, + public void writeTo(OrcidMessage xml, Class type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap 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); } } diff --git a/orcid-pub-web/pom.xml b/orcid-pub-web/pom.xml index 17d2fc06f75..46af24944b0 100644 --- a/orcid-pub-web/pom.xml +++ b/orcid-pub-web/pom.xml @@ -71,11 +71,6 @@ ${project.parent.version} test - - org.apache.jena - jena-core - 2.10.1 - diff --git a/orcid-pub-web/src/main/java/org/orcid/api/t1/server/T1OrcidApiServiceImpl.java b/orcid-pub-web/src/main/java/org/orcid/api/t1/server/T1OrcidApiServiceImpl.java index 2e4e5bca38e..df69aa1969f 100644 --- a/orcid-pub-web/src/main/java/org/orcid/api/t1/server/T1OrcidApiServiceImpl.java +++ b/orcid-pub-web/src/main/java/org/orcid/api/t1/server/T1OrcidApiServiceImpl.java @@ -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; @@ -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; @@ -69,7 +60,6 @@ @Path("/") public class T1OrcidApiServiceImpl implements OrcidApiService { - 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"); @@ -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 @@ -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); } /** diff --git a/orcid-pub-web/src/test/java/org/orcid/api/t1/server/T1OrcidApiServiceImplRDFTest.java b/orcid-pub-web/src/test/java/org/orcid/api/t1/server/T1OrcidApiServiceImplRDFTest.java deleted file mode 100644 index 4a5905b49a1..00000000000 --- a/orcid-pub-web/src/test/java/org/orcid/api/t1/server/T1OrcidApiServiceImplRDFTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/** - * ============================================================================= - * - * ORCID (R) Open Source - * http://orcid.org - * - * Copyright (c) 2012-2013 ORCID, Inc. - * Licensed under an MIT-Style License (MIT) - * http://orcid.org/open-source-license - * - * This copyright and license information (including a link to the full license) - * shall be included in its entirety in all copies or substantial portion of - * the software. - * - * ============================================================================= - */ -package org.orcid.api.t1.server; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -import static org.orcid.api.common.OrcidApiConstants.*; - -import java.util.Arrays; - -import javax.annotation.Resource; -import javax.ws.rs.core.Response; - -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.orcid.api.common.OrcidApiService; -import org.orcid.api.common.delegator.OrcidApiServiceDelegator; -import org.orcid.jaxb.model.message.ContactDetails; -import org.orcid.jaxb.model.message.CreditName; -import org.orcid.jaxb.model.message.Email; -import org.orcid.jaxb.model.message.FamilyName; -import org.orcid.jaxb.model.message.GivenNames; -import org.orcid.jaxb.model.message.OrcidBio; -import org.orcid.jaxb.model.message.OrcidMessage; -import org.orcid.jaxb.model.message.OrcidProfile; -import org.orcid.jaxb.model.message.OtherNames; -import org.orcid.jaxb.model.message.PersonalDetails; - -public class T1OrcidApiServiceImplRDFTest { - - @Resource - private OrcidApiService t1OrcidApiService = new T1OrcidApiServiceImpl(); - - - private final Response successResponse = Response.ok().build(); - - private OrcidApiServiceDelegator mockServiceDelegator; - - @Before - public void subvertDelegator() { - mockServiceDelegator = mock(OrcidApiServiceDelegator.class); - // view status is always fine - when(mockServiceDelegator.viewStatusText()).thenReturn(successResponse); - ((T1OrcidApiServiceImpl)t1OrcidApiService).setServiceDelegator(mockServiceDelegator); - } - - private Response fakeBio() { - OrcidMessage orcidMessage = new OrcidMessage(); - OrcidProfile orcidProfile1 = new OrcidProfile(); - orcidProfile1.setOrcid("http://orcid.example.com/000-1337"); - orcidProfile1.setOrcidId("000-1337"); - OrcidBio bio = new OrcidBio(); - orcidProfile1.setOrcidBio(bio); - PersonalDetails personal = new PersonalDetails(); - bio.setPersonalDetails(personal); - personal.setFamilyName(new FamilyName("Doe")); - personal.setCreditName(new CreditName("John F Doe")); - personal.setGivenNames(new GivenNames("John F")); - personal.setOtherNames(new OtherNames()); - personal.getOtherNames().addOtherName("Johnny"); - personal.getOtherNames().addOtherName("Mr Doe"); - - bio.setContactDetails(new ContactDetails()); - bio.getContactDetails().setEmail(Arrays.asList(new Email("john@example.org"), new Email("doe@example.com"))); - - - orcidMessage.setOrcidProfile(orcidProfile1); - return Response.ok(orcidMessage).build(); - - } - - @After - public void resetVals() { - T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.clear(); - T1OrcidApiServiceImpl.T1_GET_REQUESTS.clear(); - T1OrcidApiServiceImpl.T1_SEARCH_RESULTS_NONE_FOUND.clear(); - T1OrcidApiServiceImpl.T1_SEARCH_RESULTS_FOUND.clear(); - } - - @Test - public void testViewBioDetailsRdf() { - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 0); - when(mockServiceDelegator.findBioDetails(any(String.class))).thenReturn(fakeBio()); - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - Response response = t1OrcidApiService.viewBioDetailsRdf("orcid"); - assertEquals(200, response.getStatus()); -// assertEquals(APPLICATION_RDFXML, response.getMetadata().g) - String str = response.getEntity().toString(); - System.out.println(str); - assertTrue(str.contains("http://orcid.example.com/000-1337")); - assertTrue(str.contains("foaf:name>John")); - assertTrue(str.contains("rdf:about")); - assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 1); - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - - } - - /* - @Test - public void testViewExternalIdentifiersRdf() { - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 0); - when(mockServiceDelegator.findExternalIdentifiers(any(String.class))).thenReturn(successResponse); - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - Response response = t1OrcidApiService.viewExternalIdentifiersRdf("orcid"); - assertEquals(200, response.getStatus()); - assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 1); - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - } - - @Test - public void testViewFullDetailsRdf() { - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 0); - when(mockServiceDelegator.findFullDetails(any(String.class))).thenReturn(successResponse); - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - Response response = t1OrcidApiService.viewFullDetailsRdf("orcid"); - assertEquals(200, response.getStatus()); - assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 1); - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - } - - @Test - public void testViewWorksDetailsRdf() { - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 0); - when(mockServiceDelegator.findWorksDetails(any(String.class))).thenReturn(successResponse); - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - Response response = t1OrcidApiService.viewWorksDetailsRdf("orcid"); - assertEquals(200, response.getStatus()); - assertTrue(T1OrcidApiServiceImpl.T1_GET_REQUESTS.count() == 1); - assertTrue(T1OrcidApiServiceImpl.T1_SEARCH_REQUESTS.count() == 0); - } - */ -}