Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.protocol.http;
 
 
 
Implements responses over the HTTP protocol by holding an underlying HttpServletResponse object and providing convenience methods for using that object. Convenience methods include methods which: add a cookie, close the stream, encode a URL, redirect a request to another resource, determine if a redirect has been issued, set the content type, set the locale and, most importantly, write a String to the response output.

Author(s):
Jonathan Locke
 
 public class WebResponse extends Response
 {
Log.
 
 	private static final Logger log = LoggerFactory.getLogger(WebResponse.class);

True if response is a redirect.
 
 	protected boolean redirect;

The underlying response object.
 
Is the request an ajax request?
 
 	private boolean ajax;

Constructor for testing harness.
 
 	public WebResponse()
 	{
 	}

Package private constructor.

Parameters:
httpServletResponse The servlet response object
 
 	public WebResponse(final HttpServletResponse httpServletResponse)
 	{
 		this. = httpServletResponse;
 	}

Add a cookie to the web response

Parameters:
cookie
 
 	public void addCookie(final Cookie cookie)
 	{
 		if ( != null)
 		{
 		}
 	}

Convenience method for clearing a cookie.

Parameters:
cookie The cookie to set
See also:
addCookie(javax.servlet.http.Cookie)
 
 	public void clearCookie(final Cookie cookie)
 	{
 		if ( != null)
		{
			cookie.setMaxAge(0);
			cookie.setValue(null);
			addCookie(cookie);
		}
	}

Closes response output.
	public void close()
	{
		// NOTE: Servlet container will close the response output stream
		// automatically, so we do nothing here.
	}

Returns the given url encoded.

Parameters:
url The URL to encode
Returns:
The encoded url
	{
		if ( != null && url != null)
		{
			if (url.length() > 0 && url.charAt(0) == '?')
			{
				// there is a bug in apache tomcat 5.5 where tomcat doesn't put sessionid to url
				// when the URL starts with '?'. So we prepend the URL with ./ and remove it
				// afterwards (unless some container prepends session id before './' or mangles
				// the URL otherwise
				String encoded = .encodeURL("./" + url.toString());
				if (encoded.startsWith("./"))
				{
					return encoded.substring(2);
				}
				else
				{
					return encoded;
				}
			}
			else
			{
			}
		}
		return url;
	}

Gets the wrapped http servlet response object.

Returns:
The wrapped http servlet response object
	{
	}

	{
		try
		{
		}
		catch (IOException e)
		{
			throw new WicketRuntimeException("Error while getting output stream."e);
		}
	}

Whether this response is going to redirect the user agent.

Returns:
True if this response is going to redirect the user agent
	public final boolean isRedirect()
	{
		return ;
	}

CLIENTS SHOULD NEVER CALL THIS METHOD FOR DAY TO DAY USE!

Redirects to the given url. Implementations should encode the URL to make sure cookie-less operation is supported in case clients forgot.

Parameters:
url The URL to redirect to
	public void redirect(String url)
	{
		if (!)
		{
			if ( != null)
			{
				// encode to make sure no caller forgot this
				try
				{
					{
						.error("Unable to redirect to: " + url +
								", HTTP Response has already been committed.");
					}
					{
						.debug("Redirecting to " + url);
					}
					if (isAjax())
					{
						/*
						 * By reaching this point, make sure the HTTP response status code is set to
						 * 200, otherwise wicket-ajax.js will not process the Ajax-Location header
						 */
						.addHeader("Ajax-Location"url);
						// safari chokes on empty response. but perhaps this is
						// not the best place?
					}
					else
					{
					}
					 = true;
				}
				catch (IOException e)
				{
					.warn("redirect to " + url + " failed: " + e.getMessage());
				}
			}
		}
		else
		{
			.info("Already redirecting to an url current one ignored: " + url);
		}
	}

Set the content type on the response.

Parameters:
mimeType The mime type
	public final void setContentType(final String mimeType)
	{
		if ( != null)
		{
		}
	}

	public void setContentLength(long length)
	{
		if ( != null)
		{
		}
	}

	public void setLastModifiedTime(Time time)
	{
		if ( != null)
		{
			if (time != null && time.getMilliseconds() != -1)
			{
			}
		}
	}

Output stream encoding. If the deployment descriptor contains a locale-encoding-mapping-list element, and that element provides a mapping for the given locale, that mapping is used. Otherwise, the mapping from locale to character encoding is container dependent. Default is ISO-8859-1.

Parameters:
locale The locale use for mapping the character encoding
See also:
javax.servlet.ServletResponse.setLocale(java.util.Locale)
	public final void setLocale(final Locale locale)
	{
		if ( != null)
		{
		}
	}

Writes string to response output.

Parameters:
string The string to write
	public void write(final CharSequence string)
	{
		if (string instanceof AppendingStringBuffer)
		{
		}
		else if (string instanceof StringBuffer)
		{
			try
			{
				StringBuffer sb = (StringBuffer)string;
				char[] array = new char[sb.length()];
				sb.getChars(0, sb.length(), array, 0);
				.getWriter().write(array, 0, array.length);
			}
			catch (IOException e)
			{
				throw new WicketRuntimeException("Error while writing to servlet output writer."e);
			}
		}
		else
		{
			try
			{
			}
			catch (IOException e)
			{
				throw new WicketRuntimeException("Error while writing to servlet output writer."e);
			}
		}
	}

Writes AppendingStringBuffer to response output.

Parameters:
asb The AppendingStringBuffer to write to the stream
	public void write(AppendingStringBuffer asb)
	{
		try
		{
		}
		catch (IOException e)
		{
			throw new WicketRuntimeException("Error while writing to servlet output writer."e);
		}
	}

Set a header to the date value in the servlet response stream.

Parameters:
header
date
	public void setDateHeader(String headerlong date)
	{
		if ( != null)
		{
		}
	}


Set a header to the string value in the servlet response stream.

Parameters:
header
value
	public void setHeader(String headerString value)
	{
		if ( != null)
		{
		}
	}

Convenience method for setting the content-disposition:attachment header. This header is used if the response should prompt the user to download it as a file instead of opening in a browser.

Parameters:
filename file name of the attachment
	public void setAttachmentHeader(String filename)
	{
		setHeader("Content-Disposition""attachment" +
				((!Strings.isEmpty(filename)) ? ("; filename=\"" + filename + "\"") : ""));
	}

Is the request, which matches this response an ajax request.

Returns:
True if the request is an ajax request.
	public boolean isAjax()
	{
		return ;
	}

Set that the request which matches this response is an ajax request.

Parameters:
ajax True if the request is an ajax request.
	public void setAjax(boolean ajax)
	{
		this. = ajax;
	}
New to GrepCode? Check out our FAQ X