Skip to content

Commit ae982d5

Browse files
authored
Merge pull request #35869 from vespa-engine/arnej/locale-config-application-package
Ensure UTF-8 in config application package
2 parents a47c461 + 25e1769 commit ae982d5

7 files changed

Lines changed: 22 additions & 17 deletions

File tree

config-application-package/src/main/java/com/yahoo/config/application/ConfigDefinitionDir.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.IOException;
99
import java.io.OutputStream;
1010
import java.util.List;
11+
import java.nio.charset.StandardCharsets;
1112

1213
/**
1314
* A @{link ConfigDefinitionDir} contains a set of config definitions. New definitions may be added,
@@ -41,7 +42,7 @@ private void checkAndCopyUserDefs(Bundle bundle, List<Bundle> bundlesAdded) thro
4142
" (got " + outFile.getAbsolutePath() + ")");
4243
}
4344
OutputStream out = new FileOutputStream(outFile);
44-
out.write(def.contents.getBytes());
45+
out.write(def.contents.getBytes(StandardCharsets.UTF_8));
4546
out.close();
4647
}
4748
}

config-application-package/src/main/java/com/yahoo/config/application/XmlPreProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.yahoo.config.provision.InstanceName;
88
import com.yahoo.config.provision.RegionName;
99
import com.yahoo.config.provision.Tags;
10+
import com.yahoo.text.Utf8;
1011
import com.yahoo.text.XML;
1112
import org.w3c.dom.Document;
1213
import org.xml.sax.InputSource;
@@ -51,7 +52,7 @@ public XmlPreProcessor(File applicationDir,
5152
RegionName region,
5253
CloudName cloud,
5354
Tags tags) throws IOException {
54-
this(applicationDir, new FileReader(xmlInput), instance, environment, region, cloud, tags);
55+
this(applicationDir, Utf8.createReader(xmlInput), instance, environment, region, cloud, tags);
5556
}
5657

5758
public XmlPreProcessor(File applicationDir,

config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationFile.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ public static boolean deleteFile(File path) {
8383

8484
@Override
8585
public Reader createReader() throws FileNotFoundException {
86-
return new FileReader(file);
86+
return Utf8.createReader(file);
8787
}
88-
8988
@Override
9089
public InputStream createInputStream() throws FileNotFoundException {
9190
return new FileInputStream(file);

config-application-package/src/main/java/com/yahoo/config/model/application/provider/FilesApplicationPackage.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.yahoo.io.IOUtils;
2222
import com.yahoo.io.reader.NamedReader;
2323
import com.yahoo.path.Path;
24+
import com.yahoo.text.Utf8;
2425
import com.yahoo.vespa.config.ConfigDefinition;
2526
import com.yahoo.vespa.config.ConfigDefinitionBuilder;
2627
import com.yahoo.vespa.config.ConfigDefinitionKey;
@@ -145,7 +146,7 @@ private List<NamedReader> getFiles(Path relativePath, String namePrefix, String
145146
readers.addAll(getFiles(relativePath.append(file.getName()), namePrefix + "/" + file.getName(), suffix, recurse));
146147
} else {
147148
if (suffix == null || file.getName().endsWith(suffix))
148-
readers.add(new NamedReader(file.getName(), new FileReader(file)));
149+
readers.add(new NamedReader(file.getName(), Utf8.createReader(file)));
149150
}
150151
}
151152
}
@@ -175,7 +176,7 @@ public Reader getHosts() {
175176
try {
176177
File hostsFile = applicationFile(HOSTS);
177178
if (!hostsFile.exists()) return null;
178-
return new FileReader(hostsFile);
179+
return Utf8.createReader(hostsFile);
179180
} catch (Exception e) {
180181
throw new IllegalArgumentException(e);
181182
}
@@ -238,7 +239,7 @@ public Collection<NamedReader> getSchemas() {
238239
Set<NamedReader> ret = new LinkedHashSet<>();
239240
try {
240241
for (File f : getSchemaFiles()) {
241-
ret.add(new NamedReader(f.getName(), new FileReader(f)));
242+
ret.add(new NamedReader(f.getName(), Utf8.createReader(f)));
242243
}
243244
} catch (Exception e) {
244245
throw new IllegalArgumentException("Couldn't get schema contents.", e);
@@ -254,7 +255,7 @@ public Collection<NamedReader> getSchemas() {
254255
*/
255256
private Reader retrieveConfigDefReaderFromThis(File defPath) {
256257
try {
257-
return new NamedReader(defPath.getPath(), new FileReader(defPath));
258+
return new NamedReader(defPath.getPath(), Utf8.createReader(defPath));
258259
} catch (IOException e) {
259260
throw new IllegalArgumentException("Could not read config definition file '" + defPath + "'", e);
260261
}
@@ -335,8 +336,7 @@ public String getUnparsedContent() {
335336
@Override
336337
public Reader getServices() {
337338
try {
338-
return new FileReader(applicationFile(SERVICES).getPath());
339-
} catch (Exception e) {
339+
return Utf8.createReader(applicationFile(SERVICES).getPath()); } catch (Exception e) {
340340
throw new IllegalArgumentException(e);
341341
}
342342
}
@@ -403,7 +403,7 @@ private ApplicationMetaData readMetaData(File appDir) {
403403
if ( ! metaFile.exists()) {
404404
return defaultMetaData;
405405
}
406-
try (FileReader reader = new FileReader(metaFile)) {
406+
try (Reader reader = Utf8.createReader(metaFile)) {
407407
return ApplicationMetaData.fromJsonString(IOUtils.readAll(reader));
408408
} catch (Exception e) {
409409
// Not a big deal, return default

config-application-package/src/main/java/com/yahoo/config/model/application/provider/SchemaValidators.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import com.yahoo.component.Version;
55
import com.yahoo.io.IOUtils;
6+
import com.yahoo.text.Text;
67
import org.osgi.framework.Bundle;
78
import org.xml.sax.SAXException;
89
import java.io.File;
@@ -16,6 +17,8 @@
1617
import java.util.jar.JarFile;
1718
import java.util.logging.Level;
1819
import java.util.logging.Logger;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.Locale;
1922

2023
import static com.yahoo.vespa.defaults.Defaults.getDefaults;
2124
import static java.nio.file.Files.createTempDirectory;
@@ -123,7 +126,7 @@ private File saveSchemasFromJar(File tmpBase, Version vespaVersion) throws IOExc
123126
schemasFound = true;
124127
copySchemas(schemaPath, tmpDir);
125128
} else {
126-
log.log(Level.FINE, () -> String.format("Saving schemas for model bundle %s:%s", bundle.getSymbolicName(), bundle.getVersion()));
129+
log.log(Level.FINE, () -> Text.format("Saving schemas for model bundle %s:%s", bundle.getSymbolicName(), bundle.getVersion()));
127130
for (Enumeration<URL> entries = bundle.findEntries("schema", "*.rnc", true); entries.hasMoreElements(); ) {
128131
URL url = entries.nextElement();
129132
writeContentsToFile(tmpDir, url.getFile(), url.openStream());
@@ -154,7 +157,7 @@ private static void copySchemas(File from, File to) throws IOException {
154157
}
155158

156159
private static void writeContentsToFile(File outDir, String outFile, InputStream inputStream) throws IOException {
157-
String contents = IOUtils.readAll(new InputStreamReader(inputStream));
160+
String contents = IOUtils.readAll(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
158161

159162
// Validate and sanitize the output path
160163
File out = new File(outDir, outFile).getCanonicalFile();

config-application-package/src/test/java/com/yahoo/config/application/OverrideProcessorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ public void testSearchersReferredWithIdRefPerCloud() throws TransformerException
410410
</container>
411411
"</services>""";
412412

413-
assertOverride(input, "aws", expected.formatted("AwsSearcher"));
414-
assertOverride(input, "gcp", expected.formatted("GcpSearcher"));
413+
assertOverride(input, "aws", com.yahoo.text.Text.format(expected, "AwsSearcher"));
414+
assertOverride(input, "gcp", com.yahoo.text.Text.format(expected, "GcpSearcher"));
415415
}
416416

417417
private void assertOverride(Environment environment, RegionName region, String expected) throws TransformerException {

config-application-package/src/test/java/com/yahoo/config/model/application/provider/FilesApplicationPackageTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.yahoo.config.provision.RegionName;
88
import com.yahoo.config.provision.Zone;
99
import com.yahoo.io.IOUtils;
10+
import com.yahoo.text.Utf8;
1011
import org.junit.Rule;
1112
import org.junit.Test;
1213
import org.junit.rules.TemporaryFolder;
@@ -96,7 +97,7 @@ public void testDeploymentXml() throws IOException {
9697
assertTrue(app.getDeployment().isPresent());
9798
assertFalse(app.getDeploymentSpec().isEmpty());
9899
assertFalse(app.getMajorVersion().isPresent());
99-
assertEquals(IOUtils.readAll(app.getDeployment().get()), IOUtils.readAll(new FileReader(deployment)));
100+
assertEquals(IOUtils.readAll(app.getDeployment().get()), IOUtils.readAll(Utf8.createReader(deployment)));
100101
}
101102

102103
@Test
@@ -108,7 +109,7 @@ public void testPinningMajorVersion() throws IOException {
108109
assertTrue(app.getDeployment().isPresent());
109110
assertTrue(app.getMajorVersion().isPresent());
110111
assertEquals(6, (int)app.getMajorVersion().get());
111-
assertEquals(IOUtils.readAll(app.getDeployment().get()), IOUtils.readAll(new FileReader(deployment)));
112+
assertEquals(IOUtils.readAll(app.getDeployment().get()), IOUtils.readAll(Utf8.createReader(deployment)));
112113
}
113114

114115
@Test

0 commit comments

Comments
 (0)