Skip to content

Commit

Permalink
CORE-2874 Ensure consistent charset encoding usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nvoxland committed Sep 1, 2016
1 parent e9e5afe commit 936b617
Show file tree
Hide file tree
Showing 37 changed files with 161 additions and 110 deletions.
1 change: 1 addition & 0 deletions findbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<FindBugsFilter>
<LastVersion value="-1" relOp="NEQ"/>
<Class name="liquibase.util.grammar.SimpleCharStream"/>
<And>
<Rank value="10"/>
<Bug category="SECURITY"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package liquibase.change;

import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
import liquibase.database.core.MSSQLDatabase;
import liquibase.exception.*;
Expand Down Expand Up @@ -169,7 +171,7 @@ public CheckSum generateCheckSum() {
}

if (sql != null) {
stream = new ByteArrayInputStream(sql.getBytes("UTF-8"));
stream = new ByteArrayInputStream(sql.getBytes(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));
}

return CheckSum.compute(new NormalizingStream(this.getEndDelimiter(), this.isSplitStatements(), this.isStripComments(), stream), false);
Expand Down Expand Up @@ -272,7 +274,11 @@ public static class NormalizingStream extends InputStream {

public NormalizingStream(String endDelimiter, Boolean splitStatements, Boolean stripComments, InputStream stream) {
this.stream = new PushbackInputStream(stream, 2048);
this.headerStream = new ByteArrayInputStream((endDelimiter+":"+splitStatements+":"+stripComments+":").getBytes());
try {
this.headerStream = new ByteArrayInputStream((endDelimiter+":"+splitStatements+":"+stripComments+":").getBytes(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));
} catch (UnsupportedEncodingException e) {
throw new UnexpectedLiquibaseException(e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package liquibase.change.core;

import liquibase.change.*;
import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
import liquibase.database.DatabaseList;
import liquibase.database.core.HsqlDatabase;
Expand Down Expand Up @@ -219,11 +221,12 @@ public CheckSum generateCheckSum() {
procedureText = "";
}

String encoding = LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding();
if (procedureText != null) {
try {
stream = new ByteArrayInputStream(procedureText.getBytes("UTF-8"));
stream = new ByteArrayInputStream(procedureText.getBytes(encoding));
} catch (UnsupportedEncodingException e) {
throw new AssertionError("UTF-8 is not supported by the JVM, this should not happen according to the JavaDoc of the Charset class");
throw new AssertionError(encoding+" is not supported by the JVM, this should not happen according to the JavaDoc of the Charset class");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import liquibase.change.DatabaseChange;
import liquibase.change.ChangeMetaData;
import liquibase.change.DatabaseChangeProperty;
import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.exception.ValidationErrors;
Expand Down Expand Up @@ -174,8 +176,8 @@ protected void executeCommand(Database database) throws Exception {
;
}

LogFactory.getLogger().severe(errorStream.toString());
LogFactory.getLogger().info(inputStream.toString());
LogFactory.getLogger().severe(errorStream.toString(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));
LogFactory.getLogger().info(inputStream.toString(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));

if (returnCode != 0) {
throw new RuntimeException(getCommandString() + " returned an code of " + returnCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import liquibase.Labels;
import liquibase.change.CheckSum;
import liquibase.changelog.ChangeSet.ExecType;
import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
import liquibase.database.OfflineConnection;
import liquibase.exception.DatabaseException;
Expand All @@ -21,10 +23,7 @@
import liquibase.util.csv.CSVReader;
import liquibase.util.csv.CSVWriter;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.*;

@LiquibaseService(skip = true)
Expand Down Expand Up @@ -114,9 +113,9 @@ public void init() throws DatabaseException {
}

protected void writeHeader(File file) throws IOException {
FileWriter writer = null;
Writer writer = null;
try {
writer = new FileWriter(file);
writer = new OutputStreamWriter(new FileOutputStream(file), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
CSVWriter csvWriter = new CSVWriter(writer);
csvWriter.writeNext(new String[]{
"ID",
Expand Down Expand Up @@ -157,9 +156,9 @@ public String[] execute(String[] line) {

@Override
public List<RanChangeSet> getRanChangeSets() throws DatabaseException {
FileReader reader = null;
Reader reader = null;
try {
reader = new FileReader(this.changeLogFile);
reader = new InputStreamReader(new FileInputStream(this.changeLogFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
CSVReader csvReader = new CSVReader(reader);
String[] line = csvReader.readNext();

Expand Down Expand Up @@ -218,12 +217,12 @@ protected void replaceChangeSet(ChangeSet changeSet, ReplaceChangeSetLogic repla
File oldFile = this.changeLogFile;
File newFile = new File(oldFile.getParentFile(), oldFile.getName()+".new");

FileReader reader = null;
FileWriter writer = null;
Reader reader = null;
Writer writer = null;

try {
reader = new FileReader(oldFile);
writer = new FileWriter(newFile);
reader = new InputStreamReader(new FileInputStream(oldFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
writer = new OutputStreamWriter(new FileOutputStream(newFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
CSVReader csvReader = new CSVReader(reader);
CSVWriter csvWriter = new CSVWriter(writer);
String[] line;
Expand Down Expand Up @@ -267,12 +266,12 @@ protected void appendChangeSet(ChangeSet changeSet, ChangeSet.ExecType execType)
File oldFile = this.changeLogFile;
File newFile = new File(oldFile.getParentFile(), oldFile.getName()+".new");

FileReader reader = null;
FileWriter writer = null;
Reader reader = null;
Writer writer = null;

try {
reader = new FileReader(oldFile);
writer = new FileWriter(newFile);
reader = new InputStreamReader(new FileInputStream(oldFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
writer = new OutputStreamWriter(new FileOutputStream(newFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
CSVReader csvReader = new CSVReader(reader);
CSVWriter csvWriter = new CSVWriter(writer);
String[] line;
Expand Down Expand Up @@ -373,9 +372,9 @@ public int getNextSequenceValue() throws LiquibaseException {
if (lastChangeSetSequenceValue == null) {
lastChangeSetSequenceValue = 0;

FileReader reader = null;
Reader reader = null;
try {
reader = new FileReader(this.changeLogFile);
reader = new InputStreamReader(new FileInputStream(this.changeLogFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
CSVReader csvReader = new CSVReader(reader);
String[] line = csvReader.readNext(); //skip header line

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import liquibase.database.Database;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

public class AuthorWriter extends HTMLWriter {
Expand All @@ -20,6 +20,6 @@ protected String createTitle(Object object) {
}

@Override
protected void writeCustomHTML(FileWriter fileWriter, Object object, List<Change> changes, Database database) throws IOException {
protected void writeCustomHTML(Writer fileWriter, Object object, List<Change> changes, Database database) throws IOException {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package liquibase.dbdoc;

import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.resource.ResourceAccessor;
import liquibase.util.StreamUtil;

Expand Down Expand Up @@ -35,7 +37,7 @@ public void writeChangeLog(String changeLog, String physicalFilePath) throws IOE
File xmlFile = new File(outputDir, changeLogOutFile + ".html");
xmlFile.getParentFile().mkdirs();

BufferedWriter changeLogStream = new BufferedWriter(new FileWriter(xmlFile, false));
BufferedWriter changeLogStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(xmlFile, false), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()));
try {
changeLogStream.write("<html><body><pre>\n");
changeLogStream.write(StreamUtil.getStreamContents(stylesheet).replace("<", "&lt;").replace(">", "&gt;"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import liquibase.database.Database;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

public class ColumnWriter extends HTMLWriter {
Expand All @@ -21,6 +21,6 @@ protected String createTitle(Object object) {
}

@Override
protected void writeCustomHTML(FileWriter fileWriter, Object object, List<Change> changes, Database database) throws IOException {
protected void writeCustomHTML(Writer fileWriter, Object object, List<Change> changes, Database database) throws IOException {
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package liquibase.dbdoc;

import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.structure.DatabaseObject;
import liquibase.util.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.SortedSet;

public class HTMLListWriter {
Expand All @@ -25,7 +25,7 @@ public HTMLListWriter(String title, String filename, String subdir, File outputD
}

public void writeHTML(SortedSet objects) throws IOException {
FileWriter fileWriter = new FileWriter(new File(outputDir, filename));
Writer fileWriter = new OutputStreamWriter(new FileOutputStream(new File(outputDir, filename)), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());

try {
fileWriter.append("<HTML>\n" + "<HEAD>\n" + "<TITLE>\n");
Expand Down
26 changes: 13 additions & 13 deletions liquibase-core/src/main/java/liquibase/dbdoc/HTMLWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import liquibase.change.Change;
import liquibase.changelog.ChangeSet;
import liquibase.configuration.GlobalConfiguration;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.exception.DatabaseHistoryException;
import liquibase.util.LiquibaseUtil;
import liquibase.util.StringUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.util.Date;
Expand All @@ -28,14 +28,14 @@ public HTMLWriter(File outputDir, Database database) {
}
}

protected abstract void writeCustomHTML(FileWriter fileWriter, Object object, List<Change> changes, Database database) throws IOException;
protected abstract void writeCustomHTML(Writer fileWriter, Object object, List<Change> changes, Database database) throws IOException;

private FileWriter createFileWriter(Object object) throws IOException {
return new FileWriter(new File(outputDir, DBDocUtil.toFileName(object.toString().toLowerCase()) + ".html"));
private Writer createFileWriter(Object object) throws IOException {
return new OutputStreamWriter(new FileOutputStream(new File(outputDir, DBDocUtil.toFileName(object.toString().toLowerCase()) + ".html")), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
}

public void writeHTML(Object object, List<Change> ranChanges, List<Change> changesToRun, String changeLog) throws IOException, DatabaseHistoryException, DatabaseException {
FileWriter fileWriter = createFileWriter(object);
Writer fileWriter = createFileWriter(object);


try {
Expand All @@ -57,7 +57,7 @@ public void writeHTML(Object object, List<Change> ranChanges, List<Change> chang

}

private void writeFooter(FileWriter fileWriter, String changeLog) throws IOException {
private void writeFooter(Writer fileWriter, String changeLog) throws IOException {
fileWriter.append("<hr>Generated: ");
fileWriter.append(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date()));
fileWriter.append("<BR>Against: ");
Expand All @@ -68,13 +68,13 @@ private void writeFooter(FileWriter fileWriter, String changeLog) throws IOExcep
fileWriter.append("<a href='http://www.liquibase.org' target='_TOP'>Liquibase ").append(LiquibaseUtil.getBuildVersion()).append("</a>");
}

protected void writeBody(FileWriter fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {
protected void writeBody(Writer fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {
writeCustomHTML(fileWriter, object, ranChanges, database);
writeChanges("Pending Changes", fileWriter, changesToRun);
writeChanges("Past Changes", fileWriter, ranChanges);
}

protected void writeTable(String title, List<List<String>> cells, FileWriter fileWriter) throws IOException {
protected void writeTable(String title, List<List<String>> cells, Writer fileWriter) throws IOException {
fileWriter.append("<P>");
int colspan = 0;
if (cells.size() == 0) {
Expand All @@ -96,13 +96,13 @@ protected void writeTable(String title, List<List<String>> cells, FileWriter fil
fileWriter.append("</TABLE>\n");
}

private void writeTD(FileWriter fileWriter, String filePath) throws IOException {
private void writeTD(Writer fileWriter, String filePath) throws IOException {
fileWriter.append("<TD VALIGN=\"top\">\n");
fileWriter.append(filePath);
fileWriter.append("</TD>\n");
}

private void writeHeader(Object object, FileWriter fileWriter) throws IOException {
private void writeHeader(Object object, Writer fileWriter) throws IOException {
String title = createTitle(object);
fileWriter.append("<head>")
.append("<title>").append(title).append("</title>")
Expand All @@ -117,7 +117,7 @@ private void writeHeader(Object object, FileWriter fileWriter) throws IOExceptio

protected abstract String createTitle(Object object);

protected void writeChanges(String title, FileWriter fileWriter, List<Change> changes) throws IOException, DatabaseHistoryException, DatabaseException {
protected void writeChanges(String title, Writer fileWriter, List<Change> changes) throws IOException, DatabaseHistoryException, DatabaseException {
fileWriter.append("<p><TABLE BORDER=\"1\" WIDTH=\"100%\" CELLPADDING=\"3\" CELLSPACING=\"0\" SUMMARY=\"\">\n");
fileWriter.append("<TR BGCOLOR=\"#CCCCFF\" CLASS=\"TableHeadingColor\">\n");
fileWriter.append("<TD COLSPAN='4'><FONT SIZE=\"+2\">\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import liquibase.exception.DatabaseHistoryException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

public class PendingChangesWriter extends HTMLWriter {
Expand All @@ -22,12 +22,12 @@ protected String createTitle(Object object) {
}

@Override
protected void writeBody(FileWriter fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {
protected void writeBody(Writer fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {
writeCustomHTML(fileWriter, object, ranChanges, database);
writeChanges("Pending Changes", fileWriter, changesToRun);
}

@Override
protected void writeCustomHTML(FileWriter fileWriter, Object object, List<Change> changes, Database database) throws IOException {
protected void writeCustomHTML(Writer fileWriter, Object object, List<Change> changes, Database database) throws IOException {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package liquibase.dbdoc;

import liquibase.Liquibase;
import liquibase.change.Change;
import liquibase.change.ChangeFactory;
import liquibase.changelog.ChangeSet;
Expand All @@ -14,8 +13,8 @@
import liquibase.executor.LoggingExecutor;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

public class PendingSQLWriter extends HTMLWriter {
Expand All @@ -33,7 +32,7 @@ protected String createTitle(Object object) {
}

@Override
protected void writeBody(FileWriter fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {
protected void writeBody(Writer fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {

Executor oldTemplate = ExecutorService.getInstance().getExecutor(database);
LoggingExecutor loggingExecutor = new LoggingExecutor(ExecutorService.getInstance().getExecutor(database), fileWriter, database);
Expand Down Expand Up @@ -69,6 +68,6 @@ protected void writeBody(FileWriter fileWriter, Object object, List<Change> ranC
}

@Override
protected void writeCustomHTML(FileWriter fileWriter, Object object, List<Change> changes, Database database) throws IOException {
protected void writeCustomHTML(Writer fileWriter, Object object, List<Change> changes, Database database) throws IOException {
}
}
Loading

0 comments on commit 936b617

Please sign in to comment.