Skip to content

Commit 2cfe09f

Browse files
authored
Merge pull request #12 from com-pas/Fix_special_Characters_In_IID_File
Bug: Special characters should be added to the decompressed string
2 parents 66795f2 + 6bd70de commit 2cfe09f

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ SPDX-License-Identifier: Apache-2.0
152152
<groupId>org.apache.maven.plugins</groupId>
153153
<artifactId>maven-surefire-plugin</artifactId>
154154
<version>${surefire-plugin.version}</version>
155+
<configuration>
156+
<argLine>-Dfile.encoding=UTF-8</argLine>
157+
</configuration>
155158
</plugin>
156159

157160
<plugin>

service/src/main/java/org/lfenergy/compas/sitipe/service/ImportedComponentService.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@
1515
import javax.ws.rs.InternalServerErrorException;
1616
import javax.ws.rs.NotFoundException;
1717
import java.io.*;
18-
import java.nio.charset.Charset;
19-
import java.nio.charset.StandardCharsets;
20-
import java.util.Arrays;
2118
import java.util.List;
22-
import java.util.zip.Inflater;
2319
import java.util.zip.InflaterInputStream;
24-
import java.util.zip.ZipInputStream;
2520

2621
@ApplicationScoped
2722
public class ImportedComponentService {
@@ -49,28 +44,26 @@ public ImportedDataDTO getImportedComponentData(final Integer id) {
4944
throw new NotFoundException("Imported BT Component not found");
5045
}
5146

47+
return new ImportedDataDTO(this.getData(importedComponent));
48+
}
49+
50+
private String getData(final ImportedComponent importedComponent) {
5251
final ByteArrayInputStream bais = new ByteArrayInputStream(importedComponent.getData());
52+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
5353
final InflaterInputStream iis = new InflaterInputStream(bais);
5454

55-
StringBuilder result = new StringBuilder();
56-
byte[] buffer = new byte[5];
57-
58-
int rlen = -1;
55+
int data;
56+
int stopByte = -1;
5957

6058
try {
61-
while ((rlen = iis.read(buffer)) != -1) {
62-
result.append(new String(Arrays.copyOf(buffer, rlen), StandardCharsets.ISO_8859_1));
59+
while (stopByte != (data = iis.read())) {
60+
baos.write(data);
6361
}
6462
} catch (IOException e) {
6563
throw new InternalServerErrorException(e);
6664
}
6765

68-
return new ImportedDataDTO(
69-
new String(result.toString().getBytes(), StandardCharsets.UTF_8)
70-
.replaceAll("[^\\x00-\\x7F]", "")
71-
);
72-
73-
66+
return baos.toString();
7467
}
7568

7669
@Transactional

service/src/test/java/org/lfenergy/compas/sitipe/service/ImportedComponentServiceTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ void itShouldGetById() throws IOException {
8888
assertEquals(data, result.getData());
8989
}
9090

91+
@Test
92+
void itShouldGetByIdWithSpecialCharacters() throws IOException {
93+
final Integer id = 1;
94+
final String data = "<temperature><message>It's currently 5°C outside!</message><northpole> 1μ°C</northpole></temperatur>";
95+
ImportedComponent importedComponent = new ImportedComponent();
96+
importedComponent.setId(id);
97+
importedComponent.setData(compress(data.getBytes(StandardCharsets.UTF_8)));
98+
99+
when(importedComponentRepository.getById(id))
100+
.thenReturn(importedComponent);
101+
102+
ImportedDataDTO result = sut.getImportedComponentData(id);
103+
104+
assertEquals(data, result.getData());
105+
}
91106
@Test
92107
void itShouldThrowErrorWhenImportedComponentNotFound() {
93108
when(importedComponentRepository.getById(any())).thenReturn(null);

0 commit comments

Comments
 (0)