Skip to content

Commit 84d5893

Browse files
committed
Avoid method that assume default platform encoding is suitable.
1 parent f46c00b commit 84d5893

File tree

8 files changed

+44
-11
lines changed

8 files changed

+44
-11
lines changed

ChangeLog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ specific language governing permissions and limitations
1717
under the License.
1818
-->
1919

20+
*2024-02-20 Arturo Bernal (abernal AT apache DOT org)**
21+
22+
* _2.12.2-git-12_
23+
24+
Avoid method that assume default platform encoding is suitable. #232
25+
26+
2027
**2023-12-04 Juan Pablo Santos (juanpablo AT apache DOT org)**
2128

2229
* _2.12.2-git-11_

jspwiki-api/src/main/java/org/apache/wiki/api/Release.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public final class Release {
6969
* <p>
7070
* If the build identifier is empty, it is not added.
7171
*/
72-
public static final String BUILD = "11";
72+
public static final String BUILD = "12";
7373

7474
/**
7575
* This is the generic version string you should use when printing out the version. It is of

jspwiki-main/src/main/java/org/apache/wiki/auth/user/JDBCUserDatabase.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Licensed to the Apache Software Foundation (ASF) under one
3232
import javax.sql.DataSource;
3333
import java.io.IOException;
3434
import java.io.Serializable;
35+
import java.nio.charset.Charset;
3536
import java.security.Principal;
3637
import java.sql.Connection;
3738
import java.sql.DatabaseMetaData;
@@ -298,6 +299,12 @@ public class JDBCUserDatabase extends AbstractUserDatabase {
298299

299300
private boolean m_supportsCommits;
300301

302+
/**
303+
* The character set encoding used by this object. This is obtained from {@link org.apache.wiki.WikiEngine#getContentEncoding()},
304+
* which returns the content encoding of the engine, either UTF-8 or ISO-8859-1.
305+
*/
306+
private Charset m_encoding;
307+
301308
/**
302309
* Looks up and deletes the first {@link UserProfile} in the user database
303310
* that matches a profile having a given login name. If the user database
@@ -415,6 +422,7 @@ public Principal[] getWikiNames() throws WikiSecurityException {
415422
@Override
416423
public void initialize( final Engine engine, final Properties props ) throws NoRequiredPropertyException, WikiSecurityException {
417424
final String jndiName = props.getProperty( PROP_DB_DATASOURCE, DEFAULT_DB_JNDI_NAME );
425+
m_encoding = engine != null ? engine.getContentEncoding() : Charset.defaultCharset();
418426
try {
419427
final Context initCtx = new InitialContext();
420428
final Context ctx = (Context) initCtx.lookup( "java:comp/env" );
@@ -615,7 +623,7 @@ public void save( final UserProfile profile ) throws WikiSecurityException {
615623
ps1.setTimestamp( 6, ts );
616624
ps1.setString( 7, profile.getLoginName() );
617625
try {
618-
ps1.setString( 8, Serializer.serializeToBase64( profile.getAttributes() ) );
626+
ps1.setString( 8, Serializer.serializeToBase64( profile.getAttributes(), m_encoding != null ? m_encoding : Charset.defaultCharset() ) );
619627
} catch ( final IOException e ) {
620628
throw new WikiSecurityException( "Could not save user profile attribute. Reason: " + e.getMessage(), e );
621629
}
@@ -649,7 +657,7 @@ public void save( final UserProfile profile ) throws WikiSecurityException {
649657
ps4.setTimestamp( 6, ts );
650658
ps4.setString( 7, profile.getLoginName() );
651659
try {
652-
ps4.setString( 8, Serializer.serializeToBase64( profile.getAttributes() ) );
660+
ps4.setString( 8, Serializer.serializeToBase64( profile.getAttributes(), m_encoding != null ? m_encoding : Charset.defaultCharset() ) );
653661
} catch ( final IOException e ) {
654662
throw new WikiSecurityException( "Could not save user profile attribute. Reason: " + e.getMessage(), e );
655663
}

jspwiki-main/src/main/java/org/apache/wiki/auth/user/XMLUserDatabase.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Licensed to the Apache Software Foundation (ASF) under one
4141
import java.io.IOException;
4242
import java.io.OutputStreamWriter;
4343
import java.io.Serializable;
44+
import java.nio.charset.Charset;
4445
import java.nio.charset.StandardCharsets;
4546
import java.nio.file.Files;
4647
import java.security.Principal;
@@ -91,6 +92,12 @@ public class XMLUserDatabase extends AbstractUserDatabase {
9192
private Document c_dom;
9293
private File c_file;
9394

95+
/**
96+
* The character set encoding used by this object. This is obtained from {@link org.apache.wiki.WikiEngine#getContentEncoding()},
97+
* which returns the content encoding of the engine, either UTF-8 or ISO-8859-1.
98+
*/
99+
private Charset m_encoding;
100+
94101
/** {@inheritDoc} */
95102
@Override
96103
public synchronized void deleteByLoginName( final String loginName ) throws WikiSecurityException {
@@ -182,6 +189,8 @@ public void initialize( final Engine engine, final Properties props ) throws NoR
182189
defaultFile = new File( engine.getRootPath() + "/WEB-INF/" + DEFAULT_USERDATABASE );
183190
}
184191

192+
m_encoding = engine.getContentEncoding();
193+
185194
// Get database file location
186195
final String file = TextUtil.getStringProperty( props, PROP_USERDATABASE, defaultFile.getAbsolutePath() );
187196
if( file == null ) {
@@ -416,7 +425,7 @@ public synchronized void save( final UserProfile profile ) throws WikiSecurityEx
416425
// Save the attributes as Base64 string
417426
if(!profile.getAttributes().isEmpty()) {
418427
try {
419-
final String encodedAttributes = Serializer.serializeToBase64( profile.getAttributes() );
428+
final String encodedAttributes = Serializer.serializeToBase64( profile.getAttributes(), m_encoding != null ? m_encoding : Charset.defaultCharset() );
420429
final Element attributes = c_dom.createElement( ATTRIBUTES_TAG );
421430
user.appendChild( attributes );
422431
final Text value = c_dom.createTextNode( encodedAttributes );

jspwiki-main/src/main/java/org/apache/wiki/diff/ExternalDiffProvider.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public class ExternalDiffProvider implements DiffProvider {
4949
public static final String PROP_DIFFCOMMAND = "jspwiki.diffCommand";
5050

5151
private String m_diffCommand;
52+
53+
/**
54+
* The character set encoding used by this object. This is obtained from {@link org.apache.wiki.WikiEngine#getContentEncoding()},
55+
* which returns the content encoding of the engine, either UTF-8 or ISO-8859-1.
56+
*/
5257
private Charset m_encoding;
5358

5459
private static final char DIFF_ADDED_SYMBOL = '+';
@@ -113,7 +118,7 @@ public String makeDiffHtml( final Context ctx, final String p1, final String p2
113118
String cmd = TextUtil.replaceString(m_diffCommand, "%s1", f1.getPath());
114119
cmd = TextUtil.replaceString(cmd, "%s2", f2.getPath());
115120

116-
final String output = FileUtil.runSimpleCommand(cmd, f1.getParent());
121+
final String output = FileUtil.runSimpleCommand(cmd, f1.getParent(), m_encoding != null ? m_encoding : Charset.defaultCharset());
117122

118123
// FIXME: Should this rely on the system default encoding?
119124
final String rawWikiDiff = new String( output.getBytes( StandardCharsets.ISO_8859_1 ), m_encoding );

jspwiki-util/src/main/java/org/apache/wiki/util/FileUtil.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,17 @@ public static File newTmpFile( final String content ) throws IOException {
100100
* @return Standard output from the command.
101101
* @param command The command to run
102102
* @param directory The working directory to run the command in
103+
* @param charset The character set encoding to use for the input streams
103104
* @throws IOException If the command failed
104105
* @throws InterruptedException If the command was halted
105106
*/
106-
public static String runSimpleCommand( final String command, final String directory ) throws IOException, InterruptedException {
107+
public static String runSimpleCommand( final String command, final String directory , final Charset charset) throws IOException, InterruptedException {
107108
LOG.info( "Running simple command " + command + " in " + directory );
108109
final StringBuilder result = new StringBuilder();
109110
final Process process = Runtime.getRuntime().exec( command, null, new File( directory ) );
110111

111-
try( final BufferedReader stdout = new BufferedReader( new InputStreamReader( process.getInputStream() ) );
112-
final BufferedReader stderr = new BufferedReader( new InputStreamReader( process.getErrorStream() ) ) ) {
112+
try( final BufferedReader stdout = new BufferedReader( new InputStreamReader( process.getInputStream(), charset ) );
113+
final BufferedReader stderr = new BufferedReader( new InputStreamReader( process.getErrorStream(), charset ) ) ) {
113114
String line;
114115

115116
while( (line = stdout.readLine()) != null ) {

jspwiki-util/src/main/java/org/apache/wiki/util/Serializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2525
import java.io.ObjectInputStream;
2626
import java.io.ObjectOutputStream;
2727
import java.io.Serializable;
28+
import java.nio.charset.Charset;
2829
import java.nio.charset.StandardCharsets;
2930
import java.util.Base64;
3031
import java.util.HashMap;
@@ -74,10 +75,11 @@ private Serializer()
7475
* Serializes a Map and formats it into a Base64-encoded String. For ease of serialization, the Map contents
7576
* are first copied into a HashMap, then serialized into a byte array that is encoded as a Base64 String.
7677
* @param map the Map to serialize
78+
* @param charset the character set encoding to use for the Base64-encoded String
7779
* @return a String representing the serialized form of the Map
7880
* @throws IOException If serialization cannot be done
7981
*/
80-
public static String serializeToBase64(final Map< String, Serializable > map ) throws IOException {
82+
public static String serializeToBase64(final Map< String, Serializable > map, final Charset charset) throws IOException {
8183
// Load the Map contents into a defensive HashMap
8284
final Map<String, Serializable> serialMap = new HashMap<>(map);
8385

@@ -89,7 +91,7 @@ public static String serializeToBase64(final Map< String, Serializable > map ) t
8991

9092
// Transform to Base64-encoded String
9193
final byte[] result = Base64.getEncoder().encode( bytesOut.toByteArray() );
92-
return new String( result ) ;
94+
return new String( result, charset ) ;
9395
}
9496

9597
}

jspwiki-util/src/test/java/org/apache/wiki/util/SerializerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one
1919
package org.apache.wiki.util;
2020

2121
import java.io.Serializable;
22+
import java.nio.charset.Charset;
2223
import java.util.HashMap;
2324
import java.util.Map;
2425

@@ -34,7 +35,7 @@ public void testSerializeMap() throws Exception
3435
final Map<String, Serializable> map = new HashMap<String, Serializable>();
3536
map.put( "attribute1", "some random value" );
3637
map.put( "attribute2", "another value" );
37-
final String serializedForm = Serializer.serializeToBase64( map );
38+
final String serializedForm = Serializer.serializeToBase64( map , Charset.defaultCharset());
3839

3940
final Map<String, ? extends Serializable> newMap = Serializer.deserializeFromBase64( serializedForm );
4041
Assertions.assertEquals( 2, newMap.size() );

0 commit comments

Comments
 (0)