Skip to content

Commit

Permalink
fix(charset): read file with charset or as bytes (#545)
Browse files Browse the repository at this point in the history
* fix(charset): read file with charset or as bytes

* build(codeql): setup specific java version for CodeQL analysis
  • Loading branch information
juancgalvis authored Sep 19, 2024
1 parent 4cec675 commit 92a11d8
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 57 deletions.
68 changes: 37 additions & 31 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,47 @@ jobs:
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit

- name: Checkout repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Checkout repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality
- name: Set up JDK 17
uses: actions/setup-java@2dfa2011c5b2a0f1489bf9e433881c92c1631f88 # v4.3.0
with:
distribution: 'temurin'
java-version: 17


# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7
# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@8214744c546c1e5c8f03dde8fab3a7353211988d # v3.26.7
15 changes: 8 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
id 'maven-publish'
id 'java-gradle-plugin'
id 'org.sonarqube' version '5.1.0.4882'
id 'com.gradle.plugin-publish' version '1.2.2'
id 'com.gradle.plugin-publish' version '1.3.0'
id 'com.github.sherter.google-java-format' version '0.9'
id 'io.github.gradle-nexus.publish-plugin' version '2.0.0'
id 'org.owasp.dependencycheck' version '10.0.4'
Expand Down Expand Up @@ -182,13 +182,13 @@ if (project.hasProperty('signing.keyId')) { // publish as library in maven centr

dependencies {
api 'com.github.spullara.mustache.java:compiler:0.9.14'
api 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
api 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.17.2'
api 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.17.2'
api 'commons-io:commons-io:2.16.1'
api 'com.fasterxml.jackson.core:jackson-databind:2.18.0-rc1'
api 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.18.0-rc1'
api 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.18.0-rc1'
api 'commons-io:commons-io:2.17.0'
api gradleApi()

implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.17.2'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.0-rc1'
implementation 'org.reflections:reflections:0.10.2'
// swagger generators
implementation('io.swagger.codegen.v3:swagger-codegen-generators:1.0.52') {
Expand All @@ -205,6 +205,7 @@ dependencies {
testImplementation gradleTestKit()
testImplementation 'org.mockito:mockito-junit-jupiter:5.13.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.0'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.11.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.11.0'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.11.0'

Expand Down Expand Up @@ -287,7 +288,7 @@ tasks.register('installGitHooks') {
}

tasks.named('wrapper') {
gradleVersion = '8.10'
gradleVersion = '8.10.1'
}

tasks.register('ci-updater', JavaExec) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package=co.com.bancolombia
systemProp.version=3.17.22
simulateRest=true
systemProp.sonar.gradle.skipCompile=true
systemProp.sonar.gradle.skipCompile=true
4 changes: 2 additions & 2 deletions src/main/java/co/com/bancolombia/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public final class Constants {
public static final String REACTIVE_COMMONS_VERSION = "4.1.4";
public static final String REACTIVE_COMMONS_MAPPER_VERSION = "0.1.0";
public static final String BLOCK_HOUND_VERSION = "1.0.9.RELEASE";
public static final String AWS_BOM_VERSION = "2.28.1";
public static final String COMMONS_JMS_VERSION = "2.2.18";
public static final String AWS_BOM_VERSION = "2.28.4";
public static final String COMMONS_JMS_VERSION = "2.3.1";
public static final String GRAPHQL_KICKSTART_VERSION = "15.1.0";
public static final String ARCH_UNIT_VERSION = "1.3.0";
public static final String OKHTTP_VERSION = "4.12.0";
Expand Down
33 changes: 21 additions & 12 deletions src/main/java/co/com/bancolombia/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.MalformedInputException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -27,6 +26,7 @@
import java.util.Properties;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import lombok.AccessLevel;
Expand Down Expand Up @@ -59,18 +59,17 @@ public static String readFile(Project project, String filePath) throws IOExcepti
}

public static String readFileAsString(File file, Logger logger) throws IOException {
try {
return Files.lines(Paths.get(file.toURI())).collect(Collectors.joining("\n"));
} catch (MalformedInputException e) {
if (logger != null) {
logger.warn(
"error '{}' reading file {}, trying to read with ISO_8859_1 charset",
e.getMessage(),
file.getAbsoluteFile());
}
return Files.lines(Paths.get(file.toURI()), StandardCharsets.ISO_8859_1)
.collect(Collectors.joining("\n"));
try (Stream<String> lines = Files.lines(Paths.get(file.toURI()))) {
return lines.collect(Collectors.joining("\n"));
} catch (Exception e) {
logException(e, logger, file, "UTF_8");
}
try (Stream<String> lines = Files.lines(Paths.get(file.toURI()), StandardCharsets.ISO_8859_1)) {
return lines.collect(Collectors.joining("\n"));
} catch (Exception e) {
logException(e, logger, file, "ISO_8859_1");
}
return new String(Files.readAllBytes(Paths.get(file.toURI())));
}

public static List<File> finderSubProjects(String dirPath) {
Expand Down Expand Up @@ -200,4 +199,14 @@ public static String readFileFromZip(Path zip, String file) throws IOException {
}
throw new IOException("File not found in zip file");
}

private static void logException(Exception e, Logger logger, File file, String charset) {
if (logger != null) {
logger.warn(
"Error '{}' reading file {} with charset '{}'",
e.getMessage(),
file.getAbsoluteFile(),
charset);
}
}
}
21 changes: 17 additions & 4 deletions src/test/java/co/com/bancolombia/utils/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import co.com.bancolombia.exceptions.ParamNotFoundException;
import com.fasterxml.jackson.databind.node.ObjectNode;
Expand All @@ -18,11 +19,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

public class FileUtilsTest {

Expand All @@ -38,13 +43,21 @@ void readPropertiesNonExists() {
assertThrows(IOException.class, () -> FileUtils.readProperties("build", property));
}

@Test
void readFile() throws IOException {
@ParameterizedTest
@MethodSource("encodings")
void readFileWithEncodings(String file, String expected) throws IOException {
Project project =
ProjectBuilder.builder().withProjectDir(new File("src/test/resources")).build();
String response = FileUtils.readFile(project, "temp.txt");
String response = FileUtils.readFile(project, file);

assertEquals("hello", response);
assertEquals(expected, response);
}

static Stream<Arguments> encodings() {
return Stream.of(
arguments("temp.txt", "hello"),
arguments("temp-iso-8859-1.txt", "¿cómo funcionará?"),
arguments("temp-other.txt", "*\u00ADx>e\u0001%ËØ´£/!vd\u0007"));
}

@Test
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/temp-iso-8859-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�c�mo funcionar�?
1 change: 1 addition & 0 deletions src/test/resources/temp-other.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*�x>e%�ش�/!vd

0 comments on commit 92a11d8

Please sign in to comment.