📄 mockhttpservletresponse.java
字号:
public void initialize() { cookies.clear(); headers.clear(); code = HttpServletResponse.SC_OK; errorMessage = null; redirectLocation = null; status = HttpServletResponse.SC_OK; characterEncoding = "UTF-8"; locale = null; byteStream = new ByteArrayOutputStream(); servletStream = new ServletOutputStream() { public void write(int b) { byteStream.write(b); } }; stringWriter = new StringWriter(); printWriter = new PrintWriter(stringWriter) { public void close() { // Do nothing } public void flush() { // Do nothing } }; mode = MODE_NONE; } /** * Always returns false. * * @return Always false */ public boolean isCommitted() { return false; } /** * Return whether the servlet returned an error code or not. * * @return Whether an error occurred or not */ public boolean isError() { return (code != HttpServletResponse.SC_OK); } /** * Check whether the response was redirected or not. * * @return Whether the state was redirected or not */ public boolean isRedirect() { return (redirectLocation != null); } /** * Delegate to initialize method. */ public void reset() { initialize(); } /** * Clears the buffer. */ public void resetBuffer() { if (mode == MODE_BINARY) { byteStream.reset(); } else if (mode == MODE_TEXT) { stringWriter.getBuffer().delete(0, stringWriter.getBuffer().length()); } } /** * Send an error code. This implementation just sets the internal error state information. * * @param code * The code * @throws IOException * Not used */ public void sendError(final int code) throws IOException { this.code = code; errorMessage = null; } /** * Send an error code. This implementation just sets the internal error state information. * * @param code * The error code * @param msg * The error message * @throws IOException * Not used */ public void sendError(final int code, final String msg) throws IOException { this.code = code; errorMessage = msg; } /** * @see org.apache.wicket.Request#getURL() */ private String getURL() { /* * Servlet 2.3 specification : * * Servlet Path: The path section that directly corresponds to the mapping which activated * this request. This path starts with a "/" character except in the case where the request * is matched with the "/*" pattern, in which case it is the empty string. * * PathInfo: The part of the request path that is not part of the Context Path or the * Servlet Path. It is either null if there is no extra path, or is a string with a leading * "/". */ String url = servletRequest.getServletPath(); final String pathInfo = servletRequest.getPathInfo(); if (pathInfo != null) { url += pathInfo; } final String queryString = servletRequest.getQueryString(); if (queryString != null) { url += ("?" + queryString); } // If url is non-empty it will start with '/', which we should lose if (url.length() > 0 && url.charAt(0) == '/') { // Remove leading '/' url = url.substring(1); } return url; } /** * Indicate sending of a redirectLocation to a particular named resource. This implementation * just keeps hold of the redirectLocation info and makes it available for query. * * @param location * The location to redirectLocation to * @throws IOException * Not used */ public void sendRedirect(String location) throws IOException { // If the location starts with ../ if (location.startsWith("../")) { // Test if the current url has a / in it. (a mount) String url = getURL(); int index = url.lastIndexOf("/"); if (index != -1) { // Then we have to recalculate what the real redirect is for the next request // which is just getContext() + getServletPath() + "/" + location; url = url.substring(0, index + 1) + location; url = RequestUtils.removeDoubleDots(url); // stril the servlet path again from it. index = url.indexOf("/"); location = url.substring(index + 1); } } redirectLocation = location; } /** * Method ignored. * * @param size * The size */ public void setBufferSize(final int size) { } /** * Set the character encoding. * * @param characterEncoding * The character encoding */ public void setCharacterEncoding(final String characterEncoding) { this.characterEncoding = characterEncoding; } /** * Set the content length. * * @param length * The length */ public void setContentLength(final int length) { setIntHeader("Content-Length", length); } /** * Set the content type. * * @param type * The content type */ public void setContentType(final String type) { setHeader("Content-Type", type); } public String getContentType() { return getHeader("Content-Type"); } /** * Set a date header. * * @param name * The header name * @param l * The long value */ public void setDateHeader(final String name, final long l) { setHeader(name, formatDate(l)); } /** * @param l * @return */ public static String formatDate(long l) { StringBuffer _dateBuffer = new StringBuffer(32); Calendar _calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT")); _calendar.setTimeInMillis(l); formatDate(_dateBuffer, _calendar, false); return _dateBuffer.toString(); } /* BEGIN: This code comes from Jetty 6.1.1 */ private static String[] DAYS = { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; private static String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan" }; /** * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'"for * cookies * * @param buf * @param calendar * @param cookie */ public static void formatDate(StringBuffer buf, Calendar calendar, boolean cookie) { // "EEE, dd MMM yyyy HH:mm:ss 'GMT'" // "EEE, dd-MMM-yy HH:mm:ss 'GMT'", cookie int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); int day_of_month = calendar.get(Calendar.DAY_OF_MONTH); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); int century = year / 100; year = year % 100; int epoch = (int)((calendar.getTimeInMillis() / 1000) % (60 * 60 * 24)); int seconds = epoch % 60; epoch = epoch / 60; int minutes = epoch % 60; int hours = epoch / 60; buf.append(DAYS[day_of_week]); buf.append(','); buf.append(' '); append2digits(buf, day_of_month); if (cookie) { buf.append('-'); buf.append(MONTHS[month]); buf.append('-'); append2digits(buf, year); } else { buf.append(' '); buf.append(MONTHS[month]); buf.append(' '); append2digits(buf, century); append2digits(buf, year); } buf.append(' '); append2digits(buf, hours); buf.append(':'); append2digits(buf, minutes); buf.append(':'); append2digits(buf, seconds); buf.append(" GMT"); } /** * @param buf * @param i */ public static void append2digits(StringBuffer buf, int i) { if (i < 100) { buf.append((char)(i / 10 + '0')); buf.append((char)(i % 10 + '0')); } } /* END: This code comes from Jetty 6.1.1 */ /** * Set the given header value. * * @param name * The name for the header * @param value * The value for the header */ public void setHeader(final String name, final String value) { List l = new ArrayList(1); l.add(value); headers.put(name, l); } /** * Set an int header value. * * @param name * The header name * @param i * The value */ public void setIntHeader(final String name, final int i) { setHeader(name, "" + i); } /** * Set the locale in the response header. * * @param locale * The locale */ public void setLocale(final Locale locale) { this.locale = locale; } /** * Set the status for this response. * * @param status * The status */ public void setStatus(final int status) { this.status = status; } /** * Set the status for this response. * * @param status * The status * @param msg * The message * @deprecated */ public void setStatus(final int status, final String msg) { setStatus(status); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -