Skip to content

Commit

Permalink
encoding and decoding update in ContextProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
gdesiato committed Jan 29, 2025
1 parent c2207fb commit 6cb1ef6
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 16 deletions.
18 changes: 18 additions & 0 deletions logicaldoc-util/src/main/java/com/logicaldoc/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.text.DecimalFormatSymbols;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Locale;

Expand Down Expand Up @@ -247,4 +248,21 @@ public static String printFileSize(long size, Locale locale) {
public static String defaultString(String input, String def) {
return StringUtils.defaultString(input, def);
}


/**
* Checks if a give n string is base64 coded
*
* @param value The tring to evaluate
*
* @return true only if value is base64 coded
*/
public static boolean isBase64(String value) {
try {
Base64.getDecoder().decode(value);
return true;
} catch (IllegalArgumentException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand All @@ -21,6 +23,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.logicaldoc.util.StringUtil;
import com.logicaldoc.util.io.FileUtil;

/**
Expand Down Expand Up @@ -131,11 +134,10 @@ private void load(URL fileUrl) throws IOException {

public ContextProperties(File file) throws IOException {
try {
if (file.exists()) {
this.file = file;
try (FileInputStream fis = new FileInputStream(file)) {
this.file = file;
FileUtils.touch(file);
try (FileInputStream fis = new FileInputStream(file)) {
load(fis);
}
}
} catch (IOException e) {
throw new IOException("Unable to read from " + file.getPath(), e);
Expand Down Expand Up @@ -324,6 +326,13 @@ public float getFloat(String property, float defaultValue) {
return Float.parseFloat(v.trim());
}

@Override
public synchronized Object setProperty(String key, String value) {
if (value.contains("\n"))
value = Base64.getEncoder().encodeToString(value.getBytes(Charset.forName("UTF-8")));
return super.setProperty(key, value);
}

/**
* Gets the property value replacing all variable references
*
Expand All @@ -333,14 +342,32 @@ public float getFloat(String property, float defaultValue) {
*/
@Override
public String getProperty(String property) {
return StrSubstitutor.replaceSystemProperties(super.getProperty(property));
String value = super.getProperty(property);
return getPropertyWithDecoding(value);
}

/**
* Gets the property value replacing all variable references
*
* @param property name of the setting to process
* @param defaultValue default value returned in case of absence of property
*
* @return the porperty's value with expanded variables
*/
@Override
public String getProperty(String property, String defaultValue) {
return StrSubstitutor.replaceSystemProperties(super.getProperty(property, defaultValue));
public String getProperty(String property, String defaultValue) {
String value = super.getProperty(property, defaultValue);
return getPropertyWithDecoding(value);
}

private String getPropertyWithDecoding(String value) {
if (StringUtil.isBase64(value)) {
byte[] decodedBytes = Base64.getDecoder().decode(value);
value = new String(decodedBytes);
}
return StrSubstitutor.replaceSystemProperties(value);
}

public int getMaxBackups() {
return maxBackups;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void store(OutputStream out, String header) throws IOException {

for (int i = 0; i < keys.size(); i++) {
thekey = keys.get(i);
thevalue = this.getProperty(thekey);
thevalue = super.getProperty(thekey);
thevalue = doubleSlash(thevalue);

oneline = thekey + "=" + thevalue + "\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.logicaldoc.util.config;

import static org.junit.Assert.assertEquals;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Properties;

import org.junit.After;
Expand All @@ -25,7 +29,7 @@ public class ContextPropertiesTest {

private ContextProperties contextProperties;

private File propsFile = new File("target/testcontext.properties");
private File propsFile = new File("target/testtest.properties");

@Before
public void setUp() throws IOException {
Expand All @@ -43,17 +47,82 @@ public void trearDown() throws IOException {

@Test
public void testWrite() throws IOException {
Assert.assertEquals("target/store", contextProperties.getProperty("store.1.dir"));
File abcFile = new File("target/abc1.properties");
ContextProperties contextProperties = new ContextProperties(abcFile);

contextProperties.setProperty("test", "value");

contextProperties.write();

Assert.assertEquals("value", contextProperties.getProperty("test"));
}

@Test
public void testSetAndGetProperty() throws FileNotFoundException, IOException {
File abcFile = new File("target/abc.properties");
ContextProperties contextProperties = new ContextProperties(abcFile);

// testing implicit encoding and decoding in setProperty and getPoperty (ContextProperties class)
contextProperties.setProperty("propA", "pippo");
contextProperties.setProperty("propB", """
pippo
pluto
paperino""");
contextProperties.setProperty("propC",
"IExvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0LCBjb25zZWN0ZXR1ciBhZGlwaXNjaW5nIGVsaXQu");

contextProperties.setProperty("emptyKey", "");

contextProperties.write();

assertEquals("pippo", contextProperties.getProperty("propA"));
assertEquals("pippo\npluto\npaperino", contextProperties.getProperty("propB"));
assertEquals(" Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
contextProperties.getProperty("propC"));

assertEquals("", contextProperties.getProperty("emptyKey"));

Properties properties = new Properties();
try (InputStream is = new FileInputStream(propsFile)) {
properties.load(is);
try (FileReader reader = new FileReader(abcFile)) {
properties.load(reader);
} finally {
FileUtil.delete(abcFile);
}

Assert.assertEquals("target/store", properties.getProperty("store.1.dir"));
Assert.assertEquals("value", properties.getProperty("test"));
// testing explicit encoding and decoding (Properties class)
assertEquals("pippo", properties.getProperty("propA"));
assertEquals(Base64.getEncoder().encodeToString("""
pippo
pluto
paperino""".getBytes()), properties.getProperty("propB"));
assertEquals("IExvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0LCBjb25zZWN0ZXR1ciBhZGlwaXNjaW5nIGVsaXQu",
properties.getProperty("propC"));

String decodedValue = new String(
Base64.getDecoder()
.decode("IExvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0LCBjb25zZWN0ZXR1ciBhZGlwaXNjaW5nIGVsaXQu"),
StandardCharsets.UTF_8);
assertEquals(" Lorem ipsum dolor sit amet, consectetur adipiscing elit.", decodedValue);

assertEquals("cGlwcG8KcGx1dG8KcGFwZXJpbm8=", properties.getProperty("propB"));

String encodedValue = Base64.getEncoder().encodeToString("""
pippo
pluto
paperino""".getBytes());
assertEquals(encodedValue, properties.getProperty("propB"));

String decodedValue1 = new String(Base64.getDecoder().decode(encodedValue));
assertEquals("pippo\npluto\npaperino", decodedValue1);

assertEquals("", contextProperties.getProperty("emptyKey"));

// no encoding in properties.setProperty
String multiLineValue = """
pippo
pluto
paperino""";
properties.setProperty("propB", multiLineValue);
assertEquals(multiLineValue, properties.getProperty("propB"));
}
}

0 comments on commit 6cb1ef6

Please sign in to comment.