package com.gm.server;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

public class StreamCopyFilter implements Filter {
  
  private static final Logger log = Logger.getLogger(StreamCopyFilter.class.getName());

  private static class StreamCacheRequest extends HttpServletRequestWrapper {
    
    private final byte[] content;

    public StreamCacheRequest(HttpServletRequest request, byte[] content) {
      super(request);
      this.content = content;
    }

    @Override
    public int getContentLength() {
      return content.length;
    }
    
    @Override
    public ServletInputStream getInputStream() throws IOException {
      return new ServletInputStream() {
        
        private int i = -1;
        
        @Override
        public int read() throws IOException {
          ++i;
          return i < content.length ? content[i] : -1;
        }
      };
    }
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    // TODO Auto-generated method stub

  }

  @Override
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {
    if (request instanceof HttpServletRequest
        && response instanceof HttpServletResponse) {
      byte[] content = readStream(request.getInputStream());
      log.info("read " + new String(content));
      chain.doFilter(new StreamCacheRequest((HttpServletRequest) request, content), response);
    } else {
      chain.doFilter(request, response);
    }
  }

  @Override
  public void destroy() {
    // TODO Auto-generated method stub

  }

  private static byte[] readStream(InputStream in) throws IOException {
    byte[] buf = new byte[1024];
    int count = 0;
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    while ((count = in.read(buf)) != -1)
      out.write(buf, 0, count);
    return out.toByteArray();
  }

}

@@ -103,32 +103,60 @@
 io.InputStream;%0A
+import java.util.Hashtable;%0A
 import java.util
@@ -571,16 +571,53 @@
 esponse;
+%0Aimport javax.servlet.http.HttpUtils;
 %0A%0Apublic
@@ -851,28 +851,469 @@
 ate 
-final byte%5B%5D content
+static class CacheInputStream extends ServletInputStream %7B%0A      %0A      private final byte%5B%5D content;%0A      private int i;%0A      %0A      public CacheInputStream(byte%5B%5D content) %7B%0A        i = -1;%0A        this.content = content;%0A      %7D%0A%0A      @Override%0A      public int read() throws IOException %7B%0A        ++i;%0A        return i %3C content.length ? content%5Bi%5D : -1;%0A      %7D%0A      %0A    %7D%0A    %0A    private final byte%5B%5D content;%0A    private final Hashtable%3C?, ?%3E table
 ;%0A%0A 
@@ -1435,24 +1435,143 @@
  = content;%0A
+      table = HttpUtils.parsePostData(content.length, new CacheInputStream(content));%0A      System.out.println(table);%0A
     %7D%0A%0A    @
@@ -1685,108 +1685,146 @@
 ic S
-ervletInputStream getInputStream() throws IOException %7B%0A      return new ServletInputStream() %7B
+tring getParameter(String name) %7B%0A      String%5B%5D values = getParameterValues(name);%0A      return values == null ? null : values%5B0%5D;
 %0A    
+%7D%0A
     
@@ -1832,45 +1832,124 @@
     
-    private int i = -1;%0A        %0A    
+@Override%0A    public String%5B%5D getParameterValues(String name) %7B%0A      return (String%5B%5D) table.get(name);%0A    %7D%0A    %0A
     
@@ -1958,20 +1958,16 @@
 verride%0A
-    
     publ
@@ -1965,32 +1965,57 @@
 %0A    public 
-int read
+ServletInputStream getInputStream
 () throws IO
@@ -2036,89 +2036,44 @@
     
-    ++i;%0A          return i %3C content.length ? content%5Bi%5D : -1;%0A        %7D%0A      %7D
+return new CacheInputStream(content)
 ;%0A  

fix filter