You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by eh...@apache.org on 2008/10/08 23:56:22 UTC

svn commit: r703005 - /wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/basic/StringRequestTarget.java

Author: ehillenius
Date: Wed Oct  8 14:56:22 2008
New Revision: 703005

URL: http://svn.apache.org/viewvc?rev=703005&view=rev
Log:
WICKET-1810

Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/basic/StringRequestTarget.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/basic/StringRequestTarget.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/basic/StringRequestTarget.java?rev=703005&r1=703004&r2=703005&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/basic/StringRequestTarget.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/request/target/basic/StringRequestTarget.java Wed Oct  8 14:56:22 2008
@@ -16,7 +16,7 @@
  */
 package org.apache.wicket.request.target.basic;
 
-import java.io.OutputStream;
+import java.io.IOException;
 import java.nio.charset.Charset;
 
 import org.apache.wicket.Application;
@@ -24,8 +24,6 @@
 import org.apache.wicket.RequestCycle;
 import org.apache.wicket.Response;
 import org.apache.wicket.WicketRuntimeException;
-import org.apache.wicket.util.io.Streams;
-import org.apache.wicket.util.resource.StringBufferResourceStream;
 import org.apache.wicket.util.string.Strings;
 
 
@@ -43,19 +41,19 @@
 	private final String contentType;
 
 	/** charset of the string */
-	private final Charset charset;
+	private final String encoding;
 
 
 	/**
-	 * Creates a string request target with content type <code>text/plain</code> and default charset
-	 * (usually UTF-8)
+	 * Creates a string request target with content type <code>text/plain</code> and default
+	 * charset (usually UTF-8)
 	 * 
 	 * @param string
 	 *            the string for the response
 	 */
 	public StringRequestTarget(String string)
 	{
-		this("text/plain", getDefaultCharset(), string);
+		this("text/plain", getDefaultEncoding(), string);
 	}
 
 	/**
@@ -64,12 +62,12 @@
 	 * @param contentType
 	 *            content type of the data the string represents eg
 	 *            <code>text/html; charset=utf-8</code>
-	 * @param charset
+	 * @param encoding
 	 *            charset to use
 	 * @param string
 	 *            string for the response
 	 */
-	public StringRequestTarget(String contentType, Charset charset, String string)
+	public StringRequestTarget(String contentType, String encoding, String string)
 	{
 		if (string == null)
 		{
@@ -79,13 +77,31 @@
 		{
 			throw new IllegalArgumentException("Argument contentType must not be null or empty");
 		}
-		if (charset == null)
+		if (encoding == null)
 		{
 			throw new IllegalArgumentException("Argument charset must not be null");
 		}
 		this.contentType = contentType;
 		this.string = string;
-		this.charset = charset;
+		this.encoding = encoding;
+	}
+
+	/**
+	 * Constructor
+	 * 
+	 * @param contentType
+	 *            content type of the data the string represents eg
+	 *            <code>text/html; charset=utf-8</code>
+	 * @param charset
+	 *            charset to use
+	 * @param string
+	 *            string for the response
+	 * @deprecated use {@link #StringRequestTarget(String, String, String)} instead
+	 */
+	@Deprecated
+	public StringRequestTarget(String contentType, Charset charset, String string)
+	{
+		this(contentType, charset.aliases().iterator().next(), string);
 	}
 
 	/**
@@ -93,13 +109,9 @@
 	 * 
 	 * @return charset
 	 */
-	private static Charset getDefaultCharset()
+	private static String getDefaultEncoding()
 	{
-		final String charsetName = Application.get()
-			.getRequestCycleSettings()
-			.getResponseRequestEncoding();
-
-		return Charset.forName(charsetName);
+		return Application.get().getRequestCycleSettings().getResponseRequestEncoding();
 	}
 
 
@@ -112,28 +124,16 @@
 	{
 		// Get servlet response to use when responding with resource
 		final Response response = requestCycle.getResponse();
-		response.setContentType(contentType);
-		final StringBufferResourceStream stream = new StringBufferResourceStream(contentType);
-		stream.setCharset(charset);
-		stream.append(string);
+		response.setContentType(contentType + ";charset=" + encoding);
 
-		// Respond with resource
+		// send string to client
 		try
 		{
-			final OutputStream out = response.getOutputStream();
-			try
-			{
-				Streams.copy(stream.getInputStream(), out);
-			}
-			finally
-			{
-				stream.close();
-				out.flush();
-			}
+			response.getOutputStream().write(string.getBytes(encoding));
 		}
-		catch (Exception e)
+		catch (IOException e)
 		{
-			throw new WicketRuntimeException("Unable to render resource stream " + stream, e);
+			throw new WicketRuntimeException("Unable to render string: " + e.getMessage(), e);
 		}
 	}