88
99import java .io .Closeable ;
1010import java .io .IOException ;
11+ import java .util .Base64 ;
1112
1213import java .util .logging .Level ;
1314import java .util .logging .Logger ;
2627import org .apache .http .impl .client .BasicCredentialsProvider ;
2728import org .apache .http .impl .client .CloseableHttpClient ;
2829import org .apache .http .impl .client .HttpClients ;
29-
30-
31-
3230import org .apache .http .util .EntityUtils ;
31+ import edu .harvard .iq .dataverse .util .json .JsonUtil ;
32+ import jakarta .json .JsonObject ;
3333
3434/**
3535 * DataCiteRESTfullClient
@@ -46,6 +46,7 @@ public class DataCiteRESTfullClient implements Closeable {
4646 private static final long RETRY_DELAY_MS = 10000 ; // 10 seconds
4747
4848 private String url ;
49+ private String restApiUrl ;
4950 private CloseableHttpClient httpClient ;
5051 private HttpClientContext context ;
5152 private String encoding = "utf-8" ;
@@ -59,6 +60,17 @@ public DataCiteRESTfullClient(String url, String username, String password) {
5960
6061 httpClient = HttpClients .createDefault ();
6162 }
63+
64+ public DataCiteRESTfullClient (String url , String restApiUrl , String username , String password ) {
65+ this .url = url ;
66+ this .restApiUrl = restApiUrl ;
67+ context = HttpClientContext .create ();
68+ CredentialsProvider credsProvider = new BasicCredentialsProvider ();
69+ credsProvider .setCredentials (new AuthScope (null , -1 ), new UsernamePasswordCredentials (username , password ));
70+ context .setCredentialsProvider (credsProvider );
71+
72+ httpClient = HttpClients .createDefault ();
73+ }
6274
6375 public void close () {
6476 if (this .httpClient != null ) {
@@ -209,6 +221,57 @@ public String getMetadata(String doi) {
209221 }
210222 }
211223
224+ /**
225+ * getMetadataViaRestApi
226+ * a temporary/dev. version of the method utilizing REST API instead of MDS
227+ *
228+ * @param doi
229+ * @return
230+ */
231+ public String getMetadataViaRestApi (String doi ) {
232+ HttpGet httpGet = new HttpGet (this .restApiUrl + "/dois/" + doi );
233+
234+ try {
235+ HttpResponse response = executeWithRetry (httpGet , "getMetadataViaRestApi" );
236+ String restApiRawData = EntityUtils .toString (response .getEntity (), encoding );
237+
238+ logger .fine ("REST API raw data: " + restApiRawData );
239+
240+ if (response .getStatusLine ().getStatusCode () != 200 ) {
241+ String errMsg = "getMetadataViaRestApi, Response: " + response .getStatusLine ().getStatusCode () + ", " + restApiRawData ;
242+ logger .log (Level .SEVERE , errMsg );
243+ throw new RuntimeException (errMsg );
244+ }
245+
246+ JsonObject restApiJson = JsonUtil .getJsonObject (restApiRawData );
247+ String xmlEncoded = null ;
248+
249+ JsonObject restApiJsonData = restApiJson .getJsonObject ("data" );
250+ if (restApiJsonData != null ) {
251+ JsonObject restApiJsonAttributes = restApiJsonData .getJsonObject ("attributes" );
252+ if (restApiJsonAttributes != null ) {
253+ xmlEncoded = restApiJsonAttributes .getString ("xml" );
254+ }
255+ }
256+ logger .fine ("encoded XML entry: " + xmlEncoded );
257+
258+ String metadata = null ; // what we want to return, registration metadata in the XML format
259+
260+ if (xmlEncoded != null ) {
261+ // Stripping any newlines below may be unnecessary - it is likely
262+ // always returned as a continuous string; but shouldn't hurt
263+ // either.
264+ metadata = new String (Base64 .getDecoder ().decode (xmlEncoded .replaceAll ("[\\ r\\ n]" , "" )), encoding );
265+ }
266+
267+ logger .fine ("decoded XML metadata: " + metadata );
268+ return metadata ;
269+ } catch (IOException ioe ) {
270+ logger .log (Level .SEVERE , "IOException in getMetadataViaRestApi" , ioe );
271+ throw new RuntimeException ("IOException in getMetadataViaRestAPi" , ioe );
272+ }
273+ }
274+
212275 /**
213276 * testDOIExists
214277 *
0 commit comments