Esempio n. 1
0
 public void handle(NettyHttpRequest req, DefaultHttpResponse resp) {
   String filename = req.header(SentryConstants.HeaderKeys.FILENAME);
   if (filename == null) {
     resp.setContent(ChannelBuffers.copiedBuffer(EXCEPTION_STRING.getBytes()));
     return;
   }
   int didx = filename.lastIndexOf("/");
   if (didx > 0) {
     String dir = ((SlaveModule) module).getDataDir() + "/" + filename.substring(0, didx);
     new File(dir).mkdirs();
     System.out.println(dir);
   }
   byte[] file = req.getRequest().getContent().array();
   try {
     BufferedOutputStream bos =
         new BufferedOutputStream(
             new FileOutputStream(((SlaveModule) module).getDataDir() + "/" + filename));
     bos.write(file);
     bos.close();
     HttpResponseUtil.setResponse(resp, "FileReceiveHandler", SUCCESS_STRING);
   } catch (IOException e) {
     HttpResponseUtil.setResponse(
         resp, "FileReceiveHandler", EXCEPTION_STRING, HttpResponseStatus.INTERNAL_SERVER_ERROR);
     e.printStackTrace();
   }
 }
Esempio n. 2
0
 /**
  * Sends an HTTP reply to the client.
  *
  * @param status The status of the request (e.g. 200 OK or 404 Not Found).
  * @param buf The content of the reply to send.
  */
 private void sendBuffer(final HttpResponseStatus status, final ChannelBuffer buf) {
   if (!chan.isConnected()) {
     done();
     return;
   }
   final DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
   response.setHeader(HttpHeaders.Names.CONTENT_TYPE, guessMimeType(buf));
   // TODO(tsuna): Server, X-Backend, etc. headers.
   response.setContent(buf);
   final boolean keepalive = HttpHeaders.isKeepAlive(request);
   if (keepalive) {
     HttpHeaders.setContentLength(response, buf.readableBytes());
   }
   final ChannelFuture future = chan.write(response);
   if (!keepalive) {
     future.addListener(ChannelFutureListener.CLOSE);
   }
   done();
 }
Esempio n. 3
0
  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
    Channel channel = ctx.getChannel();
    if (channel.getAttachment() != null
        && (Error.class.isAssignableFrom(channel.getAttachment().getClass())
            || WRITING.equals(channel.getAttachment().toString()))) {
      return;
    }

    if (!isReceived(ctx)) {
      return;
    }

    try {
      log.error(e.getCause().getMessage(), e.getCause());
      channel.setAttachment(new Error());
      DefaultHttpResponse response =
          new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
      response.setContent(
          ChannelBuffers.copiedBuffer(
              response.getStatus().toString() + ":" + e.getCause().getMessage(),
              CharsetUtil.UTF_8));
      response.headers().set(Names.CONTENT_LENGTH, response.getContent().readableBytes());
      response.headers().set(Names.SERVER, "NAVI/1.1.4(UNIX)");
      response.headers().set(Names.CONNECTION, "close");
      ChannelFuture f = channel.write(response);
      f.addListener(ChannelFutureListener.CLOSE);
      f.addListener(
          new ChannelFutureListener() {

            public void operationComplete(ChannelFuture f) throws Exception {
              if (!f.isSuccess()) {
                log.error(f.getCause().getMessage(), f.getCause());
              }
            }
          });
    } catch (Exception ex) {
      log.error(e.getCause().getMessage(), e.getCause());
      e.getFuture().addListener(ChannelFutureListener.CLOSE);
    }
  }
Esempio n. 4
0
 public static void send(
     CharSequence content, Charset charset, HttpRequest request, ChannelHandlerContext context) {
   DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
   response.setContent(ChannelBuffers.copiedBuffer(content, charset));
   send(response, request, context);
 }