Beispiel #1
0
    public void messageReceived(ChannelHandlerContext ctx, Object o) throws Exception {

      if (o instanceof HttpRequest) { // DefaultHttpRequest ) {
        queue.add((HttpRequest) o);
      } else if (o instanceof LastHttpContent) {

        HttpRequest req = queue.remove();

        req.getMethod();
        req.getUri();
        req.headers();

        LastHttpContent content = (LastHttpContent) o;

        ByteBuf buf = content.content();

        if (buf.readableBytes() > 0) {
          Gson gson = GsonFactory.createBuilder().create();

          Reader in = new InputStreamReader(new ByteBufInputStream(buf), "utf-8");
          Object v =
              gson.fromJson(in, Class.forName("com.logbook.logbook.resources.logs.LogEntryDTO"));
          System.out.println("v = " + v);
        }

        System.out.println(
            req.getMethod() + " " + req.getUri() + "    -- " + buf.readableBytes() + " bytes");

        HttpResponse r = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
        r.headers().add("Content-Length", "0");
        ctx.write(r);

      } else {
        System.out.println("o = " + o + " : " + o.getClass());
      }
    }
Beispiel #2
0
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

      messageReceiveCount++;
      if (msg instanceof HttpResponse) {
        try {
          HttpResponse response = (HttpResponse) msg;

          StringBuilder sb = new StringBuilder();
          if (LOG.isDebugEnabled()) {
            sb.append("STATUS: ")
                .append(response.getStatus())
                .append(", VERSION: ")
                .append(response.getProtocolVersion())
                .append(", HEADER: ");
          }
          if (!response.headers().names().isEmpty()) {
            for (String name : response.headers().names()) {
              for (String value : response.headers().getAll(name)) {
                if (LOG.isDebugEnabled()) {
                  sb.append(name).append(" = ").append(value);
                }
                if (this.length == -1 && name.equals("Content-Length")) {
                  this.length = Long.parseLong(value);
                }
              }
            }
          }
          if (LOG.isDebugEnabled()) {
            LOG.debug(sb.toString());
          }

          if (response.getStatus().code() == HttpResponseStatus.NO_CONTENT.code()) {
            LOG.warn("There are no data corresponding to the request");
            length = 0;
            return;
          } else if (response.getStatus().code() != HttpResponseStatus.OK.code()) {
            LOG.error(response.getStatus().reasonPhrase());
            state = TajoProtos.FetcherState.FETCH_FAILED;
            return;
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }

      if (msg instanceof HttpContent) {
        try {
          HttpContent httpContent = (HttpContent) msg;
          ByteBuf content = httpContent.content();
          if (content.isReadable()) {
            content.readBytes(fc, content.readableBytes());
          }

          if (msg instanceof LastHttpContent) {
            if (raf != null) {
              fileLen = file.length();
            }

            finishTime = System.currentTimeMillis();
            if (state != TajoProtos.FetcherState.FETCH_FAILED) {
              state = TajoProtos.FetcherState.FETCH_FINISHED;
            }

            IOUtils.cleanup(LOG, fc, raf);
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }
    }
 /**
  * Sets the content type header for the HTTP Response
  *
  * @param response HTTP response
  * @param file file to extract content type
  */
 private static void setContentTypeHeader(HttpResponse response, File file) {
   MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
   response.headers().set(CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
 }