Пример #1
0
  @Override
  public void resetBuffer() {
    if (isCommitted()) throw new IllegalStateException("Committed");

    switch (_outputType) {
      case STREAM:
      case WRITER:
        _out.reset();
    }

    _out.resetBuffer();
  }
Пример #2
0
    /*
     * @see java.io.OutputStream#close()
     */
    @Override
    public void close() throws IOException {
      if (isClosed()) return;

      if (!isIncluding() && !super._generator.isCommitted()) commitResponse(Generator.LAST);
      else flushResponse();

      super.close();
    }
Пример #3
0
 protected void recycle() {
   _status = HttpStatus.NOT_SET_000;
   _reason = null;
   _locale = null;
   _mimeType = null;
   _characterEncoding = null;
   _contentType = null;
   _outputType = OutputType.NONE;
   _contentLength = -1;
   _out.reset();
   _fields.clear();
 }
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String fileName = request.getServletPath();
    byte[] dataBytes = loadContentFileBytes(fileName);

    ServletOutputStream out = response.getOutputStream();

    if (fileName.endsWith("txt")) response.setContentType("text/plain");
    else if (fileName.endsWith("mp3")) response.setContentType("audio/mpeg");
    response.setHeader("ETag", "W/etag-" + fileName);

    response.setContentLength(dataBytes.length);

    ((HttpOutput) out).write(ByteBuffer.wrap(dataBytes).asReadOnlyBuffer());
  }
Пример #5
0
  @Override
  public void setContentLength(int len) {
    // Protect from setting after committed as default handling
    // of a servlet HEAD request ALWAYS sets _content length, even
    // if the getHandling committed the response!
    if (isCommitted() || isIncluding()) return;

    long written = _out.getWritten();
    if (written > len)
      throw new IllegalArgumentException(
          "setContentLength(" + len + ") when already written " + written);

    _contentLength = len;
    _fields.putLongField(HttpHeader.CONTENT_LENGTH.toString(), len);

    if (_contentLength > 0) {
      try {
        closeIfAllContentWritten(written);
      } catch (IOException e) {
        throw new RuntimeIOException(e);
      }
    }
  }
Пример #6
0
    /* ------------------------------------------------------------ */
    public void sendContent(Object content) throws IOException {
      Resource resource = null;

      if (isClosed()) throw new IOException("Closed");

      if (super._generator.isWritten()) throw new IllegalStateException("!empty");

      // Convert HTTP content to contentl
      if (content instanceof HttpContent) {
        HttpContent httpContent = (HttpContent) content;
        Buffer contentType = httpContent.getContentType();
        if (contentType != null && !_responseFields.containsKey(HttpHeaders.CONTENT_TYPE_BUFFER)) {
          String enc = _response.getSetCharacterEncoding();
          if (enc == null) _responseFields.add(HttpHeaders.CONTENT_TYPE_BUFFER, contentType);
          else {
            if (contentType instanceof CachedBuffer) {
              CachedBuffer content_type = ((CachedBuffer) contentType).getAssociate(enc);
              if (content_type != null)
                _responseFields.put(HttpHeaders.CONTENT_TYPE_BUFFER, content_type);
              else {
                _responseFields.put(
                    HttpHeaders.CONTENT_TYPE_BUFFER,
                    contentType + ";charset=" + QuotedStringTokenizer.quoteIfNeeded(enc, ";= "));
              }
            } else {
              _responseFields.put(
                  HttpHeaders.CONTENT_TYPE_BUFFER,
                  contentType + ";charset=" + QuotedStringTokenizer.quoteIfNeeded(enc, ";= "));
            }
          }
        }
        if (httpContent.getContentLength() > 0)
          _responseFields.putLongField(
              HttpHeaders.CONTENT_LENGTH_BUFFER, httpContent.getContentLength());
        Buffer lm = httpContent.getLastModified();
        long lml = httpContent.getResource().lastModified();
        if (lm != null) _responseFields.put(HttpHeaders.LAST_MODIFIED_BUFFER, lm);
        else if (httpContent.getResource() != null) {
          if (lml != -1) _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER, lml);
        }

        boolean direct =
            _connector instanceof NIOConnector
                && ((NIOConnector) _connector).getUseDirectBuffers()
                && !(_connector instanceof SslConnector);
        content = direct ? httpContent.getDirectBuffer() : httpContent.getIndirectBuffer();
        if (content == null) content = httpContent.getInputStream();
      } else if (content instanceof Resource) {
        resource = (Resource) content;
        _responseFields.putDateField(HttpHeaders.LAST_MODIFIED_BUFFER, resource.lastModified());
        content = resource.getInputStream();
      }

      // Process content.
      if (content instanceof Buffer) {
        super._generator.addContent((Buffer) content, Generator.LAST);
        commitResponse(Generator.LAST);
      } else if (content instanceof InputStream) {
        InputStream in = (InputStream) content;

        try {
          int max = super._generator.prepareUncheckedAddContent();
          Buffer buffer = super._generator.getUncheckedBuffer();

          int len = buffer.readFrom(in, max);

          while (len >= 0) {
            super._generator.completeUncheckedAddContent();
            _out.flush();

            max = super._generator.prepareUncheckedAddContent();
            buffer = super._generator.getUncheckedBuffer();
            len = buffer.readFrom(in, max);
          }
          super._generator.completeUncheckedAddContent();
          _out.flush();
        } finally {
          if (resource != null) resource.release();
          else in.close();
        }
      } else throw new IllegalArgumentException("unknown content type?");
    }
Пример #7
0
 /*
  * @see java.io.OutputStream#flush()
  */
 @Override
 public void flush() throws IOException {
   if (!super._generator.isCommitted()) commitResponse(Generator.MORE);
   super.flush();
 }
Пример #8
0
 public long getContentCount() {
   return _out.getWritten();
 }
Пример #9
0
 public void complete() throws IOException {
   _out.close();
 }
Пример #10
0
 @Override
 public void flushBuffer() throws IOException {
   if (!_out.isClosed()) _out.flush();
 }
Пример #11
0
 @Override
 public int getBufferSize() {
   return _out.getBufferSize();
 }
Пример #12
0
 @Override
 public void setBufferSize(int size) {
   if (isCommitted() || getContentCount() > 0)
     throw new IllegalStateException("Committed or content written");
   _out.setBufferSize(size);
 }
Пример #13
0
 public void included() {
   _include.decrementAndGet();
   _out.reopen();
 }
Пример #14
0
 /* ------------------------------------------------------------ */
 @Override
 public void flush() throws IOException {
   _out.flush();
 }
Пример #15
0
 /* ------------------------------------------------------------ */
 @Override
 public void close() throws IOException {
   _out.close();
 }