Skip to content

Commit

Permalink
[FIXED JENKINS-16881] Introduced XStream2.toXMLUTF8.
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Feb 25, 2013
1 parent a067ec4 commit b14a7a3
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 3 deletions.
3 changes: 3 additions & 0 deletions changelog.html
Expand Up @@ -55,6 +55,9 @@
<!-- Record your changes in the trunk here. -->
<div id="trunk" style="display:none"><!--=TRUNK-BEGIN=-->
<ul class=image>
<li class=bug>
Incorrect or missing XML encoding declaration on some REST API pages.
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-16881">issue 16881</a>)
<li class=bug>
Fixed: Human readable file size method returns ",00" for files with byte length 0
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-16630">issue 16630</a>)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/Computer.java
Expand Up @@ -1177,7 +1177,7 @@ public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp)
if (req.getMethod().equals("GET")) {
// read
rsp.setContentType("application/xml");
Jenkins.XSTREAM2.toXML(getNode(), rsp.getOutputStream());
Jenkins.XSTREAM2.toXMLUTF8(getNode(), rsp.getOutputStream());
return;
}
if (req.getMethod().equals("POST")) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/model/View.java
Expand Up @@ -1016,8 +1016,7 @@ public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object nod
// pity we don't have a handy way to clone Jenkins.XSTREAM to temp add the omit Field
XStream2 xStream2 = new XStream2();
xStream2.omitField(View.class, "owner");
rsp.getOutputStream().write("<?xml version='1.0' encoding='UTF-8'?>\n".getBytes("UTF-8"));
xStream2.toXML(this, rsp.getOutputStream());
xStream2.toXMLUTF8(this, rsp.getOutputStream());
}
};
}
Expand Down
24 changes: 24 additions & 0 deletions core/src/main/java/hudson/util/XStream2.java
Expand Up @@ -54,9 +54,14 @@
import hudson.util.xstream.ImmutableListConverter;
import hudson.util.xstream.ImmutableMapConverter;
import hudson.util.xstream.MapperDelegate;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -165,6 +170,25 @@ public Mapper getMapperInjectionPoint() {
return mapperInjectionPoint.getDelegate();
}

/**
* @deprecated Uses default encoding yet fails to write an encoding header. Prefer {@link #toXMLUTF8}.
*/
@Deprecated
@Override public void toXML(Object obj, OutputStream out) {
super.toXML(obj, out);
}

/**
* Serializes to a byte stream.
* Uses UTF-8 encoding and specifies that in the XML encoding declaration.
* @since 1.504
*/
public void toXMLUTF8(Object obj, OutputStream out) throws IOException {
Writer w = new OutputStreamWriter(out, Charset.forName("UTF-8"));
w.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
toXML(obj, w);
}

/**
* This method allows one to insert additional mappers after {@link XStream2} was created,
* but because of the way XStream works internally, this needs to be done carefully.
Expand Down
96 changes: 96 additions & 0 deletions core/src/test/java/hudson/util/XStream2EncodingTest.java
@@ -0,0 +1,96 @@
/*
* The MIT License
*
* Copyright 2013 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package hudson.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import static org.hamcrest.CoreMatchers.*;
import org.junit.After;
import static org.junit.Assert.*;
import static org.junit.Assume.*;
import org.junit.Before;
import org.junit.Test;

/**
* In its own suite to minimize the chance of mucking about with other tests.
*/
public class XStream2EncodingTest {

@Before public void useNonUTF8() {
clearDefaultEncoding();
System.setProperty("file.encoding", "ISO-8859-1");
assumeThat(Charset.defaultCharset().name(), is("ISO-8859-1"));
}

@After public void clearDefaultEncodingAfter() {
clearDefaultEncoding();
}

private void clearDefaultEncoding() {
try {
Field defaultCharset = Charset.class.getDeclaredField("defaultCharset");
defaultCharset.setAccessible(true);
defaultCharset.set(null, null);
} catch (Exception x) {
assumeNoException(x);
}
}

@SuppressWarnings("deprecation")
@Test public void toXMLUnspecifiedEncoding() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XStream2 xs = new XStream2();
String msg = "k chybě";
xs.toXML(new Thing(msg), baos);
byte[] ambiguousXml = baos.toByteArray();
Thing t = (Thing) xs.fromXML(new ByteArrayInputStream(ambiguousXml));
assertThat(t.field, not(msg));
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
baos2.write("<?xml version='1.0' encoding='UTF-8'?>\n".getBytes("UTF-8"));
baos2.write(ambiguousXml);
t = (Thing) xs.fromXML(new ByteArrayInputStream(ambiguousXml));
assertThat(t.field, not(msg));
}

@Test public void toXMLUTF8() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XStream2 xs = new XStream2();
String msg = "k chybě";
xs.toXMLUTF8(new Thing(msg), baos);
byte[] unspecifiedData = baos.toByteArray();
Thing t = (Thing) xs.fromXML(new ByteArrayInputStream(unspecifiedData));
assertThat(t.field, is(msg));
}

public static class Thing {
public final String field;
Thing(String field) {
this.field = field;
}
}

}

0 comments on commit b14a7a3

Please sign in to comment.