Skip to content

Commit

Permalink
select the first reachable icon
Browse files Browse the repository at this point in the history
  • Loading branch information
utas-raymondng committed May 22, 2024
1 parent 8fef893 commit b27917d
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package au.org.aodn.esindexer.configuration;

import au.org.aodn.esindexer.service.GeoNetworkServiceImpl;
import au.org.aodn.esindexer.utils.UrlUtils;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
Expand Down Expand Up @@ -52,4 +53,10 @@ public GeoNetworkServiceImpl createGeoNetworkServiceImpl(

return new GeoNetworkServiceImpl(server, indexName, gn4ElasticsearchClient, indexerRestTemplate);
}

@Bean
@ConditionalOnMissingBean(UrlUtils.class)
public UrlUtils createUrlUtils() {
return new UrlUtils();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import au.org.aodn.esindexer.exception.MappingValueException;
import au.org.aodn.esindexer.utils.GeometryUtils;
import au.org.aodn.esindexer.configuration.AppConstants;
import au.org.aodn.esindexer.utils.UrlUtils;
import au.org.aodn.stac.model.*;
import au.org.aodn.esindexer.utils.BBoxUtils;

Expand All @@ -25,7 +26,6 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -69,6 +69,9 @@ public abstract class StacCollectionMapperService {
@Autowired
private GeoNetworkService geoNetworkService;

@Autowired
protected UrlUtils urlUtils;

@Named("mapUUID")
String mapUUID(MDMetadataType source) {
return source.getMetadataIdentifier().getMDIdentifier().getCode().getCharacterString().getValue().toString();
Expand Down Expand Up @@ -434,18 +437,19 @@ List<LinkModel> mapLinks(MDMetadataType source) {
Map<String, Object> additionalInfo = optAdditionalInfo.get();
if(additionalInfo.containsKey(SUGGEST_LOGOS)) {
if(additionalInfo.get(SUGGEST_LOGOS) instanceof List) {
final AtomicInteger index = new AtomicInteger(1);
((List<?>) additionalInfo.get(SUGGEST_LOGOS))
.stream()
.map(p -> (p instanceof String) ? (String) p : null)
.filter(Objects::nonNull)
.forEach(i -> {
.filter(i -> urlUtils.checkUrlExists(i))
.findFirst() // We only pick the first reachable one
.ifPresent(i -> {
LinkModel linkModel = LinkModel.builder().build();
linkModel.setHref(i);
// Geonetwork always return png logo
linkModel.setType("image/png");
linkModel.setRel("icon");
linkModel.setTitle("Suggest icon for dataset " + index.getAndIncrement());
linkModel.setTitle("Suggest icon for dataset");
results.add(linkModel);
});
}
Expand Down
23 changes: 23 additions & 0 deletions indexer/src/main/java/au/org/aodn/esindexer/utils/UrlUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package au.org.aodn.esindexer.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class UrlUtils {

@Autowired
protected RestTemplate restTemplate;

public boolean checkUrlExists(String url) {
try {
ResponseEntity<Void> response = restTemplate.exchange(url, HttpMethod.HEAD, null, Void.class);
return response.getStatusCode() == HttpStatus.OK;
}
catch (Exception e) {
return false;
}
}
}
2 changes: 1 addition & 1 deletion indexer/src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ elasticsearch:
apiKey: sample-api-key

geonetwork:
host: https://geonetwork-edge.mvp.aodn.org.au
host: https://geonetwork-edge.edge.aodn.org.au
search:
api:
index: "records"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

import au.org.aodn.esindexer.BaseTestClass;
import au.org.aodn.esindexer.service.GeoNetworkServiceImpl;
import au.org.aodn.esindexer.utils.UrlUtils;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.*;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
Expand All @@ -16,9 +17,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.testcontainers.containers.DockerComposeContainer;
import org.testcontainers.containers.wait.strategy.Wait;
Expand All @@ -28,6 +26,7 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -126,4 +125,24 @@ public GeoNetworkServiceImpl createGeoNetworkServiceImpl(

return impl;
}
/**
* Hardcode the result here for testing, please add more if you need to return different result
* @return
*/
@Bean
public UrlUtils createUrlUtils() {
UrlUtils urlUtils = Mockito.mock(UrlUtils.class);

final Map<String, Boolean> status = new HashMap<>();
status.put("https://catalogue-imos.aodn.org.au/geonetwork/images/logos/dbee258b-8730-4072-96d4-2818a69a4afd.png", Boolean.TRUE);

doAnswer(answer -> {
String url = answer.getArgument(0);
return status.getOrDefault(url, Boolean.FALSE);
})
.when(urlUtils)
.checkUrlExists(anyString());

return urlUtils;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void verifyLogoLinkAddedOnIndex() throws IOException {
Hit<ObjectNode> objectNodeHit = indexerService.getDocumentByUUID("2852a776-cbfc-4bc8-a126-f3c036814892");

String test = objectNodeHit.source().toPrettyString();
logger.info(test);
assertEquals("Stac equals", indexerObjectMapper.readTree(expected), indexerObjectMapper.readTree(test));

deleteRecord("2852a776-cbfc-4bc8-a126-f3c036814892");
Expand Down
13 changes: 4 additions & 9 deletions indexer/src/test/resources/canned/sample5_stac.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"temporal" : [ [ "2010-01-21T01:00:00Z", "2017-03-26T13:00:00Z" ], [ "2010-01-21T01:00:00Z", "2017-03-26T13:00:00Z" ] ]
},
"summaries" : {
"score" : 90,
"score" : 85,
"status" : "completed",
"scope" : {
"code" : "series",
Expand Down Expand Up @@ -58,16 +58,11 @@
"rel" : "self",
"type" : "text/html",
"title" : "Ocean Radar page on IMOS website"
}, {
"href" : "http://localhost:8080/geonetwork/images/logos/dbee258b-8730-4072-96d4-2818a69a4afd.png",
"rel" : "icon",
"type" : "image/png",
"title" : "Suggest icon for dataset 1"
}, {
"href" : "https://catalogue-imos.aodn.org.au/geonetwork/images/logos/dbee258b-8730-4072-96d4-2818a69a4afd.png",
"rel" : "icon",
"type" : "image/png",
"title" : "Suggest icon for dataset 2"
"title" : "Suggest icon for dataset"
} ],
"license" : "Creative Commons Attribution 4.0 International License",
"providers" : [ {
Expand Down Expand Up @@ -122,6 +117,6 @@
"title" : "IMOS - ACORN - Bonney Coast HF ocean radar site (South Australia, Australia)"
},
"type" : "Collection",
"stac_extensions" : [ "https://stac-extensions.github.io/scientific/v1.0.0/schema.json", "https://stac-extensions.github.io/contacts/v0.1.1/schema.json", "https://stac-extensions.github.io/projection/v1.1.0/schema.json", "https://stac-extensions.github.io/language/v1.0.0/schema.json", "https://stac-extensions.github.io/themes/v1.0.0/schema.json" ],
"stac_version" : "1.0.0"
"stac_version" : "1.0.0",
"stac_extensions" : [ "https://stac-extensions.github.io/scientific/v1.0.0/schema.json", "https://stac-extensions.github.io/contacts/v0.1.1/schema.json", "https://stac-extensions.github.io/projection/v1.1.0/schema.json", "https://stac-extensions.github.io/language/v1.0.0/schema.json", "https://stac-extensions.github.io/themes/v1.0.0/schema.json" ]
}

0 comments on commit b27917d

Please sign in to comment.