View Javadoc

1   /**
2    * 
3    */
4   package org.apache.geronimo;
5   
6   import java.io.IOException;
7   import java.io.OutputStream;
8   import java.io.PrintWriter;
9   import java.util.Date;
10  
11  import javax.servlet.http.HttpServletResponse;
12  
13  import org.apache.ahc.AsyncHttpClientCallback;
14  import org.apache.ahc.codec.HttpResponseMessage;
15  
16  public class Callback implements AsyncHttpClientCallback {
17  	private boolean timeout = false;
18  	private boolean closed = false;
19  	private boolean exception = false;
20  	private Throwable throwable = null;
21    private HttpServletResponse response;
22  	private HttpResponseMessage message;
23  	private long time;
24  	private String sessionId;
25  
26  	public Callback(HttpServletResponse response, String sessionId) {
27  		this.response = response;
28  		this.sessionId = sessionId;
29  		clear();
30  	}
31  	
32  	/**
33  	 * @param time
34  	 */
35  	public void setTime(long time){
36  		this.time = time;
37  	}
38  	
39  	
40  	/* (non-Javadoc)
41  	 * @see org.apache.ahc.AsyncHttpClientCallback#onResponse(org.apache.ahc.codec.HttpResponseMessage)
42  	 */
43  	public void onResponse(HttpResponseMessage msg) {		
44      System.out.println("  onResponse()" + " - Status:" + msg.getStatusCode() + " - Length:" + msg.getContentLength() + " " + sessionId);
45  
46      if (timeout) {
47          System.out.println("  Already responded to timeout " + sessionId);
48          return;
49      }
50      
51      response.setStatus(msg.getStatusCode());
52      response.setContentType(msg.getContentType());
53  		if (msg.getContentLength() > 0) {
54  			response.setContentLength(msg.getContentLength());
55  			OutputStream out = null;
56  			try {
57  				out = response.getOutputStream();
58          out.write(msg.getContent());
59  				out.flush();
60  				out.close();
61  			} catch (IOException e) {
62  				// TODO Auto-generated catch block
63  				e.printStackTrace();
64  			}
65  		}
66  		System.out.println("  Responded to " + sessionId + " in " + (new Date().getTime() - this.time));
67  	}
68  
69  	public void onException(Throwable cause) {
70  		throwable = cause;
71  		exception = true;
72  		System.out.println("  Exception by " + sessionId + " in " + (new Date().getTime() - this.time));		
73  		cause.printStackTrace();		
74  	}
75  
76  	public void onClosed() {
77        closed = true;
78  	}
79  
80  	public void onTimeout() {
81  		timeout = true;
82      try {
83  			response.setContentType("text/plain");
84  			response.setStatus(HttpServletResponse.SC_REQUEST_TIMEOUT);
85  			PrintWriter out = response.getWriter();
86  			out.println("Request Timed out!");
87  			out.println(new Date().toString());
88  			out.close();
89  		} catch (IOException e) {
90  			// TODO Auto-generated catch block
91  			e.printStackTrace();
92  		}
93  		System.out.println("  Timed out " + sessionId + " in " + (new Date().getTime() - this.time));
94  	}
95  
96  
97  	public Throwable getThrowable() {
98  		return throwable;
99  	}
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 }