diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java index f61636862c3..adcaf16a68a 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Datasets.java @@ -1,5 +1,6 @@ package edu.harvard.iq.dataverse.api; +import edu.harvard.iq.dataverse.BibtexCitation; import edu.harvard.iq.dataverse.DOIEZIdServiceBean; import edu.harvard.iq.dataverse.Dataset; import edu.harvard.iq.dataverse.DatasetField; @@ -8,10 +9,13 @@ import edu.harvard.iq.dataverse.DatasetVersion; import edu.harvard.iq.dataverse.Dataverse; import edu.harvard.iq.dataverse.DataverseServiceBean; +import edu.harvard.iq.dataverse.DataverseSession; import edu.harvard.iq.dataverse.MetadataBlock; import static edu.harvard.iq.dataverse.api.AbstractApiBean.errorResponse; import edu.harvard.iq.dataverse.authorization.DataverseRole; import edu.harvard.iq.dataverse.authorization.RoleAssignee; +import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; +import edu.harvard.iq.dataverse.authorization.users.GuestUser; import edu.harvard.iq.dataverse.authorization.users.User; import edu.harvard.iq.dataverse.engine.command.Command; import edu.harvard.iq.dataverse.engine.command.DataverseRequest; @@ -43,6 +47,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.EJB; +import javax.inject.Inject; import javax.json.Json; import javax.json.JsonArrayBuilder; import javax.json.JsonObject; @@ -80,6 +85,9 @@ public class Datasets extends AbstractApiBean { @EJB SystemConfig systemConfig; + @Inject + DataverseSession session; + /** * Used to consolidate the way we parse and handle dataset versions. * @param @@ -232,7 +240,55 @@ public Response getVersionMetadata( @PathParam("id") String datasetId, @PathPara return ex.getResponse(); } } - + + @GET + @Path("{id}/versions/{versionId}/citation") + public Response getVersionCitation(@PathParam("id") String datasetId, @PathParam("versionId") String versionId) { + try { + User user = findUserOrDie(); + /** + * @todo Refactor all this session handling that was lifted + * wholesale from Access.java into AbstractApiBean.java so that it + * can be used from any API endpoint. + */ + if (session != null) { + if (session.getUser() != null) { + if (session.getUser().isAuthenticated()) { + user = (AuthenticatedUser) session.getUser(); + } else { + LOGGER.fine("User associated with the session is not an authenticated user. (Guest access will be assumed)."); + if (session.getUser() instanceof GuestUser) { + LOGGER.fine("User associated with the session is indeed a guest user."); + } + } + } else { + LOGGER.fine("No user associated with the session."); + } + } else { + LOGGER.fine("Session is null."); + } + DatasetVersion datasetVersion = getDatasetVersionOrDie(createDataverseRequest(user), versionId, + findDatasetOrDie(datasetId)); + /** + * @todo Support all four formats rather than just BibTex: + * + * - Dataverse citation + * + * - EndNote XML + * + * - RIS + * + * - BibTex + */ + BibtexCitation bibtex = new BibtexCitation(datasetVersion); + return Response.ok().entity(bibtex.toString()) + .type(MediaType.TEXT_PLAIN) + .build(); + } catch (WrappedResponse ex) { + return ex.getResponse(); + } + } + @GET @Path("{id}/versions/{versionNumber}/metadata/{block}") public Response getVersionMetadataBlock( @PathParam("id") String datasetId, diff --git a/src/main/webapp/dataset-citation.xhtml b/src/main/webapp/dataset-citation.xhtml index c2ad5c0a7ec..a9111dc88b0 100644 --- a/src/main/webapp/dataset-citation.xhtml +++ b/src/main/webapp/dataset-citation.xhtml @@ -39,6 +39,20 @@ #{bundle['dataset.cite.downloadBtn.bib']} +
  • + + + #{bundle['dataset.cite.downloadBtn.bib']} API + +
  • diff --git a/src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java b/src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java index 7af230e1275..1557c8285d9 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java @@ -7,7 +7,11 @@ import org.junit.Test; import org.junit.AfterClass; import static com.jayway.restassured.RestAssured.given; +import com.jayway.restassured.http.ContentType; +import static javax.ws.rs.core.Response.Status.CREATED; +import static javax.ws.rs.core.Response.Status.OK; import static junit.framework.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class DatasetsIT { @@ -40,6 +44,49 @@ public void testCreateDataset() { } + @Test + public void testCitations() { + Response createUser = UtilIT.createRandomUser(); + + String username = UtilIT.getUsernameFromResponse(createUser); + String apiToken = UtilIT.getApiTokenFromResponse(createUser); + + Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken); + createDataverseResponse.prettyPrint(); + String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); + assertEquals(CREATED.getStatusCode(), createDataverseResponse.getStatusCode()); + Response createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apiToken); + createDataset.prettyPrint(); + createDataset.then().assertThat() + .statusCode(CREATED.getStatusCode()); + Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset); + + Response citation1 = UtilIT.getCitation(datasetId, apiToken); + citation1.prettyPrint(); + citation1.then().assertThat() + .statusCode(OK.getStatusCode()) + .contentType(ContentType.TEXT); + /** + * @todo Look into "Expected response body to be verified as JSON, HTML + * or XML but content-type 'text/plain' is not supported out of the + * box." + */ + assertTrue(citation1.body().asString().contains("Darwin's Finches")); + + UtilIT.deleteDatasetViaNativeApi(datasetId, apiToken).then().assertThat() + .statusCode(OK.getStatusCode()); + + UtilIT.deleteDataverse(dataverseAlias, apiToken).then().assertThat() + .statusCode(OK.getStatusCode()); + + UtilIT.makeSuperUser(username).then().assertThat() + .statusCode(OK.getStatusCode()); + + UtilIT.deleteUser(username).then().assertThat() + .statusCode(OK.getStatusCode()); + + } + @Test public void testGetDdi() { String persistentIdentifier = "FIXME"; diff --git a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java index 6d7fc96561e..6c99a5fc140 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java @@ -495,4 +495,12 @@ public void testSwordStatementWithFiles() { assertEquals("A Dataset with a File", title); } + static Response getCitation(Integer datasetId, String apiToken) { + Response response = given() + .header(API_TOKEN_HTTP_HEADER, apiToken) + .urlEncodingEnabled(false) + .get("/api/datasets/" + datasetId + "/versions/:draft/citation"); + return response; + } + }