📄 coyoteresponse.java
字号:
/** * Set the error flag. */ public void setError() { error = true; } /** * Error flag accessor. */ public boolean isError() { return error; } /** * Create and return a ServletOutputStream to write the content * associated with this Response. * * @exception IOException if an input/output error occurs */ public ServletOutputStream createOutputStream() throws IOException { // Probably useless return outputStream; } /** * Perform whatever actions are required to flush and close the output * stream or writer, in a single operation. * * @exception IOException if an input/output error occurs */ public void finishResponse() throws IOException { // Writing leftover bytes try { outputBuffer.close(); } catch(IOException e) { ; } catch(Throwable t) { t.printStackTrace(); } } /** * Return the content length that was set or calculated for this Response. */ public int getContentLength() { return (coyoteResponse.getContentLength()); } /** * Return the content type that was set or calculated for this response, * or <code>null</code> if no content type was set. */ public String getContentType() { return (coyoteResponse.getContentType()); } /** * Return a PrintWriter that can be used to render error messages, * regardless of whether a stream or writer has already been acquired. * * @return Writer which can be used for error reports. If the response is * not an error report returned using sendError or triggered by an * unexpected exception thrown during the servlet processing * (and only in that case), null will be returned if the response stream * has already been used. */ public PrintWriter getReporter() { if (outputBuffer.isNew()) { return writer; } else { return null; } } // ------------------------------------------------ ServletResponse Methods /** * Flush the buffer and commit this response. * * @exception IOException if an input/output error occurs */ public void flushBuffer() throws IOException { outputBuffer.flush(); } /** * Return the actual buffer size used for this Response. */ public int getBufferSize() { return outputBuffer.getBufferSize(); } /** * Return the character encoding used for this Response. */ public String getCharacterEncoding() { return (coyoteResponse.getCharacterEncoding()); } /** * Return the servlet output stream associated with this Response. * * @exception IllegalStateException if <code>getWriter</code> has * already been called for this response * @exception IOException if an input/output error occurs */ public ServletOutputStream getOutputStream() throws IOException { if (usingWriter) throw new IllegalStateException (sm.getString("coyoteResponse.getOutputStream.ise")); usingOutputStream = true; return outputStream; } /** * Return the Locale assigned to this response. */ public Locale getLocale() { return (coyoteResponse.getLocale()); } /** * Return the writer associated with this Response. * * @exception IllegalStateException if <code>getOutputStream</code> has * already been called for this response * @exception IOException if an input/output error occurs */ public PrintWriter getWriter() throws IOException { if (usingOutputStream) throw new IllegalStateException (sm.getString("coyoteResponse.getWriter.ise")); usingWriter = true; return writer; } /** * Has the output of this response already been committed? */ public boolean isCommitted() { return (coyoteResponse.isCommitted()); } /** * Clear any content written to the buffer. * * @exception IllegalStateException if this response has already * been committed */ public void reset() { if (included) return; // Ignore any call from an included servlet coyoteResponse.reset(); outputBuffer.reset(); } /** * Reset the data buffer but not any status or header information. * * @exception IllegalStateException if the response has already * been committed */ public void resetBuffer() { if (isCommitted()) throw new IllegalStateException (sm.getString("coyoteResponse.resetBuffer.ise")); outputBuffer.reset(); } /** * Set the buffer size to be used for this Response. * * @param size The new buffer size * * @exception IllegalStateException if this method is called after * output has been committed for this response */ public void setBufferSize(int size) { if (isCommitted() || !outputBuffer.isNew()) throw new IllegalStateException (sm.getString("coyoteResponse.setBufferSize.ise")); outputBuffer.setBufferSize(size); } /** * Set the content length (in bytes) for this Response. * * @param length The new content length */ public void setContentLength(int length) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; coyoteResponse.setContentLength(length); } /** * Set the content type for this Response. * * @param type The new content type */ public void setContentType(String type) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; coyoteResponse.setContentType(type); } /** * Set the Locale that is appropriate for this response, including * setting the appropriate character encoding. * * @param locale The new locale */ public void setLocale(Locale locale) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; coyoteResponse.setLocale(locale); // Set the specified locale's default encoding of a response CharsetMapper cm = context.getCharsetMapper(); String charset = cm.getCharset(locale); if (charset != null) { coyoteResponse.setCharacterEncoding(charset); } } // --------------------------------------------------- HttpResponse Methods /** * Return an array of all cookies set for this response, or * a zero-length array if no cookies have been set. */ public Cookie[] getCookies() { return ((Cookie[]) cookies.toArray(new Cookie[cookies.size()])); } /** * Return the value for the specified header, or <code>null</code> if this * header has not been set. If more than one value was added for this * name, only the first is returned; use getHeaderValues() to retrieve all * of them. * * @param name Header name to look up */ public String getHeader(String name) { return coyoteResponse.getMimeHeaders().getHeader(name); } /** * Return an array of all the header names set for this response, or * a zero-length array if no headers have been set. */ public String[] getHeaderNames() { MimeHeaders headers = coyoteResponse.getMimeHeaders(); int n = headers.size(); String[] result = new String[n]; for (int i = 0; i < n; i++) { result[i] = headers.getName(i).toString(); } return result; } /** * Return an array of all the header values associated with the * specified header name, or an zero-length array if there are no such * header values. * * @param name Header name to look up */ public String[] getHeaderValues(String name) { Enumeration enum = coyoteResponse.getMimeHeaders().values(name); Vector result = new Vector(); while (enum.hasMoreElements()) { result.addElement(enum.nextElement()); } String[] resultArray = new String[result.size()]; result.copyInto(resultArray); return resultArray; } /** * Return the error message that was set with <code>sendError()</code> * for this Response. */ public String getMessage() { return coyoteResponse.getMessage(); } /** * Return the HTTP status code associated with this Response. */ public int getStatus() { return coyoteResponse.getStatus(); } /** * Reset this response, and specify the values for the HTTP status code * and corresponding message. * * @exception IllegalStateException if this response has already been * committed */ public void reset(int status, String message) { reset(); setStatus(status, message); } // -------------------------------------------- HttpServletResponse Methods /** * Add the specified Cookie to those that will be included with * this Response. * * @param cookie Cookie to be added */ public void addCookie(Cookie cookie) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; cookies.add(cookie); StringBuffer sb = new StringBuffer(); ServerCookie.appendCookieValue (sb, cookie.getVersion(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getDomain(), cookie.getComment(), cookie.getMaxAge(), cookie.getSecure()); // the header name is Set-Cookie for both "old" and v.1 ( RFC2109 ) // RFC2965 is not supported by browsers and the Servlet spec // asks for 2109. addHeader("Set-Cookie", sb.toString()); } /** * Add the specified date header to the specified value. * * @param name Name of the header to set * @param value Date value to be set */ public void addDateHeader(String name, long value) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; addHeader(name, format.format(new Date(value))); } /** * Add the specified header to the specified value. * * @param name Name of the header to set * @param value Value to be set */ public void addHeader(String name, String value) { if (isCommitted()) return; // Ignore any call from an included servlet if (included) return; coyoteResponse.addHeader(name, value); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -