[DURACOM-109] Minor fix

This commit is contained in:
Elios Buzo
2025-04-23 17:43:49 +02:00
parent c553910750
commit 4dfbbec06f
2 changed files with 74 additions and 71 deletions

View File

@@ -27,10 +27,10 @@ import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair; import org.apache.http.NameValuePair;
import org.apache.http.NoHttpResponseException; import org.apache.http.NoHttpResponseException;
import org.apache.http.StatusLine; import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.client.DSpaceHttpClientFactory; import org.dspace.app.client.DSpaceHttpClientFactory;
@@ -120,7 +120,7 @@ public class OpenaireRestConnector {
params.add(new BasicNameValuePair("grant_type", "client_credentials")); params.add(new BasicNameValuePair("grant_type", "client_credentials"));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8")); httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpClient httpClient = DSpaceHttpClientFactory.getInstance().build(); try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
HttpResponse getResponse = httpClient.execute(httpPost); HttpResponse getResponse = httpClient.execute(httpPost);
JSONObject responseObject = null; JSONObject responseObject = null;
@@ -141,12 +141,13 @@ public class OpenaireRestConnector {
} }
} }
if (responseObject == null || !responseObject.has("access_token") || !responseObject.has("expires_in")) { if (responseObject == null || !responseObject.has("access_token") || !responseObject.has("expires_in")) {
throw new IOException("Unable to grab the access token using provided service url, client id and secret"); throw new IOException("Unable to grab the access token using provided service url, " +
"client id and secret");
} }
return new OpenaireRestToken(responseObject.get("access_token").toString(), return new OpenaireRestToken(responseObject.get("access_token").toString(),
Long.valueOf(responseObject.get("expires_in").toString())); Long.valueOf(responseObject.get("expires_in").toString()));
}
} }
/** /**
@@ -171,7 +172,7 @@ public class OpenaireRestConnector {
httpGet.addHeader("Authorization", "Bearer " + accessToken); httpGet.addHeader("Authorization", "Bearer " + accessToken);
} }
HttpClient httpClient = DSpaceHttpClientFactory.getInstance().build(); try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
getResponse = httpClient.execute(httpGet); getResponse = httpClient.execute(httpGet);
StatusLine status = getResponse.getStatusLine(); StatusLine status = getResponse.getStatusLine();
@@ -192,8 +193,8 @@ public class OpenaireRestConnector {
if (limitMax.length > 0) { if (limitMax.length > 0) {
limitMsg = limitMsg.concat(" of " + limitMax[0].getValue()); limitMsg = limitMsg.concat(" of " + limitMax[0].getValue());
} }
getGotError( getGotError(new NoHttpResponseException(status.getReasonPhrase() + " with usage limit "
new NoHttpResponseException(status.getReasonPhrase() + " with usage limit " + limitMsg), + limitMsg),
url + '/' + file); url + '/' + file);
} else { } else {
// 429 - Rate limit abuse // 429 - Rate limit abuse
@@ -207,6 +208,7 @@ public class OpenaireRestConnector {
// do not close this httpClient // do not close this httpClient
result = getResponse.getEntity().getContent(); result = getResponse.getEntity().getContent();
}
} catch (MalformedURLException e1) { } catch (MalformedURLException e1) {
getGotError(e1, url + '/' + file); getGotError(e1, url + '/' + file);
} catch (Exception e) { } catch (Exception e) {

View File

@@ -22,8 +22,8 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.dspace.app.client.DSpaceHttpClientFactory; import org.dspace.app.client.DSpaceHttpClientFactory;
@@ -87,7 +87,7 @@ public class OrcidV3AuthorDataProvider extends AbstractExternalDataProvider {
httpPost.addHeader("Accept", "application/json"); httpPost.addHeader("Accept", "application/json");
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpClient httpClient = DSpaceHttpClientFactory.getInstance().build(); try (CloseableHttpClient httpClient = DSpaceHttpClientFactory.getInstance().build()) {
HttpResponse getResponse = httpClient.execute(httpPost); HttpResponse getResponse = httpClient.execute(httpPost);
JSONObject responseObject = null; JSONObject responseObject = null;
@@ -110,6 +110,7 @@ public class OrcidV3AuthorDataProvider extends AbstractExternalDataProvider {
} }
} }
} }
}
@Override @Override
public Optional<ExternalDataObject> getExternalDataObject(String id) { public Optional<ExternalDataObject> getExternalDataObject(String id) {