Skip to content

Commit

Permalink
Merge pull request #424 from hbz/DoiRegistration
Browse files Browse the repository at this point in the history
Doi registration
  • Loading branch information
hadoud authored Nov 8, 2023
2 parents 72ecfb6 + c6a1d62 commit 6b12efb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
12 changes: 12 additions & 0 deletions app/actions/Modify.java
Original file line number Diff line number Diff line change
Expand Up @@ -890,22 +890,31 @@ public Map<String, Object> addDoi(Node node) {
}
Map<String, Object> result = new HashMap<>();
String doi = node.getDoi();
play.Logger.debug("doi=" + doi);

if (doi == null || doi.isEmpty()) {
doi = createDoiIdentifier(node);
play.Logger.debug("doi=" + doi);
result.put("Doi", doi);
// node.setDoi(doi);
String objectUrl = Globals.urnbase + node.getPid();
play.Logger.debug("objectUrl=" + objectUrl);

String xml = new Transform().datacite(node, doi);
play.Logger.debug("xml=" + xml);
MyController.validate(xml,
"public/schemas/datacite/kernel-4.1/metadata.xsd",
"https://schema.datacite.org/meta/kernel-4.1/",
"public/schemas/datacite/kernel-4.1/");
try {
DataciteClient client = new DataciteClient();
play.Logger.debug("xml=" + xml);
result.put("Metadata", xml);
String registerMetadataResponse =
client.registerMetadataAtDatacite(node, xml);
play.Logger
.debug("registerMetadataResponse=" + registerMetadataResponse);

result.put("registerMetadataResponse", registerMetadataResponse);
if (client.getStatus() != 200)
throw new RuntimeException("Registering Doi failed!");
Expand All @@ -921,6 +930,7 @@ public Map<String, Object> addDoi(Node node) {
ToScienceObject o = new ToScienceObject();
o.getIsDescribedBy().setDoi(doi);
new Create().patchResource(node, o);
play.Logger.debug("result=" + result.toString());
return result;
} else {
throw new HttpArchiveException(409,
Expand Down Expand Up @@ -976,7 +986,9 @@ public Map<String, Object> updateDoi(Node node) {
private String createDoiIdentifier(Node node) {
String pid = node.getPid();
String id = pid.replace(node.getNamespace() + ":", "");
play.Logger.debug("id=" + id);
String doi = Globals.doiPrefix + "00" + id;
play.Logger.debug("doi=" + doi);
return doi;
}

Expand Down
8 changes: 7 additions & 1 deletion app/controllers/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ public static Promise<Result> asAleph(@PathParam("pid") String pid) {
return ok(mab.render(result));
});
}

@ApiOperation(produces = "application/xml", nickname = "asAlmaNz", value = "asAlmaNz", notes = "Returns an Alma Network Zone xml display of the resource", response = Message.class, httpMethod = "GET")
public static Promise<Result> asAlmaNz(@PathParam("pid") String pid) {
return new ReadMetadataAction().call(pid, node -> {
Expand Down Expand Up @@ -1322,26 +1322,32 @@ private static Result htmlStatusList(List<Map<String, Object>> stati) {
@ApiOperation(produces = "application/json", nickname = "addDoi", value = "addDoi", notes = "Adds a Doi and performes a registration at Datacite", response = String.class, httpMethod = "POST")
public static Promise<Result> addDoi(@PathParam("pid") String pid) {
return new ModifyAction().call(pid, userId -> {
play.Logger.debug("Endpoint addDoi has been called");
Node node = readNodeOrNull(pid);
Map<String, Object> result = modify.addDoi(node);
play.Logger.debug("Resource.ddDoi()" + result.toString());
return JsonMessage(new Message(json(result)));
});
}

@ApiOperation(produces = "application/json", nickname = "updateDoi", value = "updateDoi", notes = "Update the Doi's metadata at Datacite", response = String.class, httpMethod = "POST")
public static Promise<Result> updateDoi(@PathParam("pid") String pid) {
return new ModifyAction().call(pid, userId -> {
play.Logger.debug("Endpoint updateDoi has been called");
Node node = readNodeOrNull(pid);
Map<String, Object> result = modify.updateDoi(node);
play.Logger.debug("Resource.updateDoi()" + result.toString());
return JsonMessage(new Message(json(result)));
});
}

@ApiOperation(produces = "application/json", nickname = "updateDoi", value = "updateDoi", notes = "Update the Doi's metadata at Datacite", response = String.class, httpMethod = "POST")
public static Promise<Result> replaceDoi(@PathParam("pid") String pid) {
return new ModifyAction().call(pid, userId -> {
play.Logger.debug("Endpoint replaceDoi has been called");
Node node = readNodeOrNull(pid);
Map<String, Object> result = modify.replaceDoi(node);
play.Logger.debug("Resource.replaceDoi()" + result.toString());
return JsonMessage(new Message(json(result)));
});
}
Expand Down
25 changes: 18 additions & 7 deletions app/helper/DataciteClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.sun.jersey.client.urlconnection.HTTPSProperties;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.impl.MultiPartWriter;
import com.typesafe.config.ConfigFactory;

/**
* @author Jan Schnasse
Expand All @@ -45,14 +46,16 @@ public class DataciteClient {
Client webclient = null;
boolean testMode = false;
int status = 200;
String user = Globals.dataCiteUser;
String password = Globals.dataCitePasswd;
// URL of Datacite-Repository(Prod | Test)
String dataciteURL = Globals.datCiteUrl;

/**
* The DataciteClient can mint and register dois via the datacite api
*/
public DataciteClient() {
status = 200;
String user = Globals.dataCiteUser;
String password = Globals.dataCitePasswd;
ClientConfig cc = new DefaultClientConfig();
cc.getClasses().add(MultiPartWriter.class);
cc.getClasses().add(FormDataMultiPart.class);
Expand Down Expand Up @@ -103,14 +106,18 @@ public void setTestMode(boolean t) {
*/
public String mintDoiAtDatacite(String doi, String objectUrl) {
try {
play.Logger.debug("mintDoiAtDatacite is called");
play.Logger.debug("objectUrl=" + objectUrl);
status = 200;
String url = testMode ? "https://mds.datacite.org/doi?testMode=true"
: "https://mds.datacite.org/doi";
String url =
testMode ? dataciteURL + "/doi?testMode=true" : dataciteURL + "/doi";
WebResource resource = webclient.resource(url);
play.Logger.debug("resource=" + resource.toString());
String postBody = "doi=" + doi + "\nurl=" + objectUrl + "\n";
play.Logger.info("PostBody:\n" + postBody);
play.Logger.debug("PostBody=" + postBody);
String response =
resource.type("application/xml").post(String.class, postBody);
play.Logger.debug("response" + response);
return response;
} catch (UniformInterfaceException e) {
setStatus(e.getResponse().getStatus());
Expand All @@ -125,9 +132,13 @@ public String mintDoiAtDatacite(String doi, String objectUrl) {
*/
public String registerMetadataAtDatacite(Node node, String xml) {
try {
play.Logger.debug("registerMetadataAtDatacite is called");
play.Logger.debug("dataciteURL=" + dataciteURL);
status = 200;
String url = testMode ? "https://mds.datacite.org/metadata?testMode=true"
: "https://mds.datacite.org/metadata";
String url = testMode ? dataciteURL + "/metadata?testMode=true"
: dataciteURL + "/metadata";

play.Logger.debug("url=" + url);
WebResource resource = webclient.resource(url);
String response = resource.type("application/xml;charset=UTF-8")
.post(String.class, xml);
Expand Down
4 changes: 4 additions & 0 deletions app/models/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ public class Globals {
*/
public static String dataCitePasswd = Play.application().configuration()
.getString("regal-api.datacitePassword");

public static String datCiteUrl =
Play.application().configuration().getString("regal-api.datacite");

/**
* Test Prefix 10.5072 Productive Prefix 10.4126
*/
Expand Down

0 comments on commit 6b12efb

Please sign in to comment.