コード例 #1
0
ファイル: Response.java プロジェクト: sslavic/jetty.project
  @Override
  public void setCharacterEncoding(String encoding) {
    if (isIncluding()) return;

    if (_outputType == OutputType.NONE && !isCommitted()) {
      if (encoding == null) {
        // Clear any encoding.
        if (_characterEncoding != null) {
          _characterEncoding = null;
          if (_contentType != null) {
            _contentType = MimeTypes.getContentTypeWithoutCharset(_contentType);
            HttpField field = HttpField.CONTENT_TYPE.get(_contentType);
            if (field != null) _fields.put(field);
            else _fields.put(HttpHeader.CONTENT_TYPE, _contentType);
          }
        }
      } else {
        // No, so just add this one to the mimetype
        _characterEncoding = StringUtil.normalizeCharset(encoding);
        if (_contentType != null) {
          _contentType =
              MimeTypes.getContentTypeWithoutCharset(_contentType)
                  + ";charset="
                  + _characterEncoding;
          HttpField field = HttpField.CONTENT_TYPE.get(_contentType);
          if (field != null) _fields.put(field);
          else _fields.put(HttpHeader.CONTENT_TYPE, _contentType);
        }
      }
    }
  }
コード例 #2
0
ファイル: Response.java プロジェクト: sslavic/jetty.project
  @Override
  public void setContentType(String contentType) {
    if (isCommitted() || isIncluding()) return;

    if (contentType == null) {
      if (isWriting() && _characterEncoding != null) throw new IllegalSelectorException();

      if (_locale == null) _characterEncoding = null;
      _mimeType = null;
      _contentType = null;
      _fields.remove(HttpHeader.CONTENT_TYPE);
    } else {
      _contentType = contentType;
      _mimeType = MimeTypes.CACHE.get(contentType);
      String charset;
      if (_mimeType != null && _mimeType.getCharset() != null)
        charset = _mimeType.getCharset().toString();
      else charset = MimeTypes.getCharsetFromContentType(contentType);

      if (charset == null) {
        if (_characterEncoding != null) {
          _contentType = contentType + ";charset=" + _characterEncoding;
          _mimeType = null;
        }
      } else if (isWriting() && !charset.equals(_characterEncoding)) {
        // too late to change the character encoding;
        _mimeType = null;
        _contentType = MimeTypes.getContentTypeWithoutCharset(_contentType);
        if (_characterEncoding != null)
          _contentType = _contentType + ";charset=" + _characterEncoding;
      } else {
        _characterEncoding = charset;
      }

      HttpField field = HttpField.CONTENT_TYPE.get(_contentType);
      if (field != null) _fields.put(field);
      else _fields.put(HttpHeader.CONTENT_TYPE, _contentType);
    }
  }