/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.wicket.request.target.basic;

import java.io.OutputStream;

import org.apache.wicket.IRequestTarget;
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;


/**
 * Request target that responds by sending its string property.
 * 
 * @author Eelco Hillenius
 */
public class StringRequestTarget implements IRequestTarget
{
	/** the string for the response. */
	private final String string;

	/**
	 * Construct.
	 * 
	 * @param string
	 *            the string for the response
	 */
	public StringRequestTarget(String string)
	{
		if (string == null)
		{
			throw new IllegalArgumentException("Argument string must be not null");
		}

		this.string = string;
	}

	/**
	 * Responds by sending the string property.
	 * 
	 * @see org.apache.wicket.IRequestTarget#respond(org.apache.wicket.RequestCycle)
	 */
	public void respond(RequestCycle requestCycle)
	{
		// Get servlet response to use when responding with resource
		final Response response = requestCycle.getResponse();
		final StringBufferResourceStream stream = new StringBufferResourceStream();
		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);
		}
	}

	/**
	 * @see org.apache.wicket.IRequestTarget#detach(org.apache.wicket.RequestCycle)
	 */
	public void detach(RequestCycle requestCycle)
	{
	}

	/**
	 * Gets the string property.
	 * 
	 * @return the string property
	 */
	public String getString()
	{
		return string;
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object obj)
	{
		if (obj instanceof StringRequestTarget)
		{
			StringRequestTarget that = (StringRequestTarget)obj;
			return string.equals(that.string);
		}
		return false;
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode()
	{
		int result = "StringRequestTarget".hashCode();
		result += string.hashCode();
		return 17 * result;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		return "[StringRequestTarget@" + hashCode() + " " + string + "]";
	}
}

@@ -1148,16 +1148,62 @@
 Stream;%0A
+import org.apache.wicket.util.string.Strings;%0A
 %0A%0A/**%0A *
@@ -1428,16 +1428,88 @@
 tring;%0A%0A
+%09/** content type for the string */%0A%09private final String contentType;%0A%0A
 %09/**%0A%09 *
@@ -1518,17 +1518,18 @@
 onstruct
-.
+or
 %0A%09 * %0A%09 
@@ -1644,111 +1644,600 @@
 %7B%0A%09%09
-if (string == null)%0A%09%09%7B%0A%09%09%09throw new IllegalArgumentException(%22Argument string must be not null%22);%0A%09%09%7D%0A
+this(%22text%22, string);%0A%09%7D%0A%0A%09/**%0A%09 * Constructor%0A%09 * %0A%09 * @param contentType%0A%09 *            content type of the data the string represents eg%0A%09 *            %3Ccode%3Etext/html; charset=utf-8%3C/code%3E%0A%09 * @param string%0A%09 *            string for the response%0A%09 */%0A%09public StringRequestTarget(String contentType, String string)%0A%09%7B%0A%09%09if (string == null)%0A%09%09%7B%0A%09%09%09throw new IllegalArgumentException(%22Argument string must be not null%22);%0A%09%09%7D%0A%09%09if (Strings.isEmpty(contentType))%0A%09%09%7B%0A%09%09%09throw new IllegalArgumentException(%22Argument contentType must not be null or empty%22);%0A%09%09%7D%0A%09%09this.contentType = contentType;
 %0A%09%09t
@@ -2253,32 +2253,33 @@
 g = string;%0A%09%7D%0A%0A
+%0A
 %09/**%0A%09 * Respond
@@ -2645,24 +2645,35 @@
 ourceStream(
+contentType
 );%0A%09%09stream.

allow custom content types for string request target