Beispiel #1
0
  @Override
  public PrintWriter getWriter() throws IOException {
    if (_outputType == OutputType.STREAM) throw new IllegalStateException("STREAM");

    if (_outputType == OutputType.NONE) {
      /* get encoding from Content-Type header */
      String encoding = _characterEncoding;
      if (encoding == null) {
        encoding = MimeTypes.inferCharsetFromContentType(_contentType);
        if (encoding == null) encoding = StringUtil.__ISO_8859_1;
        setCharacterEncoding(encoding);
      }

      if (_writer != null && _writer.isFor(encoding)) _writer.reopen();
      else {
        if (StringUtil.__ISO_8859_1.equalsIgnoreCase(encoding))
          _writer = new ResponseWriter(new Iso88591HttpWriter(_out), encoding);
        else if (StringUtil.__UTF8.equalsIgnoreCase(encoding))
          _writer = new ResponseWriter(new Utf8HttpWriter(_out), encoding);
        else _writer = new ResponseWriter(new EncodingHttpWriter(_out, encoding), encoding);
      }

      // Set the output type at the end, because setCharacterEncoding() checks for it
      _outputType = OutputType.WRITER;
    }
    return _writer;
  }
Beispiel #2
0
  @Override
  public void setLocale(Locale locale) {
    if (locale == null || isCommitted() || isIncluding()) return;

    _locale = locale;
    _fields.put(HttpHeader.CONTENT_LANGUAGE, locale.toString().replace('_', '-'));

    if (_outputType != OutputType.NONE) return;

    if (_channel.getRequest().getContext() == null) return;

    String charset =
        _channel.getRequest().getContext().getContextHandler().getLocaleEncoding(locale);

    if (charset != null && charset.length() > 0 && _characterEncoding == null)
      setCharacterEncoding(charset);
  }