You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by "Niels Boeger (JIRA)" <ji...@apache.org> on 2008/08/28 09:09:44 UTC

[jira] Created: (WICKET-1805) Allow to change charset in StringRequestTarget: change CharSet used by the OutStream as well

Allow to change charset in StringRequestTarget: change CharSet used by the OutStream as well
--------------------------------------------------------------------------------------------

                 Key: WICKET-1805
                 URL: https://issues.apache.org/jira/browse/WICKET-1805
             Project: Wicket
          Issue Type: Sub-task
          Components: wicket
    Affects Versions: 1.4-M3, 1.3.4
         Environment: Java 5 on Mac and Win, using FF2/3 and IE7
            Reporter: Niels Boeger


the CharSet is provided as meta info only, the actual CharSet used by the OutStream is not changed. This leads to data being transferred in a different encoding than specified by the ContentType meta data.
I suggest using another constructor:

Index: StringRequestTarget.java
===================================================================
--- StringRequestTarget.java	(revision 689726)
+++ StringRequestTarget.java	(working copy)
@@ -17,6 +17,7 @@
 package org.apache.wicket.request.target.basic;
 
 import java.io.OutputStream;
+import java.nio.charset.Charset;
 
 import org.apache.wicket.IRequestTarget;
 import org.apache.wicket.RequestCycle;
@@ -37,8 +38,11 @@
 	/** the string for the response. */
 	private final String string;
 
-	/** content type for the string */
-	private final String contentType;
+	/** mime type for the string */
+  private final String mimeType;
+  
+  /** charset of the string */
+  private final Charset charset;
 
 	/**
 	 * Constructor
@@ -48,30 +52,51 @@
 	 */
 	public StringRequestTarget(String string)
 	{
-		this("text/plain", string);
+		this("text/plain", string, Charset.defaultCharset());
 	}
+  
+  /**
+	 * Constructor
+	 * 
+	 * @param mimeType
+	 *            mime type of the data the string represents, e.g.
+	 *            <code>text/html</code>
+	 * @param string
+	 *            string for the response
+	 */
+	public StringRequestTarget(String mimeType, String string)
+  {
+    this(mimeType, string, Charset.defaultCharset());
+  }
 
 	/**
 	 * Constructor
 	 * 
-	 * @param contentType
-	 *            content type of the data the string represents eg
-	 *            <code>text/html; charset=utf-8</code>
+	 * @param mimeType
+	 *            mime type of the data the string represents, e.g.
+	 *            <code>text/html</code>
 	 * @param string
 	 *            string for the response
+     * @param the Charset to be used in the response , e.g.
+     *             <code>Charset.forName("UTF-8")</code>
 	 */
-	public StringRequestTarget(String contentType, String string)
+	public StringRequestTarget(String mimeType, String string, Charset charset)
 	{
 		if (string == null)
 		{
 			throw new IllegalArgumentException("Argument string must be not null");
 		}
-		if (Strings.isEmpty(contentType))
+		if (Strings.isEmpty(mimeType))
 		{
-			throw new IllegalArgumentException("Argument contentType must not be null or empty");
+			throw new IllegalArgumentException("Argument mimeType must not be null or empty");
 		}
-		this.contentType = contentType;
+    if (charset == null)
+		{
+			throw new IllegalArgumentException("Argument charset must not be null");
+		}
+		this.mimeType = mimeType;
 		this.string = string;
+    this.charset = charset;
 	}
 
 
