Skip to content

Commit

Permalink
Merge pull request #163 from aodn/bugs/5964-fix-text-format
Browse files Browse the repository at this point in the history
Fix issue with char encoding
  • Loading branch information
HavierD authored Oct 30, 2024
2 parents 9ca28d4 + 1f1f55b commit 72582ac
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import au.org.aodn.esindexer.exception.MetadataNotFoundException;
import au.org.aodn.esindexer.model.RelationType;
import au.org.aodn.esindexer.utils.CommonUtils;
import au.org.aodn.esindexer.utils.StringUtil;
import au.org.aodn.esindexer.configuration.AppConstants;
import au.org.aodn.esindexer.utils.UrlUtils;
Expand Down Expand Up @@ -35,6 +36,7 @@
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -72,7 +74,9 @@ protected HttpEntity<String> getRequestEntity(String body) {

protected HttpEntity<String> getRequestEntity(MediaType accept, String body) {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(accept == null ? MediaType.APPLICATION_XML : accept));
headers.setAccept(Collections.singletonList(accept == null ? CommonUtils.MEDIA_UTF8_XML : accept));
headers.setContentType(CommonUtils.MEDIA_UTF8_XML);

return body == null ? new HttpEntity<>(headers) : new HttpEntity<>(body, headers);
}

Expand Down Expand Up @@ -357,7 +361,7 @@ public String searchRecordBy(String uuid) throws HttpServerErrorException.Servic
params);

if (responseEntity.getStatusCode().is2xxSuccessful()) {
return StringUtil.encodeUTF8(Objects.requireNonNull(responseEntity.getBody()));
return Objects.requireNonNull(responseEntity.getBody());
}
else {
throw new RuntimeException("Failed to fetch data from the API");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package au.org.aodn.esindexer.utils;

import au.org.aodn.metadata.iso19115_3_2018.*;
import org.springframework.http.MediaType;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

public class CommonUtils {

public static MediaType MEDIA_UTF8_XML = new MediaType("application", "xml", StandardCharsets.UTF_8);

public static <T> Optional<T> safeGet(Supplier<T> supplier) {
try {
return Optional.of(supplier.get());
Expand Down
14 changes: 7 additions & 7 deletions indexer/src/main/java/au/org/aodn/esindexer/utils/JaxbUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package au.org.aodn.esindexer.utils;

import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import jakarta.xml.bind.*;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
Expand All @@ -19,9 +17,11 @@ public JaxbUtils(Class<T> clazz) throws JAXBException {

@SuppressWarnings("unchecked")
public T unmarshal(String input) throws JAXBException {
Source source = new StreamSource(new StringReader(input));
synchronized (jaxbUnmarshaller) {
return ((JAXBElement<T>)jaxbUnmarshaller.unmarshal(source)).getValue();
try(StringReader reader = new StringReader(input)) {
Source source = new StreamSource(reader);
synchronized (jaxbUnmarshaller) {
return ((JAXBElement<T>) jaxbUnmarshaller.unmarshal(source)).getValue();
}
}
}
}
27 changes: 16 additions & 11 deletions indexer/src/test/java/au/org/aodn/esindexer/BaseTestClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import au.org.aodn.esindexer.configuration.GeoNetworkSearchTestConfig;
import au.org.aodn.esindexer.service.VocabServiceImpl;
import au.org.aodn.esindexer.utils.CommonUtils;
import au.org.aodn.esindexer.utils.VocabsIndexUtils;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
Expand All @@ -21,6 +22,7 @@
import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -84,7 +86,7 @@ protected HttpEntity<String> getRequestEntity(Optional<Map<String, String>> oh,
HttpHeaders headers = new HttpHeaders();

headers.setAccept(List.of(MediaType.APPLICATION_JSON, MediaType.ALL, MediaType.TEXT_PLAIN));
headers.setContentType(contentType == null ? MediaType.APPLICATION_XML : contentType);
headers.setContentType(contentType == null ? CommonUtils.MEDIA_UTF8_XML : contentType);
headers.setCacheControl(CacheControl.empty());

headers.add(HttpHeaders.USER_AGENT, "TestRestTemplate");
Expand Down Expand Up @@ -260,21 +262,24 @@ private boolean delete(String uuid, HttpEntity<String> requestEntity) {

public static String readResourceFile(String path) throws IOException {
File f = ResourceUtils.getFile(path);
return new String(Files.readAllBytes(f.toPath()));
return Files.readString(f.toPath(), StandardCharsets.UTF_8);
}

public String insertMetadataRecords(String uuid, String path) throws RestClientException, IOException {
String content = readResourceFile(path);

HttpEntity<String> requestEntity = getRequestEntity(Optional.empty(), null, content);

ResponseEntity<Map> r = testRestTemplate
.exchange(
getGeoNetworkRecordsInsertUrl(),
HttpMethod.PUT,
requestEntity,
Map.class
);
HttpEntity<String> requestEntity = getRequestEntity(
Optional.empty(),
null,
content
);

ResponseEntity<Map> r = testRestTemplate.exchange(
getGeoNetworkRecordsInsertUrl(),
HttpMethod.PUT,
requestEntity,
Map.class
);

assertEquals("Insert record OK", HttpStatus.CREATED, r.getStatusCode());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import au.org.aodn.esindexer.utils.AssociatedRecordsUtil;
import au.org.aodn.esindexer.model.RelationType;
import au.org.aodn.esindexer.utils.JaxbUtils;
import au.org.aodn.esindexer.utils.StringUtil;
import au.org.aodn.metadata.iso19115_3_2018.MDMetadataType;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchRequest;
Expand Down

0 comments on commit 72582ac

Please sign in to comment.