Ejemplo n.º 1
0
 /**
  * Utility function to add a new decoded data
  *
  * @param data
  */
 private void addHttpData(InterfaceHttpData data) {
   if (data == null) {
     return;
   }
   List<InterfaceHttpData> datas = bodyMapHttpData.get(data.getName());
   if (datas == null) {
     datas = new ArrayList<InterfaceHttpData>(1);
     bodyMapHttpData.put(data.getName(), datas);
   }
   datas.add(data);
   bodyListHttpData.add(data);
 }
 /**
  * From the current context (currentBuffer and currentData), returns the next HttpChunk (if
  * possible) trying to get sizeleft bytes more into the currentBuffer. This is the UrlEncoded
  * version.
  *
  * @param sizeleft the number of bytes to try to get from currentData
  * @return the next HttpChunk or null if not enough bytes were found
  * @throws ErrorDataEncoderException if the encoding is in error
  */
 private HttpChunk encodeNextChunkUrlEncoded(int sizeleft) throws ErrorDataEncoderException {
   if (currentData == null) {
     return null;
   }
   int size = sizeleft;
   ChannelBuffer buffer;
   if (isKey) {
     // get name
     String key = currentData.getName();
     buffer = ChannelBuffers.wrappedBuffer(key.getBytes());
     isKey = false;
     if (currentBuffer == null) {
       currentBuffer =
           ChannelBuffers.wrappedBuffer(buffer, ChannelBuffers.wrappedBuffer("=".getBytes()));
       // continue
       size -= buffer.readableBytes() + 1;
     } else {
       currentBuffer =
           ChannelBuffers.wrappedBuffer(
               currentBuffer, buffer, ChannelBuffers.wrappedBuffer("=".getBytes()));
       // continue
       size -= buffer.readableBytes() + 1;
     }
     if (currentBuffer.readableBytes() >= HttpPostBodyUtil.chunkSize) {
       buffer = fillChannelBuffer();
       return new DefaultHttpChunk(buffer);
     }
   }
   try {
     buffer = ((Attribute) currentData).getChunk(size);
   } catch (IOException e) {
     throw new ErrorDataEncoderException(e);
   }
   ChannelBuffer delimiter = null;
   if (buffer.readableBytes() < size) {
     // delimiter
     isKey = true;
     delimiter = iterator.hasNext() ? ChannelBuffers.wrappedBuffer("&".getBytes()) : null;
   }
   if (buffer.capacity() == 0) {
     // end for current InterfaceHttpData, need potentially more data
     currentData = null;
     if (currentBuffer == null) {
       currentBuffer = delimiter;
     } else {
       if (delimiter != null) {
         currentBuffer = ChannelBuffers.wrappedBuffer(currentBuffer, delimiter);
       }
     }
     if (currentBuffer.readableBytes() >= HttpPostBodyUtil.chunkSize) {
       buffer = fillChannelBuffer();
       return new DefaultHttpChunk(buffer);
     }
     return null;
   }
   if (currentBuffer == null) {
     if (delimiter != null) {
       currentBuffer = ChannelBuffers.wrappedBuffer(buffer, delimiter);
     } else {
       currentBuffer = buffer;
     }
   } else {
     if (delimiter != null) {
       currentBuffer = ChannelBuffers.wrappedBuffer(currentBuffer, buffer, delimiter);
     } else {
       currentBuffer = ChannelBuffers.wrappedBuffer(currentBuffer, buffer);
     }
   }
   if (currentBuffer.readableBytes() < HttpPostBodyUtil.chunkSize) {
     // end for current InterfaceHttpData, need more data
     currentData = null;
     isKey = true;
     return null;
   }
   buffer = fillChannelBuffer();
   // size = 0
   return new DefaultHttpChunk(buffer);
 }