Skip to content

Commit

Permalink
RESTEASY-1066: Changed DefaultTextPlain, StringTextStar, and
Browse files Browse the repository at this point in the history
JAXB providers to default to UTF-8
  • Loading branch information
ronsigal committed Aug 21, 2014
1 parent ab570b5 commit a284d46
Show file tree
Hide file tree
Showing 14 changed files with 866 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.util.NoContent;
import org.jboss.resteasy.util.TypeConverter;
import org.xml.sax.InputSource;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
Expand All @@ -20,6 +21,7 @@
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -95,10 +97,22 @@ public T readFrom(Class<T> type,

if (suppressExpandEntityExpansion())
{
return processWithoutEntityExpansion(unmarshaller, entityStream);
return processWithoutEntityExpansion(unmarshaller, entityStream, getCharset(mediaType));
}

return (T) unmarshaller.unmarshal(new StreamSource(entityStream));
if (getCharset(mediaType) == null)
{
InputSource is = new InputSource(entityStream);
is.setEncoding("UTF-8");
System.out.println("readFrom(): UTF-8");
StreamSource source = new StreamSource(new InputStreamReader(entityStream, "UTF-8"));
source.setInputStream(entityStream);
return (T) unmarshaller.unmarshal(source);
}
else
{
return (T) unmarshaller.unmarshal(new StreamSource(entityStream));
}
}
catch (JAXBException e)
{
Expand Down Expand Up @@ -170,6 +184,11 @@ public static void setCharset(MediaType mediaType, Marshaller marshaller)
{
marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
}
else
{
System.out.println("setCharset(): UTF-8");
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
}
}

/**
Expand Down Expand Up @@ -230,9 +249,19 @@ protected boolean suppressExpandEntityExpansion()
return !isExpandEntityReferences();
}

protected T processWithoutEntityExpansion(Unmarshaller unmarshaller, InputStream entityStream) throws JAXBException
protected T processWithoutEntityExpansion(Unmarshaller unmarshaller, InputStream entityStream, String charset) throws JAXBException
{
unmarshaller = new ExternalEntityUnmarshaller(unmarshaller);
return (T) unmarshaller.unmarshal(entityStream);
if (charset == null)
{
InputSource is = new InputSource(entityStream);
is.setEncoding("UTF-8");
System.out.println("processWithoutEntityExpansion(): UTF-8");
return (T) unmarshaller.unmarshal(is);
}
else
{
return (T) unmarshaller.unmarshal(entityStream);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
Expand Down Expand Up @@ -116,19 +117,35 @@ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annota

if (suppressExpandEntityExpansion())
{
SAXSource source = new SAXSource(new InputSource(entityStream));
SAXSource source = null;
if (getCharset(mediaType) == null)
{
source = new SAXSource(new InputSource(new InputStreamReader(entityStream, "UTF-8")));
}
else
{
source = new SAXSource(new InputSource(entityStream));
}
JAXBContext ctx = finder.findCachedContext(JaxbCollection.class, mediaType, annotations);
Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller = new ExternalEntityUnmarshaller(unmarshaller);
ele = unmarshaller.unmarshal(source, JaxbCollection.class);
}
else
{
StreamSource source = new StreamSource(entityStream);
StreamSource source = null;
if (getCharset(mediaType) == null)
{
source = new StreamSource(new InputStreamReader(entityStream, "UTF-8"));
}
else
{
source = new StreamSource(entityStream);
}
JAXBContext ctx = finder.findCachedContext(JaxbCollection.class, mediaType, annotations);
ele = ctx.createUnmarshaller().unmarshal(source, JaxbCollection.class);
}

Wrapped wrapped = FindAnnotation.findAnnotation(annotations, Wrapped.class);
if (wrapped != null)
{
Expand Down Expand Up @@ -273,4 +290,13 @@ protected boolean suppressExpandEntityExpansion()
{
return !isExpandEntityReferences();
}
}

public static String getCharset(final MediaType mediaType)
{
if (mediaType != null)
{
return mediaType.getParameters().get("charset");
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,4 @@ protected boolean suppressExpandEntityExpansion()
{
return !isExpandEntityReferences();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jboss.resteasy.test.charset;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class FavoriteMovieXmlRootElement {
private String _title;
public String getTitle() {
return _title;
}
public void setTitle(String title) {
_title = title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jboss.resteasy.test.charset;

import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.MediaType;

public class MediaTypes
{
public static final MediaType APPLICATION_XML_UTF16_TYPE;
public static final MediaType TEXT_PLAIN_UTF16_TYPE;
public static final MediaType WILDCARD_UTF16_TYPE;
public static final String APPLICATION_XML_UTF16 = "application/xml;charset=UTF-16";
public static final String TEXT_PLAIN_UTF16 = "text/plain;charset=UTF-16";
public static final String WILDCARD_UTF16 = "*/*;charset=UTF-16";

