Skip to content

Commit

Permalink
Merge pull request #12 from com-pas/Fix_special_Characters_In_IID_File
Browse files Browse the repository at this point in the history
Bug: Special characters should be added to the decompressed string
  • Loading branch information
Stef3st authored Jul 3, 2023
2 parents 66795f2 + 6bd70de commit 2cfe09f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ SPDX-License-Identifier: Apache-2.0
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<argLine>-Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>

<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@
import javax.ws.rs.InternalServerErrorException;
import javax.ws.rs.NotFoundException;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import java.util.zip.ZipInputStream;

@ApplicationScoped
public class ImportedComponentService {
Expand Down Expand Up @@ -49,28 +44,26 @@ public ImportedDataDTO getImportedComponentData(final Integer id) {
throw new NotFoundException("Imported BT Component not found");
}

return new ImportedDataDTO(this.getData(importedComponent));
}

private String getData(final ImportedComponent importedComponent) {
final ByteArrayInputStream bais = new ByteArrayInputStream(importedComponent.getData());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final InflaterInputStream iis = new InflaterInputStream(bais);

StringBuilder result = new StringBuilder();
byte[] buffer = new byte[5];

int rlen = -1;
int data;
int stopByte = -1;

try {
while ((rlen = iis.read(buffer)) != -1) {
result.append(new String(Arrays.copyOf(buffer, rlen), StandardCharsets.ISO_8859_1));
while (stopByte != (data = iis.read())) {
baos.write(data);
}
} catch (IOException e) {
throw new InternalServerErrorException(e);
}

return new ImportedDataDTO(
new String(result.toString().getBytes(), StandardCharsets.UTF_8)
.replaceAll("[^\\x00-\\x7F]", "")
);


return baos.toString();
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ void itShouldGetById() throws IOException {
assertEquals(data, result.getData());
}

@Test
void itShouldGetByIdWithSpecialCharacters() throws IOException {
final Integer id = 1;
final String data = "<temperature><message>It's currently 5°C outside!</message><northpole> 1μ°C</northpole></temperatur>";
ImportedComponent importedComponent = new ImportedComponent();
importedComponent.setId(id);
importedComponent.setData(compress(data.getBytes(StandardCharsets.UTF_8)));

when(importedComponentRepository.getById(id))
.thenReturn(importedComponent);

ImportedDataDTO result = sut.getImportedComponentData(id);

assertEquals(data, result.getData());
}
@Test
void itShouldThrowErrorWhenImportedComponentNotFound() {
when(importedComponentRepository.getById(any())).thenReturn(null);
Expand Down

0 comments on commit 2cfe09f

Please sign in to comment.