abstracthttpresponse.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 2,408 行 · 第 1/4 页

JAVA
2,408
字号
      encoding = null;      _charEncoding = "iso-8859-1";    }    else      _charEncoding = encoding;    try {      _responseStream.setEncoding(encoding);    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);    }  }  String getRealCharacterEncoding()  {    return _charEncoding;  }  /**   * Adds a cookie to the response.   *   * @param cookie the response cookie   */  public void addCookie(Cookie cookie)  {    _request.setHasCookie();    if (_disableHeaders)      return;    if (cookie == null)      return;    _cookiesOut.add(cookie);  }  public Cookie getCookie(String name)  {    if (_cookiesOut == null)      return null;    for (int i = _cookiesOut.size() - 1; i >= 0; i--) {      Cookie cookie = (Cookie) _cookiesOut.get(i);      if (cookie.getName().equals(name))        return cookie;    }    return null;  }  public ArrayList getCookies()  {    return _cookiesOut;  }  public void setSessionId(String id)  {    _sessionId = id;    // XXX: server/1315 vs server/0506 vs server/170k    // could also set the nocache=JSESSIONID    setPrivateOrResinCache(true);  }  /**   * Sets a footer, replacing an already-existing footer   *   * @param key the header key to set.   * @param value the header value to set.   */  public void setFooter(String key, String value)  {    if (_disableHeaders)      return;    else if (value == null)      throw new NullPointerException();    int i = 0;    boolean hasFooter = false;    for (i = _footerKeys.size() - 1; i >= 0; i--) {      String oldKey = _footerKeys.get(i);      if (oldKey.equalsIgnoreCase(key)) {	if (hasFooter) {	  _footerKeys.remove(i);	  _footerValues.remove(i);	}	else {	  hasFooter = true;	  _footerValues.set(i, value);	}      }    }    if (! hasFooter) {      _footerKeys.add(key);      _footerValues.add(value);    }  }  /**   * Adds a new footer.  If an old footer with that name exists,   * both footers are output.   *   * @param key the footer key.   * @param value the footer value.   */  public void addFooter(String key, String value)  {    if (_disableHeaders)      return;    if (setSpecial(key, value))      return;    _footerKeys.add(key);    _footerValues.add(value);  }  /**   * Sets the ResponseStream   */  public void setResponseStream(AbstractResponseStream responseStream)  {    _responseStream = responseStream;    _responseOutputStream.init(responseStream);    _responsePrintWriter.init(responseStream);  }  /**   * Gets the response stream.   */  public AbstractResponseStream getResponseStream()  {    return _responseStream;  }  /**   * Gets the response stream.   */  public AbstractResponseStream getOriginalStream()  {    return _originalResponseStream;  }  /**   * Returns true for a Caucho response stream.   */  public boolean isCauchoResponseStream()  {    return _responseStream.isCauchoResponseStream();  }    /**   * Returns the ServletOutputStream for the response.   */  public ServletOutputStream getOutputStream() throws IOException  {    /*    if (_hasWriter)      throw new IllegalStateException(L.l("getOutputStream() can't be called after getWriter()."));    */    _hasOutputStream = true;    /*    // server/10a2    if (! _hasWriter) {      // jsp/0510 vs jsp/1b00      // _responseStream.setOutputStreamOnly(true);    }    */        return _responseOutputStream;  }  /**   * Sets the flush buffer   */  public void setFlushBuffer(FlushBuffer flushBuffer)  {    _flushBuffer = flushBuffer;  }  /**   * Gets the flush buffer   */  public FlushBuffer getFlushBuffer()  {    return _flushBuffer;  }  /**   * Returns a PrintWriter for the response.   */  public PrintWriter getWriter() throws IOException  {    /*    if (_hasOutputStream)      throw new IllegalStateException(L.l("getWriter() can't be called after getOutputStream()."));    */    if (! _hasWriter) {      _hasWriter = true;      if (_charEncoding != null)	_responseStream.setEncoding(_charEncoding);    }        return _responsePrintWriter;  }  /**   * Returns the parent writer.   */  public PrintWriter getNextWriter()  {    return null;  }  /**   * Encode the URL with the session jd.   *   * @param string the url to be encoded   *   * @return the encoded url   */  public String encodeURL(String string)  {    CauchoRequest request = getRequest();        WebApp app = request.getWebApp();    if (app == null)      return string;        if (request.isRequestedSessionIdFromCookie())      return string;    HttpSession session = request.getSession(false);    if (session == null)      return string;    SessionManager sessionManager = app.getSessionManager();    if (! sessionManager.enableSessionUrls())      return string;    CharBuffer cb = _cb;    cb.clear();    String altPrefix = sessionManager.getAlternateSessionPrefix();    if (altPrefix == null) {      // standard url rewriting      int p = string.indexOf('?');      if (p == 0) {	cb.append(string);      }      else if (p > 0) {        cb.append(string, 0, p);        cb.append(sessionManager.getSessionPrefix());        cb.append(session.getId());        cb.append(string, p, string.length() - p);      }      else if ((p = string.indexOf('#')) >= 0) {        cb.append(string, 0, p);        cb.append(sessionManager.getSessionPrefix());        cb.append(session.getId());        cb.append(string, p, string.length() - p);      }      else {        cb.append(string);        cb.append(sessionManager.getSessionPrefix());        cb.append(session.getId());      }    }    else {      int p = string.indexOf("://");            if (p < 0) {	cb.append(altPrefix);	cb.append(session.getId());		if (! string.startsWith("/")) {	  cb.append(_request.getContextPath());	  cb.append('/');	}        cb.append(string);      }      else {	int q = string.indexOf('/', p + 3);	if (q < 0) {	  cb.append(string);	  cb.append(altPrefix);	  cb.append(session.getId());	}	else {	  cb.append(string.substring(0, q));	  cb.append(altPrefix);	  cb.append(session.getId());	  cb.append(string.substring(q));	}      }    }    return cb.toString();  }  public String encodeRedirectURL(String string)  {    return encodeURL(string);  }    /**     * @deprecated     */  public String encodeRedirectUrl(String string)  {    return encodeRedirectURL(string);  }    /**     * @deprecated     */  public String encodeUrl(String string)  {    return encodeURL(string);  }  /*   * jsdk 2.2   */  public void setBufferSize(int size)  {    _responseStream.setBufferSize(size);  }  public int getBufferSize()  {    return _responseStream.getBufferSize();  }  public void flushBuffer()    throws IOException  {    // server/10sn    _responseStream.flush();  }  public void flushHeader()    throws IOException  {    _responseStream.flushBuffer();  }  public void setDisableAutoFlush(boolean disable)  {    // XXX: _responseStream.setDisableAutoFlush(disable);  }  /**   * Returns true if some data has been sent to the browser.   */  public boolean isCommitted()  {    return _originalResponseStream.isCommitted();  }  public void reset()  {    reset(false);  }  public void resetBuffer()  {    _responseStream.clearBuffer();    /*    if (_currentWriter instanceof JspPrintWriter)      ((JspPrintWriter) _currentWriter).clear();    */  }  /**   * Clears the response for a forward()   *   * @param force if not true and the response stream has committed,   *   throw the IllegalStateException.   */  void reset(boolean force)  {    if (! force && _originalResponseStream.isCommitted())      throw new IllegalStateException(L.l("response cannot be reset() after committed"));        _responseStream.clearBuffer();    /*    if (_currentWriter instanceof JspPrintWriter)      ((JspPrintWriter) _currentWriter).clear();    */    _statusCode = 200;    _statusMessage = "OK";    _headerKeys.clear();    _headerValues.clear();    // cookiesOut.clear();    _contentLength = -1;    //_isNoCache = false;    //_isPrivateCache = false;        _charEncoding = null;    _locale = null;    _hasOutputStream = false;    _hasWriter = false;    try {      _responseStream.setLocale(null);      _responseStream.setEncoding(null);    } catch (Exception e) {    }  }  // XXX: hack to deal with forwarding  public void clearBuffer()  {    _responseStream.clearBuffer();  }  public void setLocale(Locale locale)  {    _locale = locale;    if (! _hasCharEncoding && ! isCommitted()) {      _charEncoding = getRequest().getWebApp().getLocaleEncoding(locale);      try {        if (_charEncoding != null) {          // _originalStream.setEncoding(_charEncoding);          _responseStream.setEncoding(_charEncoding);	}      } catch (IOException e) {      }    }    CharBuffer cb = _cb;    cb.clear();    cb.append(locale.getLanguage());    if (locale.getCountry() != null &&  ! "".equals(locale.getCountry())) {      cb.append("-");      cb.append(locale.getCountry());      if (locale.getVariant() != null && ! "".equals(locale.getVariant())) {        cb.append("-");        cb.append(locale.getVariant());      }    }        setHeader("Content-Language", cb.toString());  }  public Locale getLocale()  {    if (_locale != null)      return _locale;    else      return Locale.getDefault();  }  // needed to support JSP    public int getRemaining()  {    return _responseStream.getRemaining();  }  /**   * Returns the number of bytes sent to the output.   */  public int getContentLength()  {    return _originalResponseStream.getContentLength();  }  /**   * Disables the response   *   * @since Servlet 3.0   */  public void disable()  {  }  /**   * Enables the response   *   * @since Servlet 3.0   */  public void enable()  {  }  /**   * Returns true if the response is disabled   *   * @since Servlet 3.0   */  public boolean isDisabled()  {    return false;  }  public boolean disableHeaders(boolean disable)  {    boolean old = _disableHeaders;    _disableHeaders = disable;    return old;  }  public boolean disableCaching(boolean disable)  {    boolean old = _disableCaching;    _disableCaching = disable;    return old;  }  /**   * Returns true if the headers have been written.   */  final public boolean isHeaderWritten()  {    return _isHeaderWritten;  }  /**   * Returns true if the headers have been written.   */  final public void setHeaderWritten(boolean isWritten)  {    _isHeaderWritten = isWritten;  }    /**   * Writes the continue   */  final void writeContinue()    throws IOException  {    if (! _isHeaderWritten) {      writeContinueInt(_rawWrite);      _rawWrite.flush();    }  }    /**   * Writes the continue   */  protected void writeContinueInt(WriteStream os)    throws IOException  {  }  /**   * Writes the headers to the stream.  Called prior to the first flush   * of data.   *   * @param os browser stream.   * @param length length of the response if known, or -1 is unknown.   * @return true if the content is chunked.   */  protected boolean writeHeaders(WriteStream os, int length)    throws IOException  {    if (_isHeaderWritten)      return _isChunked;    // server/1373 for getBufferSize()    boolean canCache = startCaching(true);    _isHeaderWritten = true;    boolean isHead = false;        if (_request.getMethod().equals("HEAD")) {      isHead = true;      _originalResponseStream.setHead();    }    WebApp webApp = _request.getWebApp();    int majorCode = _statusCode / 100;    if (webApp != null) {      if (majorCode == 5)	webApp.addStatus500();    }    HttpSession session = _originalRequest.getMemorySession();    if (session instanceof SessionImpl)

⌨️ 快捷键说明

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