Skip to content

Commit

Permalink
CVOC: Fix NPE (#10603)
Browse files Browse the repository at this point in the history
* Fixed NPE

* CVOC - Improved logger message and avoid http call on invalid url
  • Loading branch information
luddaniel authored Jun 21, 2024
1 parent 0f0c3b6 commit b405081
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/main/java/edu/harvard/iq/dataverse/DatasetFieldServiceBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException;
import java.sql.Timestamp;
import java.text.MessageFormat;
import java.time.Instant;
Expand All @@ -20,8 +21,6 @@

import jakarta.ejb.EJB;
import jakarta.ejb.Stateless;
import jakarta.ejb.TransactionAttribute;
import jakarta.ejb.TransactionAttributeType;
import jakarta.inject.Named;
import jakarta.json.Json;
import jakarta.json.JsonArray;
Expand Down Expand Up @@ -523,10 +522,19 @@ public void registerExternalTerm(JsonObject cvocEntry, String term, List<Dataset
if (evv.getValue() == null) {
String adjustedTerm = (prefix==null)? term: term.replace(prefix, "");

retrievalUri = replaceRetrievalUriParam(retrievalUri, "0", adjustedTerm);
retrievalUri = replaceRetrievalUriParam(retrievalUri, termUriFieldName, adjustedTerm);
for (DatasetField f : relatedDatasetFields) {
retrievalUri = replaceRetrievalUriParam(retrievalUri, f.getDatasetFieldType().getName(), f.getValue());
try {
retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, "0", adjustedTerm);
retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, termUriFieldName, adjustedTerm);
for (DatasetField f : relatedDatasetFields) {
retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, f.getDatasetFieldType().getName(), f.getValue());
}
} catch (InvalidParameterException e) {
logger.warning("InvalidParameterException in tryReplaceRetrievalUriParam : " + e.getMessage());
return;
}
if (retrievalUri.contains("{")) {
logger.severe("Retrieval URI still contains unreplaced parameter :" + retrievalUri);
return;
}

logger.fine("Didn't find " + term + ", calling " + retrievalUri);
Expand Down Expand Up @@ -580,20 +588,32 @@ public void process(HttpResponse response, HttpContext context) throws HttpExcep
} catch (IOException ioe) {
logger.severe("IOException when retrieving url: " + retrievalUri + " : " + ioe.getMessage());
}

}
} catch (URISyntaxException e) {
logger.fine("Term is not a URI: " + term);
}

}

private String replaceRetrievalUriParam(String retrievalUri, String paramName, String value) {
private String tryToReplaceRetrievalUriParam(String retrievalUri, String paramName, String value) throws InvalidParameterException {

if(StringUtils.isBlank(paramName)) {
throw new InvalidParameterException("Empty or null paramName is not allowed while replacing retrieval uri parameter");
}

if(retrievalUri.contains(paramName)) {
logger.fine("Parameter {" + paramName + "} found in retrievalUri");

if(StringUtils.isBlank(value)) {
throw new InvalidParameterException("Empty or null value is not allowed while replacing retrieval uri parameter");
}

if(retrievalUri.contains("encodeUrl:" + paramName)) {
retrievalUri = retrievalUri.replace("{encodeUrl:"+paramName+"}", URLEncoder.encode(value, StandardCharsets.UTF_8));
if(retrievalUri.contains("encodeUrl:" + paramName)) {
retrievalUri = retrievalUri.replace("{encodeUrl:"+paramName+"}", URLEncoder.encode(value, StandardCharsets.UTF_8));
} else {
retrievalUri = retrievalUri.replace("{"+paramName+"}", value);
}
} else {
retrievalUri = retrievalUri.replace("{"+paramName+"}", value);
logger.fine("Parameter {" + paramName + "} not found in retrievalUri");
}

return retrievalUri;
Expand Down

0 comments on commit b405081

Please sign in to comment.