001    /**
002     * 
003     */
004    package org.apache.geronimo;
005    
006    import java.io.IOException;
007    import java.io.OutputStream;
008    import java.io.PrintWriter;
009    import java.util.Date;
010    
011    import javax.servlet.http.HttpServletResponse;
012    
013    import org.apache.ahc.AsyncHttpClientCallback;
014    import org.apache.ahc.codec.HttpResponseMessage;
015    
016    public class Callback implements AsyncHttpClientCallback {
017            private boolean timeout = false;
018            private boolean closed = false;
019            private boolean exception = false;
020            private Throwable throwable = null;
021      private HttpServletResponse response;
022            private HttpResponseMessage message;
023            private long time;
024            private String sessionId;
025    
026            public Callback(HttpServletResponse response, String sessionId) {
027                    this.response = response;
028                    this.sessionId = sessionId;
029                    clear();
030            }
031            
032            /**
033             * @param time
034             */
035            public void setTime(long time){
036                    this.time = time;
037            }
038            
039            
040            /* (non-Javadoc)
041             * @see org.apache.ahc.AsyncHttpClientCallback#onResponse(org.apache.ahc.codec.HttpResponseMessage)
042             */
043            public void onResponse(HttpResponseMessage msg) {               
044        System.out.println("  onResponse()" + " - Status:" + msg.getStatusCode() + " - Length:" + msg.getContentLength() + " " + sessionId);
045    
046        if (timeout) {
047            System.out.println("  Already responded to timeout " + sessionId);
048            return;
049        }
050        
051        response.setStatus(msg.getStatusCode());
052        response.setContentType(msg.getContentType());
053                    if (msg.getContentLength() > 0) {
054                            response.setContentLength(msg.getContentLength());
055                            OutputStream out = null;
056                            try {
057                                    out = response.getOutputStream();
058            out.write(msg.getContent());
059                                    out.flush();
060                                    out.close();
061                            } catch (IOException e) {
062                                    // TODO Auto-generated catch block
063                                    e.printStackTrace();
064                            }
065                    }
066                    System.out.println("  Responded to " + sessionId + " in " + (new Date().getTime() - this.time));
067            }
068    
069            public void onException(Throwable cause) {
070                    throwable = cause;
071                    exception = true;
072                    System.out.println("  Exception by " + sessionId + " in " + (new Date().getTime() - this.time));                
073                    cause.printStackTrace();                
074            }
075    
076            public void onClosed() {
077          closed = true;
078            }
079    
080            public void onTimeout() {
081                    timeout = true;
082        try {
083                            response.setContentType("text/plain");
084                            response.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT);
085                            PrintWriter out = response.getWriter();
086                            out.println("Request Timed out!");
087                            out.println(new Date().toString());
088                            out.close();
089                    } catch (IOException e) {
090                            // TODO Auto-generated catch block
091                            e.printStackTrace();
092                    }
093                    System.out.println("  Timed out " + sessionId + " in " + (new Date().getTime() - this.time));
094            }
095    
096    
097            public Throwable getThrowable() {
098                    return throwable;
099            }
100    
101            public void clear() {
102                    closed = false;
103                    timeout = false;
104                    exception = false;
105                    message = null;
106            }
107    
108            public boolean isClosed() {
109                    return closed;
110            }
111    
112            public void setClosed(boolean closed) {
113                    this.closed = closed;
114            }
115    
116            public boolean isTimeout() {
117                    return timeout;
118            }
119    
120            public boolean isException() {
121                    return exception;
122            }
123    
124            public void setException(boolean exception) {
125                    this.exception = exception;
126            }
127    
128            public HttpResponseMessage getMessage() {
129                    return message;
130            }
131    
132            public void setMessage(HttpResponseMessage message) {
133                    this.message = message;
134            }
135    
136    }