/*
 * 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.handler;

import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.wicket.request.IRequestCycle;
import org.apache.wicket.request.IRequestHandler;
import org.apache.wicket.request.http.WebResponse;
import org.apache.wicket.util.string.Strings;


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

	/** content type for the string */
	private final String contentType;

	/** charset of the string */
	private final String encoding;


	/**
	 * 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 TextRequestHandler(final String string)
	{
		this("text/plain", null, string);
	}

	/**
	 * Constructor
	 * 
	 * @param contentType
	 *            content type of the data the string represents eg
	 *            <code>text/html; charset=utf-8</code>
	 * @param encoding
	 *            charset to use
	 * @param string
	 *            string for the response
	 */
	public TextRequestHandler(final String contentType, final String encoding, final String string)
	{
		if (string == null)
		{
			throw new IllegalArgumentException("Argument string must be not null");
		}
		if (Strings.isEmpty(contentType))
		{
			throw new IllegalArgumentException("Argument contentType must not be null or empty");
		}

		this.contentType = contentType;
		this.string = string;
		this.encoding = encoding;
	}


	/**
	 * Responds by sending the string property.
	 * 
	 * @see org.apache.wicket.request.IRequestHandler#respond(org.apache.wicket.request.IRequestCycle)
	 */
	public void respond(final IRequestCycle requestCycle)
	{
		String encoding = getEncoding(requestCycle);

		// Get servlet response to use when responding with resource
		final WebResponse response = (WebResponse)requestCycle.getResponse();
		response.setContentType(contentType + ";charset=" + encoding);

		// send string to client
		try
		{
			response.write(string.getBytes(encoding));
		}
		catch (IOException e)
		{
			throw new RuntimeException("Unable to render string: " + e.getMessage(), e);
		}
	}

	/**
	 * @param requestCycle
	 * @return the configured encoding or the request's one as default
	 */
	private String getEncoding(final IRequestCycle requestCycle)
	{
		String encoding = this.encoding;
		if (Strings.isEmpty(encoding))
		{
			Charset charset = requestCycle.getRequest().getCharset();
			if (charset != null)
			{
				encoding = charset.name();
			}
		}
		return encoding;
	}

	/** {@inheritDoc} */
	public void detach(final IRequestCycle requestCycle)
	{
	}

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

}

@@ -1051,16 +1051,57 @@
 sponse;%0A
+import org.apache.wicket.util.lang.Args;%0A
 import o
@@ -1932,11 +1932,14 @@
 ents
+,
  e
-g
+.g.
 %0A%09 *
@@ -2206,301 +2206,116 @@
 %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%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%0A%09%09this.contentType = contentType;%0A%09%09this.
+this.contentType = Args.notEmpty(contentType, %22contentType%22);%0A%09%09this.string = Args.notNull(
 string
- = 
+, %22
 string
+%22)
 ;%0A%09%09

Use Args to make the argument verification.