@Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
    if (event.getMessage() instanceof MappingHttpRequest) {
      MappingHttpRequest httpRequest = (MappingHttpRequest) event.getMessage();

      if (httpRequest.getMessage() instanceof ObjectStorageDataRequestType) {
        if (httpRequest.isChunked()) {
          // Chunked request, and beginning, setup map etc.
          initializeNewPut(ctx, (ObjectStorageDataRequestType) httpRequest.getMessage());
        }
      }
    } else if (event.getMessage() instanceof HttpChunk) {
      // Add the chunk to the current streams channel buffer.
      HttpChunk chunk = (HttpChunk) event.getMessage();
      appendChunk(chunk.getContent(), ctx.getChannel());

      if (chunk.isLast()) {
        // Remove from the map
        Logs.extreme()
            .debug(
                "Removing data map due to last chunk processed event for channel: "
                    + ctx.getChannel().getId());
        dataMap.remove(ctx.getChannel());
      }
    }
    // Always pass it on
    ctx.sendUpstream(event);
  }
  @Override
  public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e)
      throws Exception {

    if (!chunked) {
      final HttpRequest request = (HttpRequest) e.getMessage();

      final ChannelBuffer buffer = request.getContent();
      receivedData.write(buffer.array());
      // System.out.println("received "+buffer.array() );
      // System.out.println(buffer.array().length);
      if (!request.isChunked()) {
        processRequest(e);
      } else {
        chunked = true;
      }
      // final boolean keepAlive = isKeepAlive(request);
    } else {
      final HttpChunk chunk = (HttpChunk) e.getMessage();
      final ChannelBuffer buffer = chunk.getContent();
      receivedData.write(buffer.array());
      if (chunk.isLast()) {
        processRequest(e);
      }
    }
  }
Esempio n. 3
0
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

      Channel ch = e.getChannel();
      ClientConnection conn = connectionMap.get(ch);
      Object msg = e.getMessage();
      if (msg instanceof HttpResponse) {
        HttpResponse response = (HttpResponse) msg;

        conn.handleResponse(response);
        ChannelBuffer content = response.getContent();
        if (content.readable()) {
          conn.handleResponseChunk(new Buffer(content));
        }
        if (!response.isChunked()) {
          conn.handleResponseEnd();
        }
      } else if (msg instanceof HttpChunk) {
        HttpChunk chunk = (HttpChunk) msg;
        if (chunk.getContent().readable()) {
          Buffer buff = new Buffer(chunk.getContent());
          conn.handleResponseChunk(buff);
        }
        if (chunk.isLast()) {
          if (chunk instanceof HttpChunkTrailer) {
            HttpChunkTrailer trailer = (HttpChunkTrailer) chunk;
            conn.handleResponseEnd(trailer);
          } else {
            conn.handleResponseEnd();
          }
        }
      } else if (msg instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) msg;
        conn.handleWsFrame(frame);
      } else {
        throw new IllegalStateException("Invalid object " + e.getMessage());
      }
    }
 /**
  * Initialized the internals from a new chunk
  *
  * @param chunk the new received chunk
  * @throws ErrorDataDecoderException if there is a problem with the charset decoding or other
  *     errors
  */
 public void offer(HttpChunk chunk) throws ErrorDataDecoderException {
   ChannelBuffer chunked = chunk.getContent();
   if (undecodedChunk == null) {
     undecodedChunk = chunked;
   } else {
     // undecodedChunk = ChannelBuffers.wrappedBuffer(undecodedChunk, chunk.getContent());
     // less memory usage
     undecodedChunk = ChannelBuffers.wrappedBuffer(undecodedChunk, chunked);
   }
   if (chunk.isLast()) {
     isLastChunk = true;
   }
   parseBody();
 }
Esempio n. 5
0
 void chunk(HttpChunk chunk) throws Exception {
   Preconditions.checkState(
       bodyConsumer != null, "Received chunked content without BodyConsumer.");
   if (chunk.isLast()) {
     bodyConsumer.finished(responder);
     bodyConsumer = null;
   } else {
     try {
       bodyConsumer.chunk(chunk.getContent(), responder);
     } catch (Throwable t) {
       bodyConsumer.handleError(t);
       bodyConsumer = null;
       throw new HandlerException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "", t);
     }
   }
 }
  @Override
  protected void httpMessageReceived(
      ChannelHandlerContext ctx, MessageEvent e, HttpChunk httpMessage) throws Exception {
    ChannelBuffer content = httpMessage.getContent();
    if (content.readable()) {
      fireMessageReceived(httpChildChannel, content);
    }

    boolean last = httpMessage.isLast();
    if (last) {
      HttpChildChannel httpChildChannel = this.httpChildChannel;
      httpChildChannel.readState(HttpReadState.CONTENT_COMPLETE);
      this.httpChildChannel = null;
      fireInputShutdown(httpChildChannel);
      if (httpChildChannel.setReadClosed()) {
        fireChannelDisconnected(httpChildChannel);
        fireChannelUnbound(httpChildChannel);
        fireChannelClosed(httpChildChannel);
      }
    }
  }