static
{
Map<String, String> params = new HashMap<String, String>();
params.put("charset", "UTF-16");
APPLICATION_XML_UTF16_TYPE = new MediaType("application", "xml", params);
TEXT_PLAIN_UTF16_TYPE = new MediaType("text", "plain", params);
WILDCARD_UTF16_TYPE = new MediaType("*", "*", params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.jboss.resteasy.test.charset;

import static org.jboss.resteasy.test.TestPortProvider.generateURL;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.junit.AfterClass;
import org.junit.BeforeClass;

/**
* Unit tests for RESTEASY-1066.
*
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
* @date Aug 13, 2014
*/
public class TestCharsetExpand extends TestCharsetParent
{
protected static Process process;

@BeforeClass
public static void deployServer() throws InterruptedException
{
System.out.println("server default charset: " + Charset.defaultCharset());
Map<String, Charset> charsets = Charset.availableCharsets();
Charset charset = null;
for (Iterator<String> it = charsets.keySet().iterator(); it.hasNext(); )
{
String cs = it.next();
if (!cs.equals(Charset.defaultCharset().name()))
{
charset = charsets.get(cs);
break;
}
}
System.out.println("server using charset: " + charset);

System.out.println(TestServerExpand.class.getCanonicalName());
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home") + separator + "bin" + separator + "java";
System.out.println("classpath: " + classpath);
System.out.println("path: " + path);
ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", classpath, TestServerExpand.class.getCanonicalName());

try
{
System.out.println("Starting server JVM");
process = processBuilder.start();
System.out.println("Started server JVM");
} catch (IOException e1)
{
e1.printStackTrace();
}

ClientRequest request = new ClientRequest(generateURL("/junk"));
ClientResponse<?> response = null;
while (true)
{
try
{
response = request.get();
if (response.getStatus() == 200)
{
System.out.println("Server started: " + response.getEntity(String.class));
break;
}
System.out.println("Waiting on server ...");
}
catch (Exception e)
{
// keep trying
}
System.out.println("Waiting for server");
Thread.sleep(1000);
}
}

@AfterClass
public static void after() throws Exception
{
process.destroy();
System.out.println("Process destroyed.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.jboss.resteasy.test.charset;

import static org.jboss.resteasy.test.TestPortProvider.generateURL;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Map;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.junit.AfterClass;
import org.junit.BeforeClass;

/**
* Unit tests for RESTEASY-1066.
*
* @author <a href="mailto:ron.sigal@jboss.com">Ron Sigal</a>
* @date Aug 13, 2014
*/
public class TestCharsetNoExpand extends TestCharsetParent
{
protected static Process process;

@BeforeClass
public static void deployServer() throws InterruptedException
{
System.out.println("server default charset: " + Charset.defaultCharset());
Map<String, Charset> charsets = Charset.availableCharsets();
Charset charset = null;
for (Iterator<String> it = charsets.keySet().iterator(); it.hasNext(); )
{
String cs = it.next();
if (!cs.equals(Charset.defaultCharset().name()))
{
charset = charsets.get(cs);
break;
}
}
System.out.println("server using charset: " + charset);

System.out.println(TestServerExpand.class.getCanonicalName());
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home") + separator + "bin" + separator + "java";
System.out.println("classpath: " + classpath);
System.out.println("path: " + path);
ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", classpath, TestServerNoExpand.class.getCanonicalName());

try
{
System.out.println("Starting server JVM");
process = processBuilder.start();
System.out.println("Started server JVM");
} catch (IOException e1)
{
e1.printStackTrace();
}

ClientRequest request = new ClientRequest(generateURL("/junk"));
ClientResponse<?> response = null;
while (true)
{
try
{
response = request.get();
if (response.getStatus() == 200)
{
System.out.println("Server started: " + response.getEntity(String.class));
break;
}
System.out.println("Waiting on server ...");
}
catch (Exception e)
{
// keep trying
}
System.out.println("Waiting for server");
Thread.sleep(1000);
}

}

@AfterClass
public static void after() throws Exception
{
process.destroy();
System.out.println("Process destroyed.");
}
}
Loading

0 comments on commit a284d46

Please sign in to comment.