Пример #1
0
  public NettyHttpResponse error(Throwable error) {
    response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    String message = getStackTrace(error);
    header("Content-Type", "text/plain");
    content(message);
    flushResponse();

    exceptionHandler.uncaughtException(
        Thread.currentThread(), AradonRuntimeException.fromException(error, ctx.getChannel()));

    return this;
  }
Пример #2
0
  private NettyHttpResponse writeFileEntity(Response ares, FileRepresentation fentity) {
    try {
      setFileHeader(ares, fentity);

      File file = fentity.getFile();
      FileChannel fc = fentity.getChannel();

      MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
      content(bb);
    } catch (IOException ex) {
      ioExceptionHandler.uncaughtException(
          Thread.currentThread(), AradonRuntimeException.fromException(ex, ctx.getChannel()));
      error(ex);
    } finally {
      end();
    }
    return this;
  }
Пример #3
0
  private NettyHttpResponse writeBigFile(Response ares, FileRepresentation fentity) {

    try {
      setFileHeader(ares, fentity);

      ChannelFuture writeFuture = write(responseBuffer);

      final long fileLength = fentity.getSize();
      final FileInputStream fis = fentity.getStream();
      writeFuture.addListener(
          new ChannelFutureListener() {
            private final ChannelBuffer buffer = ChannelBuffers.buffer(4096);
            private long offset = 0;

            public void operationComplete(ChannelFuture future) throws Exception {
              if (!future.isSuccess()) {
                future.getCause().printStackTrace();
                future.getChannel().close();
                fis.close();
                return;
              }

              // System.out.println("SENDING: " + offset + " / " + fileLength);
              buffer.clear();
              buffer.writeBytes(fis, (int) Math.min(fileLength - offset, buffer.writableBytes()));
              offset += buffer.writerIndex();
              ChannelFuture chunkWriteFuture = future.getChannel().write(buffer);
              if (offset < fileLength) { // Send the next chunk
                chunkWriteFuture.addListener(this);
              } else {
                // Wrote the last chunk - close the connection if the write is done.
                // System.out.println("DONE: " + fileLength);
                chunkWriteFuture.addListener(ChannelFutureListener.CLOSE);
                fis.close();
              }
            }
          });

    } catch (Exception ex) {
      exceptionHandler.uncaughtException(
          Thread.currentThread(), AradonRuntimeException.fromException(ex, ctx.getChannel()));
    }
    return this;
  }
Пример #4
0
  private void flushResponse() {
    try {
      // TODO: Shouldn't have to do this, but without it we sometimes seem to get two Content-Length
      // headers in the response.
      //			if (StringUtil.isEmpty(response.getHeader(HeaderConstants.HEADER_CONTENT_LENGTH))) {
      //				header(HeaderConstants.HEADER_CONTENT_LENGTH, responseBuffer.readableBytes());
      //			}
      header("Content-Length", (String) null);
      header("Content-Length", responseBuffer.readableBytes());

      ChannelFuture future = write(responseBuffer);
      if (!isKeepAlive) {
        future.addListener(ChannelFutureListener.CLOSE);
      }
    } catch (Exception e) {
      exceptionHandler.uncaughtException(
          Thread.currentThread(), AradonRuntimeException.fromException(e, ctx.getChannel()));
    }
  }