Esempio n. 7
0
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
      if (!readingChunks) {
        HttpResponse response = (HttpResponse) e.getMessage();

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

        if (response.getStatus() == HttpResponseStatus.NO_CONTENT) {
          LOG.info("There are no data corresponding to the request");
          return;
        }

        this.raf = new RandomAccessFile(file, "rw");
        this.fc = raf.getChannel();

        if (response.isChunked()) {
          readingChunks = true;
        } else {
          ChannelBuffer content = response.getContent();
          if (content.readable()) {
            fc.write(content.toByteBuffer());
          }
        }
      } else {
        HttpChunk chunk = (HttpChunk) e.getMessage();
        if (chunk.isLast()) {
          readingChunks = false;
          long fileLength = fc.position();
          fc.close();
          raf.close();
          if (fileLength == length) {
            LOG.info("Data fetch is done (total received bytes: " + fileLength + ")");
          } else {
            LOG.info(
                "Data fetch is done, but cannot get all data "
                    + "(received/total: "
                    + fileLength
                    + "/"
                    + length
                    + ")");
          }
        } else {
          fc.write(chunk.getContent().toByteBuffer());
        }
      }
    }
 /**
  * Finalize the request by preparing the Header in the request and returns the request ready to be
  * sent.<br>
  * Once finalized, no data must be added.<br>
  * If the request does not need chunk (isChunked() == false), this request is the only object to
  * send to the remote server.
  *
  * @return the request object (chunked or not according to size of body)
  * @throws ErrorDataEncoderException if the encoding is in error or if the finalize were already
  *     done
  */
 public HttpRequest finalizeRequest() throws ErrorDataEncoderException {
   // Finalize the multipartHttpDatas
   if (!headerFinalized) {
     if (isMultipart) {
       InternalAttribute internal = new InternalAttribute();
       if (duringMixedMode) {
         internal.addValue("\r\n--" + multipartMixedBoundary + "--");
       }
       internal.addValue("\r\n--" + multipartDataBoundary + "--\r\n");
       multipartHttpDatas.add(internal);
       multipartMixedBoundary = null;
       currentFileUpload = null;
       duringMixedMode = false;
       globalBodySize += internal.size();
     }
     headerFinalized = true;
   } else {
     throw new ErrorDataEncoderException("Header already encoded");
   }
   List<String> contentTypes = request.getHeaders(HttpHeaders.Names.CONTENT_TYPE);
   List<String> transferEncoding = request.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
   if (contentTypes != null) {
     request.removeHeader(HttpHeaders.Names.CONTENT_TYPE);
     for (String contentType : contentTypes) {
       // "multipart/form-data; boundary=--89421926422648"
       if (contentType.toLowerCase().startsWith(HttpHeaders.Values.MULTIPART_FORM_DATA)) {
         // ignore
       } else if (contentType
           .toLowerCase()
           .startsWith(HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED)) {
         // ignore
       } else {
         request.addHeader(HttpHeaders.Names.CONTENT_TYPE, contentType);
       }
     }
   }
   if (isMultipart) {
     String value =
         HttpHeaders.Values.MULTIPART_FORM_DATA
             + "; "
             + HttpHeaders.Values.BOUNDARY
             + "="
             + multipartDataBoundary;
     request.addHeader(HttpHeaders.Names.CONTENT_TYPE, value);
   } else {
     // Not multipart
     request.addHeader(
         HttpHeaders.Names.CONTENT_TYPE, HttpHeaders.Values.APPLICATION_X_WWW_FORM_URLENCODED);
   }
   // Now consider size for chunk or not
   long realSize = globalBodySize;
   if (isMultipart) {
     iterator = multipartHttpDatas.listIterator();
   } else {
     realSize -= 1; // last '&' removed
     iterator = multipartHttpDatas.listIterator();
   }
   request.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(realSize));
   if (realSize > HttpPostBodyUtil.chunkSize) {
     isChunked = true;
     if (transferEncoding != null) {
       request.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
       for (String v : transferEncoding) {
         if (v.equalsIgnoreCase(HttpHeaders.Values.CHUNKED)) {
           // ignore
         } else {
           request.addHeader(HttpHeaders.Names.TRANSFER_ENCODING, v);
         }
       }
     }
     request.addHeader(HttpHeaders.Names.TRANSFER_ENCODING, HttpHeaders.Values.CHUNKED);
     request.setContent(ChannelBuffers.EMPTY_BUFFER);
   } else {
     // get the only one body and set it to the request
     HttpChunk chunk = nextChunk();
     request.setContent(chunk.getContent());
   }
   return request;
 }