Skip to content

Commit

Permalink
Citation API: initial investigation see todo's #3140
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed May 26, 2016
1 parent 7e8005c commit c433395
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 <T>
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions src/main/webapp/dataset-citation.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@
#{bundle['dataset.cite.downloadBtn.bib']}
</a>
</li>
<li>
<!--
TODO: Don't hard code ":latest" here.
Support :draft, :latest-published, x.y, as well from
http://guides.dataverse.org/en/latest/api/native-api.html#datasets
We can only assume this will be worked on as part of
https://github.com/IQSS/dataverse/issues/2608 .
Hopefully it's clear enough that this is just a stub for BibTex
and that the other formats above need to be supported as well.
-->
<a href="/api/datasets/#{DatasetPage.dataset.id}/versions/:latest/citation">
#{bundle['dataset.cite.downloadBtn.bib']} API
</a>
</li>
</ul>
</div>
<div id="citation-standards" class="col-xs-12 text-muted small text-right"><span class="glyphicon glyphicon-info-sign"></span> <h:outputText value=" #{bundle['dataset.cite.standards.tip']}" escape="false"/></div>
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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";
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit c433395

Please sign in to comment.