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

📄 responseimpl.java

📁 低版本的tomcat 对于有些老版本的应用还真的需要老版的中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /** Either implement ServletOutputStream or return BufferedServletOutputStream(this)
	and implement doWrite();
     */
    public BufferedServletOutputStream getBufferedOutputStream() {
	return out;
    }
    

    // -------------------- Headers --------------------
    public MimeHeaders getMimeHeaders() {
	return headers;
    }


    public void setHeader(String name, String value) {
        if( isIncluded() ) return;      // We are in an included sub-request
	if( ! checkSpecialHeader(name, value) ) 
	    headers.putHeader(name, value);
    }

    public void addHeader(String name, String value) {
        if( isIncluded() ) return;      // We are in an included sub-request
	if( ! checkSpecialHeader(name, value) ) 
	    headers.addHeader(name, value);
    }

    
    /** Set internal fields for special header names. Called from set/addHeader.
	Return true if the header is special, no need to set the header.
     */
    private boolean checkSpecialHeader( String name, String value) {
	// XXX Eliminate redundant fields !!!
	// ( both header and in special fields )
	if( name.equalsIgnoreCase( "Content-Type" ) ) {
	    setContentType( value );
	    return true;
	}
	if( name.equalsIgnoreCase( "Content-Length" ) ) {
	    try {
		int cL=Integer.parseInt( value );
		setContentLength( cL );
		return true;
	    } catch( NumberFormatException ex ) {
		// We shouldn't set the header 
	    }
	}
	if( name.equalsIgnoreCase( "Content-Language" ) ) {
	    // XXX XXX Need to construct Locale or something else
	}
	return false;
    }

    public int getBufferSize() {
	if( bBuffer != null ) return bBuffer.getBufferSize();
	return out.getBufferSize();
    }

    public void setBufferSize(int size) throws IllegalStateException {
	// Force the PrintWriter to flush the data to the OutputStream.
	if (usingWriter == true && writer != null ) writer.flush();

	if( bBuffer != null ) {
	    if( bBuffer.isContentWritten() ) {
		throw new IllegalStateException ( sm.getString("servletOutputStreamImpl.setbuffer.ise"));
	    }
	    bBuffer.setBufferSize(size);
	    return;
	}
	
	if (out.isContentWritten() == true) {
	    throw new IllegalStateException ( sm.getString("servletOutputStreamImpl.setbuffer.ise"));
	}
	out.setBufferSize(size);
    }

    /*
     * Methodname "isCommitted" already taken by Response class.
     */
    public boolean isBufferCommitted() {
	return commited;
	//	return out.isCommitted();
    }

    public void setBufferCommitted( boolean v ) {
	this.commited=v;
	// 	System.out.println("Buffer commited " );
	// 	/*DEBUG*/ try {throw new Exception(); }
	//catch(Exception ex) {ex.printStackTrace();}
    }
    
    public void reset() throws IllegalStateException {

        if( isIncluded() ) return;      // We are in an included sub-request

	// Force the PrintWriter to flush its data to the output
        // stream before resetting the output stream
        //
        userCookies.removeAllElements();  // keep system (session) cookies
        contentType = Constants.DEFAULT_CONTENT_TYPE;
        locale = DEFAULT_LOCALE;
        characterEncoding = Constants.DEFAULT_CHAR_ENCODING;
        contentLength = -1;
        status = 200;

	if (usingWriter == true && writer != null)
	    writer.flush();

	body=null;
	// Reset the stream
	out.reset();

        // Clear the cookies and such

        // Clear the headers
        headers.clear();
    }

    // Reset the response buffer but not headers and cookies
    public void resetBuffer() throws IllegalStateException {
	if( usingWriter && writer != null )
	    writer.flush();

	out.reset();	// May throw IllegalStateException

    }

    public void flushBuffer() throws IOException {
	//	if( notIncluded) {
	    if (usingWriter == true && writer != null)
		writer.flush();
	    
	    out.reallyFlush();
	    //} 
    }


    /** Signal that we're done with the headers, and body will follow.
     *  Any implementation needs to notify ContextManager, to allow
     *  interceptors to fix headers.
     */
    public void endHeaders() throws IOException {
	notifyEndHeaders();
    }

    /** Signal that we're done with the headers, and body will follow.
     *  Any implementation needs to notify ContextManager, to allow
     *  interceptors to fix headers.
     */
    public void notifyEndHeaders() throws IOException {
	//	System.out.println("End headers " + request.getProtocol());
	if(request.getProtocol()==null) // HTTP/0.9 
	    return;

	// let CM notify interceptors and give a chance to fix
	// the headers
	if(request.getContext() != null) 
	    request.getContext().getContextManager().doBeforeBody(request, this);

	// No action.. 
    }

    public void addCookie(Cookie cookie) {
        if( isIncluded() ) return;      // We are in an included sub-request
	addHeader( CookieTools.getCookieHeaderName(cookie),
			    CookieTools.getCookieHeaderValue(cookie));
	if( cookie.getVersion() == 1 ) {
	    // add a version 0 header too.
	    // XXX what if the user set both headers??
	    Cookie c0 = (Cookie)cookie.clone();
	    c0.setVersion(0);
	    addHeader( CookieTools.getCookieHeaderName(c0),
				CookieTools.getCookieHeaderValue(c0));
	}
	userCookies.addElement(cookie);
    }

    public Enumeration getCookies() {
	return userCookies.elements();
    }

    public void setSessionId( String id ) {
	if( notIncluded ) sessionId=id;
    }

    public String getSessionId() {
	return sessionId;
    }
    
    public Locale getLocale() {
        return locale;
    }

    public void setLocale(Locale locale) {
        if (locale == null || isIncluded() ) {
            return;  // throw an exception?
        }

        // Save the locale for use by getLocale()
        this.locale = locale;

        // Set the contentLanguage for header output
        contentLanguage = locale.getLanguage();

        // Set the contentType for header output
        // Use the setContentType() method so encoding is set properly
        String newType = constructLocalizedContentType(contentType, locale);
        setContentType(newType);

	// only one header !
	headers.putHeader("Content-Language", contentLanguage);
    }

    /** Utility method for parsing the mime type and setting
     *  the encoding to locale. Also, convert from java Locale to mime encodings
    */
    private static String constructLocalizedContentType(String type, Locale loc) {
        // Cut off everything after the semicolon
        int semi = type.indexOf(";");
        if (semi != -1) {
            type = type.substring(0, semi);
        }

        // Append the appropriate charset, based on the locale
        String charset = LocaleToCharsetMap.getCharset(loc);
        if (charset != null) {
            type = type + "; charset=" + charset;
        }

        return type;
    }

    public String getCharacterEncoding() {
	return characterEncoding;
    }

    public void setContentType(String contentType) {
        if( isIncluded() ) return;      // We are in an included sub-request
	this.contentType = contentType;
	String encoding = RequestUtil.getCharsetFromContentType(contentType);
        if (encoding != null) {
	    characterEncoding = encoding;
        }
	headers.putHeader("Content-Type", contentType);
    }

    public String getContentType() {
	return contentType;
    }
    
    public void setContentLength(int contentLength) {
        if( isIncluded() ) return;      // We are in an included sub-request
	this.contentLength = contentLength;
	headers.putHeader("Content-Length", (new Integer(contentLength)).toString());
    }

    public int getContentLength() {
	return contentLength;
    }

    public int getStatus() {
        return status;
    }

    
    /** Set the response status 
     */ 
    public void setStatus( int status ) {
        if( isIncluded() ) return;      // We are in an included sub-request
	this.status=status;
    }

    /** Write a chunk of bytes. Should be called only from ServletOutputStream implementations,
     *	No need to implement it if your adapter implements ServletOutputStream.
     *  Headers and status will be written before this method is exceuted.
     */
    public void doWrite( byte buffer[], int pos, int count) throws IOException {
        // XXX fix if charset is other than default.
        if( body==null)
	    body=new StringBuffer();
	body.append(new String(buffer, pos, count, 
			       Constants.DEFAULT_CHAR_ENCODING) );
    }

    public StringBuffer getBody() {
	return body;
    }

    // utility method - should be in a different class
    public static String getMessage( int status ) {
	return sm.getString("sc."+ status);
    }

}

⌨️ 快捷键说明

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