Page tree
Contents

The following example requests a new DOI using Java.

The highlighted text indicates text that will need to be modified in order to use the code. A request to update a DOI would be very similar except the update service end point would be called.

 Click to expand code block ...
public static void main(String[] args) throws Exception {
 // Location of the DOI Service
 String DOIServicePoint = " https://services.ands.org.au/doi/";
 // DOI Application ID (supplied by ANDS)
 String AppID = "MY_DOI_Application_ID_(supplied_by_ANDS)";
 String DOIMintServiceSuffix = "1.1/mint.json/?app_id=" + AppID;
 String url = "http://url doi will resolve to.url";
 String datacite_xml_file = "path_to_your.xml";
 String mint_service_url = DOIServicePoint + DOIMintServiceSuffix;
 mint_service_url = mint_service_url + "&url=" + url
 String response_string = postDOIRequest(mint_service_url, loadXMLFromFile(datacite_xml_file));
 System.out.println(response_string);
}
private static String postDOIRequest(String service_url, String contents) throws Exception {
 URL url = new URL(service_url);
 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
 try {
  // Construct data
  String data = "xml=" + URLEncoder.encode(contents, "UTF-8");
  // Send data 
  conn.setRequestMethod("POST");
  conn.setDoOutput(true);
  conn.setDoInput(true);
  OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  wr.write(data);
  wr.flush();
  System.out.println(conn.getResponseCode() + " - " + conn.getResponseMessage());
  // Get the response
  StringBuffer output_buffer = new StringBuffer();
  BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  String line;
  while ((line = rd.readLine()) != null) {
   output_buffer.append(line);
  }
  wr.close();
  rd.close();
  return output_buffer.toString();
 } catch (Exception e) {
  // error occured
  BufferedReader rde = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
  String line;
  while ((line = rde.readLine()) != null) {
   System.out.println(line);
  }
  rde.close();
 } finally {}
 return "";
}
private static String loadXMLFromFile(String filename) throws Exception {
 FileInputStream fis = new FileInputStream(filename);
 InputStreamReader in = new InputStreamReader(fis, "UTF-8");
 Scanner s = null;
 StringBuffer xml_buffer = new StringBuffer();
 try {
  s = new Scanner(new BufferedReader(new FileReader(filename)));
  s.useDelimiter(System.getProperty("line.separator"));
  while (s.hasNext()) {
   xml_buffer.append(s.next() + System.getProperty("line.separator"));
  }
 } finally {
  if (s != null) {
   s.close();
  }
 }
 return xml_buffer.toString();
}
  • No labels