https://issues.apache.org/bugzilla/show_bug.cgi?id=52055

--- Comment #2 from Raymond Feng <rf...@apache.org> 2011-10-19 19:05:06 UTC ---
Created attachment 27821
  --> https://issues.apache.org/bugzilla/attachment.cgi?id=27821
Test case

This the war file that contains a simple servlet filter that uses servelet 3.0
async api to echo the posted content. 

package test;

import java.io.IOException;
import java.io.Reader;

import javax.servlet.AsyncContext;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class TestAsyncFilter implements Filter {

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
        ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)request;
        String path = httpServletRequest.getServletPath();
        if ("POST".equalsIgnoreCase(httpServletRequest.getMethod()) &&
path.startsWith("/test")) {
            final AsyncContext asyncContext = request.startAsync();
            asyncContext.start(new Runnable() {

                @Override
                public void run() {
                    try {
                        readContent(asyncContext);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

        } else {
            chain.doFilter(request, response);
        }
    }

    private void readContent(AsyncContext asyncContext) throws IOException {
        HttpServletRequest httpServletRequest =
(HttpServletRequest)asyncContext.getRequest();
        Reader reader = httpServletRequest.getReader();
        StringBuilder sb = new StringBuilder();
        char[] buf = new char[4096];
        while (true) {
            int len = reader.read(buf);
            if (len < 0) {
                break;
            } else {
                sb.append(buf, 0, len);
            }
        }
        System.out.println("Request: " + sb.toString());
        asyncContext.getResponse().getWriter().println(sb.toString());
        asyncContext.complete();
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}


And the test client is:

package test;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class TestHttpClient {
    public static void main(String[] args) throws IOException {
        HttpClient client = new DefaultHttpClient();
        for (int i = 0; i < 5; i++) {
            HttpPost post = new
HttpPost("http://localhost:8080/tomcat7-async/test";);
            StringEntity entity = new StringEntity("Test String: " + i);
            entity.setChunked(true);
            post.setEntity(entity);
            HttpResponse response = client.execute(post);
            System.out.println("Response[" + i + "]: " +
EntityUtils.toString(response.getEntity()));
        }
    }
}

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to