mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-12 20:43:18 +00:00
[DURACOM-109] Minor fix
This commit is contained in:
@@ -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,33 +120,34 @@ 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;
|
||||||
try (InputStream is = getResponse.getEntity().getContent();
|
try (InputStream is = getResponse.getEntity().getContent();
|
||||||
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
|
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
|
||||||
String inputStr;
|
String inputStr;
|
||||||
// verify if we have basic json
|
// verify if we have basic json
|
||||||
while ((inputStr = streamReader.readLine()) != null && responseObject == null) {
|
while ((inputStr = streamReader.readLine()) != null && responseObject == null) {
|
||||||
if (inputStr.startsWith("{") && inputStr.endsWith("}") && inputStr.contains("access_token")
|
if (inputStr.startsWith("{") && inputStr.endsWith("}") && inputStr.contains("access_token")
|
||||||
&& inputStr.contains("expires_in")) {
|
&& inputStr.contains("expires_in")) {
|
||||||
try {
|
try {
|
||||||
responseObject = new JSONObject(inputStr);
|
responseObject = new JSONObject(inputStr);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Not as valid as I'd hoped, move along
|
// Not as valid as I'd hoped, move along
|
||||||
responseObject = null;
|
responseObject = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
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, " +
|
||||||
throw new IOException("Unable to grab the access token using provided service url, client id and secret");
|
"client id and secret");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new OpenaireRestToken(responseObject.get("access_token").toString(),
|
|
||||||
Long.valueOf(responseObject.get("expires_in").toString()));
|
|
||||||
|
|
||||||
|
return new OpenaireRestToken(responseObject.get("access_token").toString(),
|
||||||
|
Long.valueOf(responseObject.get("expires_in").toString()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,42 +172,43 @@ 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();
|
||||||
|
|
||||||
// registering errors
|
// registering errors
|
||||||
switch (status.getStatusCode()) {
|
switch (status.getStatusCode()) {
|
||||||
case HttpStatus.SC_NOT_FOUND:
|
case HttpStatus.SC_NOT_FOUND:
|
||||||
// 404 - Not found
|
// 404 - Not found
|
||||||
case HttpStatus.SC_FORBIDDEN:
|
case HttpStatus.SC_FORBIDDEN:
|
||||||
// 403 - Invalid Access Token
|
// 403 - Invalid Access Token
|
||||||
case 429:
|
case 429:
|
||||||
// 429 - Rate limit abuse for unauthenticated user
|
// 429 - Rate limit abuse for unauthenticated user
|
||||||
Header[] limitUsed = getResponse.getHeaders("x-ratelimit-used");
|
Header[] limitUsed = getResponse.getHeaders("x-ratelimit-used");
|
||||||
Header[] limitMax = getResponse.getHeaders("x-ratelimit-limit");
|
Header[] limitMax = getResponse.getHeaders("x-ratelimit-limit");
|
||||||
|
|
||||||
if (limitUsed.length > 0) {
|
if (limitUsed.length > 0) {
|
||||||
String limitMsg = limitUsed[0].getValue();
|
String limitMsg = limitUsed[0].getValue();
|
||||||
if (limitMax.length > 0) {
|
if (limitMax.length > 0) {
|
||||||
limitMsg = limitMsg.concat(" of " + limitMax[0].getValue());
|
limitMsg = limitMsg.concat(" of " + limitMax[0].getValue());
|
||||||
|
}
|
||||||
|
getGotError(new NoHttpResponseException(status.getReasonPhrase() + " with usage limit "
|
||||||
|
+ limitMsg),
|
||||||
|
url + '/' + file);
|
||||||
|
} else {
|
||||||
|
// 429 - Rate limit abuse
|
||||||
|
getGotError(new NoHttpResponseException(status.getReasonPhrase()), url + '/' + file);
|
||||||
}
|
}
|
||||||
getGotError(
|
break;
|
||||||
new NoHttpResponseException(status.getReasonPhrase() + " with usage limit " + limitMsg),
|
default:
|
||||||
url + '/' + file);
|
// 200 or other
|
||||||
} else {
|
break;
|
||||||
// 429 - Rate limit abuse
|
}
|
||||||
getGotError(new NoHttpResponseException(status.getReasonPhrase()), url + '/' + file);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// 200 or other
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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) {
|
||||||
|
@@ -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,26 +87,27 @@ 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;
|
||||||
try (InputStream is = getResponse.getEntity().getContent();
|
try (InputStream is = getResponse.getEntity().getContent();
|
||||||
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
|
BufferedReader streamReader = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
|
||||||
String inputStr;
|
String inputStr;
|
||||||
while ((inputStr = streamReader.readLine()) != null && responseObject == null) {
|
while ((inputStr = streamReader.readLine()) != null && responseObject == null) {
|
||||||
if (inputStr.startsWith("{") && inputStr.endsWith("}") && inputStr.contains("access_token")) {
|
if (inputStr.startsWith("{") && inputStr.endsWith("}") && inputStr.contains("access_token")) {
|
||||||
try {
|
try {
|
||||||
responseObject = new JSONObject(inputStr);
|
responseObject = new JSONObject(inputStr);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//Not as valid as I'd hoped, move along
|
//Not as valid as I'd hoped, move along
|
||||||
responseObject = null;
|
responseObject = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if (responseObject != null && responseObject.has("access_token")) {
|
||||||
if (responseObject != null && responseObject.has("access_token")) {
|
accessToken = (String) responseObject.get("access_token");
|
||||||
accessToken = (String) responseObject.get("access_token");
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user