Pull request #558: Feature/RED-4543 1
Merge in RED/persistence-service from feature/RED-4543-1 to master * commit 'faa1037beada9349fcd3bee8be42d3cf06aaea9c': RED-4543 - In fileAttributesController, support ASCII and ISO as Encoding Type RED-4543 - In fileAttributesController, support ASCII and ISO as Encoding Type
This commit is contained in:
commit
2c1c0059a5
@ -32,7 +32,7 @@ public interface FileAttributesConfigResource {
|
||||
|
||||
String UTF_ENCODING = "UTF-8";
|
||||
String ASCII_ENCODING = "ASCII";
|
||||
String ISO_ENCODING = "ISO-8601";
|
||||
String ISO_ENCODING = "ISO";
|
||||
|
||||
@ResponseBody
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
|
||||
@ -4,6 +4,7 @@ import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@ -46,6 +47,8 @@ import com.opencsv.exceptions.CsvException;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import static com.iqser.red.service.persistence.service.v1.api.resources.FileAttributesConfigResource.*;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@ -71,7 +74,7 @@ public class FileAttributesController implements FileAttributesResource {
|
||||
.filter(f -> !f.isSoftOrHardDeleted())
|
||||
.collect(Collectors.toMap(FileModel::getFilename, Function.identity()));
|
||||
|
||||
List<List<String>> rows = getCsvRecords(importCsvRequest.getCsvFile(), generalConfiguration.getDelimiter());
|
||||
List<List<String>> rows = getCsvRecords(importCsvRequest.getCsvFile(), generalConfiguration.getDelimiter(), generalConfiguration.getEncoding());
|
||||
|
||||
Iterator<List<String>> rowIterator = rows.iterator();
|
||||
|
||||
@ -138,13 +141,13 @@ public class FileAttributesController implements FileAttributesResource {
|
||||
|
||||
|
||||
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE")
|
||||
private List<List<String>> getCsvRecords(byte[] csv, String delimiter) {
|
||||
private List<List<String>> getCsvRecords(byte[] csv, String delimiter, String encoding) {
|
||||
|
||||
CSVParser parser = new CSVParserBuilder().withSeparator(delimiter.charAt(0)).build();
|
||||
|
||||
List<List<String>> records = new ArrayList<>();
|
||||
|
||||
try (CSVReader csvReader = new CSVReaderBuilder(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(csv), StandardCharsets.UTF_8))).withCSVParser(parser)
|
||||
try (CSVReader csvReader = new CSVReaderBuilder(new BufferedReader(new InputStreamReader(new ByteArrayInputStream(csv), parseEncoding(encoding)))).withCSVParser(parser)
|
||||
.build()) {
|
||||
records.addAll(csvReader.readAll().stream().map(Lists::newArrayList).collect(Collectors.toList()));
|
||||
} catch (IOException | CsvException e) {
|
||||
@ -154,5 +157,13 @@ public class FileAttributesController implements FileAttributesResource {
|
||||
return records;
|
||||
}
|
||||
|
||||
private Charset parseEncoding(String encoding) {
|
||||
if (ASCII_ENCODING.equalsIgnoreCase(encoding))
|
||||
return StandardCharsets.US_ASCII;
|
||||
if (ISO_ENCODING.equalsIgnoreCase(encoding))
|
||||
return StandardCharsets.ISO_8859_1;
|
||||
return StandardCharsets.UTF_8;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user