⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mockhttpservletresponse.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.PrintWriter;import java.io.StringWriter;import java.text.DateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Collection;import java.util.Date;import java.util.GregorianCalendar;import java.util.List;import java.util.Locale;import java.util.Set;import java.util.TimeZone;import javax.servlet.ServletOutputStream;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletResponse;import org.apache.wicket.util.value.ValueMap;/** * Mock servlet response. Implements all of the methods from the standard HttpServletResponse class * plus helper methods to aid viewing the generated response. *  * @author Chris Turner */public class MockHttpServletResponse implements HttpServletResponse{	private static final int MODE_BINARY = 1;	private static final int MODE_NONE = 0;	private static final int MODE_TEXT = 2;	private ByteArrayOutputStream byteStream;	private String characterEncoding = "UTF-8";	private int code = HttpServletResponse.SC_OK;	private final List cookies = new ArrayList();	private String errorMessage = null;	private final ValueMap headers = new ValueMap();	private Locale locale = null;	private int mode = MODE_NONE;	private PrintWriter printWriter;	private String redirectLocation = null;	private ServletOutputStream servletStream;	private int status = HttpServletResponse.SC_OK;	private StringWriter stringWriter;	private final MockHttpServletRequest servletRequest;	/**	 * Create the response object.	 * 	 * @param servletRequest	 */	public MockHttpServletResponse(MockHttpServletRequest servletRequest)	{		this.servletRequest = servletRequest;		initialize();	}	/**	 * Add a cookie to the response.	 * 	 * @param cookie	 *            The cookie to add	 */	public void addCookie(final Cookie cookie)	{		cookies.add(cookie);	}	/**	 * Add a date header.	 * 	 * @param name	 *            The header value	 * @param l	 *            The long value	 */	public void addDateHeader(String name, long l)	{		DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);		addHeader(name, df.format(new Date(l)));	}	/**	 * Add the given header value, including an additional entry if one already exists.	 * 	 * @param name	 *            The name for the header	 * @param value	 *            The value for the header	 */	public void addHeader(final String name, final String value)	{		List list = (List)headers.get(name);		if (list == null)		{			list = new ArrayList(1);			headers.put(name, list);		}		list.add(value);	}	/**	 * Add an int header value.	 * 	 * @param name	 *            The header name	 * @param i	 *            The value	 */	public void addIntHeader(final String name, final int i)	{		addHeader(name, "" + i);	}	/**	 * Check if the response contains the given header name.	 * 	 * @param name	 *            The name to check	 * @return Whether header in response or not	 */	public boolean containsHeader(final String name)	{		return headers.containsKey(name);	}	/**	 * Encode the redirectLocation URL. Does no changes as this test implementation uses cookie	 * based url tracking.	 * 	 * @param url	 *            The url to encode	 * @return The encoded url	 */	public String encodeRedirectUrl(final String url)	{		return url;	}	/**	 * Encode the redirectLocation URL. Does no changes as this test implementation uses cookie	 * based url tracking.	 * 	 * @param url	 *            The url to encode	 * @return The encoded url	 */	public String encodeRedirectURL(final String url)	{		return url;	}	/**	 * Encode the URL. Does no changes as this test implementation uses cookie based url tracking.	 * 	 * @param url	 *            The url to encode	 * @return The encoded url	 */	public String encodeUrl(final String url)	{		return url;	}	/**	 * Encode the URL. Does no changes as this test implementation uses cookie based url tracking.	 * 	 * @param url	 *            The url to encode	 * @return The encoded url	 */	public String encodeURL(final String url)	{		return url;	}	/**	 * Flush the buffer.	 * 	 * @throws IOException	 */	public void flushBuffer() throws IOException	{	}	/**	 * Get the binary content that was written to the servlet stream.	 * 	 * @return The binary content	 */	public byte[] getBinaryContent()	{		return byteStream.toByteArray();	}	/**	 * Return the current buffer size	 * 	 * @return The buffer size	 */	public int getBufferSize()	{		if (mode == MODE_NONE)		{			return 0;		}		else if (mode == MODE_BINARY)		{			return byteStream.size();		}		else		{			return stringWriter.getBuffer().length();		}	}	/**	 * Get the character encoding of the response.	 * 	 * @return The character encoding	 */	public String getCharacterEncoding()	{		return characterEncoding;	}	/**	 * Get the response code for this request.	 * 	 * @return The response code	 */	public int getCode()	{		return code;	}	/**	 * Get all of the cookies that have been added to the response.	 * 	 * @return The collection of cookies	 */	public Collection getCookies()	{		return cookies;	}	/**	 * Get the text document that was written as part of this response.	 * 	 * @return The document	 */	public String getDocument()	{		if (mode == MODE_BINARY)		{			return new String(byteStream.toByteArray());		}		else		{			return stringWriter.getBuffer().toString();		}	}	/**	 * Get the error message.	 * 	 * @return The error message, or null if no message	 */	public String getErrorMessage()	{		return errorMessage;	}	/**	 * Return the value of the given named header.	 * 	 * @param name	 *            The header name	 * @return The value, or null	 */	public String getHeader(final String name)	{		List l = (List)headers.get(name);		if (l == null || l.size() < 1)		{			return null;		}		else		{			return (String)l.get(0);		}	}	/**	 * Get the names of all of the headers.	 * 	 * @return The header names	 */	public Set getHeaderNames()	{		return headers.keySet();	}	/**	 * Get the encoded locale	 * 	 * @return The locale	 */	public Locale getLocale()	{		return locale;	}	/**	 * Get the output stream for writing binary data from the servlet.	 * 	 * @return The binary output stream.	 */	public ServletOutputStream getOutputStream()	{		if (mode == MODE_TEXT)		{			throw new IllegalArgumentException("Can't write binary after already selecting text");		}		mode = MODE_BINARY;		return servletStream;	}	/**	 * Get the location that was redirected to.	 * 	 * @return The redirect location, or null if not a redirect	 */	public String getRedirectLocation()	{		return redirectLocation;	}	/**	 * Get the status code.	 * 	 * @return The status code	 */	public int getStatus()	{		return status;	}	/**	 * Get the print writer for writing text output for this response.	 * 	 * @return The writer	 * @throws IOException	 *             Not used	 */	public PrintWriter getWriter() throws IOException	{		if (mode == MODE_BINARY)		{			throw new IllegalArgumentException("Can't write text after already selecting binary");		}		mode = MODE_TEXT;		return printWriter;	}	/**	 * Reset the response ready for reuse.	 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -