|
6 | 6 | import java.net.URISyntaxException; |
7 | 7 | import java.net.URLEncoder; |
8 | 8 | import java.nio.charset.StandardCharsets; |
| 9 | +import java.security.InvalidParameterException; |
9 | 10 | import java.sql.Timestamp; |
10 | 11 | import java.text.MessageFormat; |
11 | 12 | import java.time.Instant; |
|
20 | 21 |
|
21 | 22 | import jakarta.ejb.EJB; |
22 | 23 | import jakarta.ejb.Stateless; |
23 | | -import jakarta.ejb.TransactionAttribute; |
24 | | -import jakarta.ejb.TransactionAttributeType; |
25 | 24 | import jakarta.inject.Named; |
26 | 25 | import jakarta.json.Json; |
27 | 26 | import jakarta.json.JsonArray; |
@@ -495,10 +494,19 @@ public void registerExternalTerm(JsonObject cvocEntry, String term, List<Dataset |
495 | 494 | if (evv.getValue() == null) { |
496 | 495 | String adjustedTerm = (prefix==null)? term: term.replace(prefix, ""); |
497 | 496 |
|
498 | | - retrievalUri = replaceRetrievalUriParam(retrievalUri, "0", adjustedTerm); |
499 | | - retrievalUri = replaceRetrievalUriParam(retrievalUri, termUriFieldName, adjustedTerm); |
500 | | - for (DatasetField f : relatedDatasetFields) { |
501 | | - retrievalUri = replaceRetrievalUriParam(retrievalUri, f.getDatasetFieldType().getName(), f.getValue()); |
| 497 | + try { |
| 498 | + retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, "0", adjustedTerm); |
| 499 | + retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, termUriFieldName, adjustedTerm); |
| 500 | + for (DatasetField f : relatedDatasetFields) { |
| 501 | + retrievalUri = tryToReplaceRetrievalUriParam(retrievalUri, f.getDatasetFieldType().getName(), f.getValue()); |
| 502 | + } |
| 503 | + } catch (InvalidParameterException e) { |
| 504 | + logger.warning("InvalidParameterException in tryReplaceRetrievalUriParam : " + e.getMessage()); |
| 505 | + return; |
| 506 | + } |
| 507 | + if (retrievalUri.contains("{")) { |
| 508 | + logger.severe("Retrieval URI still contains unreplaced parameter :" + retrievalUri); |
| 509 | + return; |
502 | 510 | } |
503 | 511 |
|
504 | 512 | logger.fine("Didn't find " + term + ", calling " + retrievalUri); |
@@ -552,24 +560,32 @@ public void process(HttpResponse response, HttpContext context) throws HttpExcep |
552 | 560 | } catch (IOException ioe) { |
553 | 561 | logger.severe("IOException when retrieving url: " + retrievalUri + " : " + ioe.getMessage()); |
554 | 562 | } |
555 | | - |
556 | 563 | } |
557 | 564 | } catch (URISyntaxException e) { |
558 | 565 | logger.fine("Term is not a URI: " + term); |
559 | 566 | } |
560 | | - |
561 | 567 | } |
562 | 568 |
|
563 | | - private String replaceRetrievalUriParam(String retrievalUri, String paramName, String value) { |
| 569 | + private String tryToReplaceRetrievalUriParam(String retrievalUri, String paramName, String value) throws InvalidParameterException { |
564 | 570 |
|
565 | | - if(StringUtils.isBlank(paramName) || StringUtils.isBlank(value)) { |
566 | | - return retrievalUri; |
| 571 | + if(StringUtils.isBlank(paramName)) { |
| 572 | + throw new InvalidParameterException("Empty or null paramName is not allowed while replacing retrieval uri parameter"); |
567 | 573 | } |
568 | 574 |
|
569 | | - if(retrievalUri.contains("encodeUrl:" + paramName)) { |
570 | | - retrievalUri = retrievalUri.replace("{encodeUrl:"+paramName+"}", URLEncoder.encode(value, StandardCharsets.UTF_8)); |
| 575 | + if(retrievalUri.contains(paramName)) { |
| 576 | + logger.fine("Parameter {" + paramName + "} found in retrievalUri"); |
| 577 | + |
| 578 | + if(StringUtils.isBlank(value)) { |
| 579 | + throw new InvalidParameterException("Empty or null value is not allowed while replacing retrieval uri parameter"); |
| 580 | + } |
| 581 | + |
| 582 | + if(retrievalUri.contains("encodeUrl:" + paramName)) { |
| 583 | + retrievalUri = retrievalUri.replace("{encodeUrl:"+paramName+"}", URLEncoder.encode(value, StandardCharsets.UTF_8)); |
| 584 | + } else { |
| 585 | + retrievalUri = retrievalUri.replace("{"+paramName+"}", value); |
| 586 | + } |
571 | 587 | } else { |
572 | | - retrievalUri = retrievalUri.replace("{"+paramName+"}", value); |
| 588 | + logger.fine("Parameter {" + paramName + "} not found in retrievalUri"); |
573 | 589 | } |
574 | 590 |
|
575 | 591 | return retrievalUri; |
|
0 commit comments