@@ -83,29 +108,30 @@
 	public void respond(RequestCycle requestCycle)
 	{
 		// Get servlet response to use when responding with resource
-		final Response response = requestCycle.getResponse();
-		response.setContentType(contentType);
-		final StringBufferResourceStream stream = new StringBufferResourceStream(contentType);
-		stream.append(string);
+    final Response response = requestCycle.getResponse();
+    final StringBufferResourceStream stream = new StringBufferResourceStream(mimeType);
+    stream.setCharset(charset);
+    response.setContentType(mimeType + ";charset=" + charset.name());
+    stream.append(string);
 
-		// Respond with resource
-		try
-		{
-			final OutputStream out = response.getOutputStream();
-			try
-			{
-				Streams.copy(stream.getInputStream(), out);
-			}
-			finally
-			{
-				stream.close();
-				out.flush();
-			}
-		}
-		catch (Exception e)
-		{
-			throw new WicketRuntimeException("Unable to render resource stream " + stream, e);
-		}
+    // Respond with resource
+    try
+    {
+      final OutputStream out = response.getOutputStream();
+      try
+      {
+        Streams.copy(stream.getInputStream(), out);
+      }
+      finally
+      {
+        stream.close();
+        out.flush();
+      }
+    }
+    catch (Exception e)
+    {
+      throw new WicketRuntimeException("Unable to render resource stream " + stream, e);
+    }
 	}
 
 	/**



-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Resolved: (WICKET-1805) Allow to change charset in StringRequestTarget: change CharSet used by the OutStream as well

Posted by "Igor Vaynberg (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/WICKET-1805?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Igor Vaynberg resolved WICKET-1805.
-----------------------------------

       Resolution: Fixed
    Fix Version/s: 1.4-M4
                   1.3.5
         Assignee: Igor Vaynberg

> Allow to change charset in StringRequestTarget: change CharSet used by the OutStream as well
> --------------------------------------------------------------------------------------------
>
>                 Key: WICKET-1805
>                 URL: https://issues.apache.org/jira/browse/WICKET-1805
>             Project: Wicket
>          Issue Type: Sub-task
>          Components: wicket
>    Affects Versions: 1.3.4, 1.4-M3
>         Environment: Java 5 on Mac and Win, using FF2/3 and IE7
>            Reporter: Niels Boeger
>            Assignee: Igor Vaynberg
>             Fix For: 1.3.5, 1.4-M4
>
>
> the CharSet is provided as meta info only, the actual CharSet used by the OutStream is not changed. This leads to data being transferred in a different encoding than specified by the ContentType meta data.
> I suggest using another constructor:
> Index: StringRequestTarget.java
> ===================================================================
> --- StringRequestTarget.java	(revision 689726)
> +++ StringRequestTarget.java	(working copy)
> @@ -17,6 +17,7 @@
>  package org.apache.wicket.request.target.basic;
>  
>  import java.io.OutputStream;
> +import java.nio.charset.Charset;
>  
>  import org.apache.wicket.IRequestTarget;
>  import org.apache.wicket.RequestCycle;
> @@ -37,8 +38,11 @@
>  	/** the string for the response. */
>  	private final String string;
>  
> -	/** content type for the string */
> -	private final String contentType;
> +	/** mime type for the string */
> +  private final String mimeType;
> +  
> +  /** charset of the string */
> +  private final Charset charset;
>  
>  	/**
>  	 * Constructor
> @@ -48,30 +52,51 @@
>  	 */
>  	public StringRequestTarget(String string)
>  	{
> -		this("text/plain", string);
> +		this("text/plain", string, Charset.defaultCharset());
>  	}
> +  
> +  /**
> +	 * Constructor
> +	 * 
> +	 * @param mimeType
> +	 *            mime type of the data the string represents, e.g.
> +	 *            <code>text/html</code>
> +	 * @param string
> +	 *            string for the response
> +	 */
> +	public StringRequestTarget(String mimeType, String string)
> +  {
> +    this(mimeType, string, Charset.defaultCharset());
> +  }
>  
>  	/**
>  	 * Constructor
>  	 * 
> -	 * @param contentType
> -	 *            content type of the data the string represents eg
> -	 *            <code>text/html; charset=utf-8</code>
> +	 * @param mimeType
> +	 *            mime type of the data the string represents, e.g.
> +	 *            <code>text/html</code>
>  	 * @param string
>  	 *            string for the response
> +     * @param the Charset to be used in the response , e.g.
> +     *             <code>Charset.forName("UTF-8")</code>
>  	 */
> -	public StringRequestTarget(String contentType, String string)
> +	public StringRequestTarget(String mimeType, String string, Charset charset)
>  	{
>  		if (string == null)
>  		{
>  			throw new IllegalArgumentException("Argument string must be not null");
>  		}
> -		if (Strings.isEmpty(contentType))
> +		if (Strings.isEmpty(mimeType))
>  		{
> -			throw new IllegalArgumentException("Argument contentType must not be null or empty");
> +			throw new IllegalArgumentException("Argument mimeType must not be null or empty");
>  		}
> -		this.contentType = contentType;
> +    if (charset == null)
> +		{
> +			throw new IllegalArgumentException("Argument charset must not be null");
> +		}
> +		this.mimeType = mimeType;
>  		this.string = string;
> +    this.charset = charset;
>  	}
>  
>  
> @@ -83,29 +108,30 @@
>  	public void respond(RequestCycle requestCycle)
>  	{
>  		// Get servlet response to use when responding with resource
> -		final Response response = requestCycle.getResponse();
> -		response.setContentType(contentType);
> -		final StringBufferResourceStream stream = new StringBufferResourceStream(contentType);
> -		stream.append(string);
> +    final Response response = requestCycle.getResponse();
> +    final StringBufferResourceStream stream = new StringBufferResourceStream(mimeType);
> +    stream.setCharset(charset);
> +    response.setContentType(mimeType + ";charset=" + charset.name());
> +    stream.append(string);
>  
> -		// Respond with resource
> -		try
> -		{
> -			final OutputStream out = response.getOutputStream();
> -			try
> -			{
> -				Streams.copy(stream.getInputStream(), out);
> -			}
> -			finally
> -			{
> -				stream.close();
> -				out.flush();
> -			}
> -		}
> -		catch (Exception e)
> -		{
> -			throw new WicketRuntimeException("Unable to render resource stream " + stream, e);
> -		}
> +    // Respond with resource
> +    try
> +    {
> +      final OutputStream out = response.getOutputStream();
> +      try
> +      {
> +        Streams.copy(stream.getInputStream(), out);
> +      }
> +      finally
> +      {
> +        stream.close();
> +        out.flush();
> +      }
> +    }
> +    catch (Exception e)
> +    {
> +      throw new WicketRuntimeException("Unable to render resource stream " + stream, e);
> +    }
>  	}
>  
>  	/**

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.