Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
[FIXED JENKINS-16222] Ignore invalid encodings when reading files.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Feb 13, 2013
1 parent 7e79c79 commit 3b9fbe4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Expand Up @@ -19,6 +19,7 @@
import hudson.model.AbstractBuild;
import hudson.model.Project;

import hudson.plugins.analysis.util.EncodingValidator;
import hudson.plugins.analysis.util.PluginLogger;
import hudson.plugins.analysis.util.model.AbstractAnnotation;
import hudson.plugins.analysis.util.model.AnnotationContainer;
Expand Down Expand Up @@ -450,7 +451,7 @@ private void logExceptionToFile(final IOException exception, final File masterFi

private void print(final FileOutputStream outputStream, final String message,
final Object... arguments) throws IOException {
IOUtils.write(String.format(message, arguments), outputStream, getDefaultEncoding());
IOUtils.write(String.format(message, arguments), outputStream, EncodingValidator.getEncoding(getDefaultEncoding()));
}

/**
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/hudson/plugins/analysis/util/EncodingValidator.java
Expand Up @@ -11,6 +11,8 @@
import java.util.HashSet;
import java.util.Set;

import javax.annotation.CheckForNull;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.lang.StringUtils;
Expand All @@ -31,7 +33,8 @@ public class EncodingValidator implements Validator {

static {
try {
allCharacterSets = Collections.unmodifiableSet(new HashSet<String>(Charset.availableCharsets().keySet()));
allCharacterSets = Collections.unmodifiableSet(new HashSet<String>(Charset
.availableCharsets().keySet()));
}
// CHECKSTYLE:OFF
catch (Exception exception) {
Expand All @@ -54,11 +57,11 @@ public static Set<String> getAvailableCharsets() {
* default encoding is empty or <code>null</code>, or if the charset is not
* valid then the default encoding of the platform is returned.
*
* @param defaultEncoding identifier of the character set
*
* @param defaultEncoding
* identifier of the character set
* @return the default charset for the specified encoding string
*/
public static Charset defaultCharset(final String defaultEncoding) {
public static Charset defaultCharset(@CheckForNull final String defaultEncoding) {
try {
if (StringUtils.isNotBlank(defaultEncoding)) {
return Charset.forName(defaultEncoding);
Expand Down Expand Up @@ -88,7 +91,8 @@ public static Charset defaultCharset(final String defaultEncoding) {
* Signals that an I/O exception has occurred during reading of
* the file.
*/
public static LineIterator readFile(final String fileName, final String encoding) throws FileNotFoundException, IOException {
public static LineIterator readFile(final String fileName, @CheckForNull final String encoding)
throws FileNotFoundException, IOException {
FileInputStream stream = new FileInputStream(new File(fileName));
if (StringUtils.isNotBlank(encoding)) {
return IOUtils.lineIterator(stream, encoding);
Expand Down Expand Up @@ -122,5 +126,20 @@ public FormValidation check(final String encoding) throws FormValidation {
}
throw FormValidation.error(Messages.FieldValidator_Error_DefaultEncoding());
}
}

/**
* Returns the encoding used to read a file. If the specified
* encoding is empty or <code>null</code>, or if the encoding is not valid
* then the default encoding of the platform is returned.
*
* @param encoding
* identifier of the character set
* @return the default encoding for the specified encoding string
*/
public static String getEncoding(@CheckForNull final String encoding) {
if (StringUtils.isNotBlank(encoding) && Charset.forName(encoding).canEncode()) {
return encoding;
}
return Charset.defaultCharset().name();
}
}
@@ -1,5 +1,9 @@
package hudson.plugins.analysis.util;

import static org.junit.Assert.*;

import java.nio.charset.Charset;

import org.junit.Test;

/**
Expand Down Expand Up @@ -32,6 +36,17 @@ public void testInvalidEncodings() {
assertThatInputIsInvalid("ISO-8859-42");
}

/**
* Verifies that the platform encoding is used if encoding is invalid.
*/
@Test
public void testDefaultEncoding() {
assertEquals("Wrong encoding used", "UTF-8", EncodingValidator.getEncoding("UTF-8"));
String osCharset = Charset.defaultCharset().name();
assertEquals("Wrong encoding used", osCharset, EncodingValidator.getEncoding(""));
assertEquals("Wrong encoding used", osCharset, EncodingValidator.getEncoding(null));
}

@Override
protected Validator createValidator() {
return new EncodingValidator();
Expand Down

0 comments on commit 3b9fbe4

Please sign in to comment.