View Javadoc

1   //========================================================================
2   //Copyright 2008 Mort Bay Consulting Pty. Ltd.
3   //------------------------------------------------------------------------
4   //Licensed under the Apache License, Version 2.0 (the "License");
5   //you may not use this file except in compliance with the License.
6   //You may obtain a copy of the License at
7   //http://www.apache.org/licenses/LICENSE-2.0
8   //Unless required by applicable law or agreed to in writing, software
9   //distributed under the License is distributed on an "AS IS" BASIS,
10  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  //See the License for the specific language governing permissions and
12  //limitations under the License.
13  //========================================================================
14  
15  
16  package org.mortbay.jetty.client;
17  
18  import java.io.IOException;
19  
20  import org.mortbay.io.Buffer;
21  
22  public class HttpEventListenerWrapper implements HttpEventListener
23  {
24      HttpEventListener _listener;
25      boolean _delegatingRequests;
26      boolean _delegatingResponses;
27      boolean _delegationResult = true;
28      private Buffer _version;
29      private int _status;
30      private Buffer _reason;
31  
32      public HttpEventListenerWrapper()
33      {
34          _listener=null;
35          _delegatingRequests=false;
36          _delegatingResponses=false;
37      }
38      
39      public HttpEventListenerWrapper(HttpEventListener eventListener,boolean delegating)
40      {
41          _listener=eventListener;
42          _delegatingRequests=delegating;
43          _delegatingResponses=delegating;
44      }
45      
46      public void setDelegationResult( boolean result )
47      {
48      	_delegationResult = result;
49      }
50      
51      public HttpEventListener getEventListener()
52      {
53          return _listener;
54      }
55  
56      public void setEventListener(HttpEventListener listener)
57      {
58          _listener = listener;
59      }
60  
61      public boolean isDelegatingRequests()
62      {
63          return _delegatingRequests;
64      }
65      
66      public boolean isDelegatingResponses()
67      {
68          return _delegatingResponses;
69      }
70  
71      public void setDelegatingRequests(boolean delegating)
72      {
73          _delegatingRequests = delegating;
74      }
75      
76      public void setDelegatingResponses(boolean delegating)
77      {
78          _delegatingResponses = delegating;
79      }
80      
81      public void onConnectionFailed(Throwable ex)
82      {
83          if (_delegatingRequests)
84              _listener.onConnectionFailed(ex);
85      }
86  
87      public void onException(Throwable ex)
88      {
89          if (_delegatingRequests||_delegatingResponses)
90              _listener.onException(ex);
91      }
92  
93      public void onExpire()
94      {
95          if (_delegatingRequests||_delegatingResponses)
96              _listener.onExpire();
97      }
98  
99      public void onRequestCommitted() throws IOException
100     {
101         if (_delegatingRequests)
102             _listener.onRequestCommitted();
103     }
104 
105     public void onRequestComplete() throws IOException
106     {
107         if (_delegatingRequests)
108             _listener.onRequestComplete();
109     }
110 
111     public void onResponseComplete() throws IOException
112     {
113 		if (_delegatingResponses) 
114 		{
115 			if (_delegationResult == false) 
116 			{
117 				_listener.onResponseStatus(_version, _status, _reason);
118 			}
119 			_listener.onResponseComplete();
120 		}
121     }
122 
123     public void onResponseContent(Buffer content) throws IOException
124     {
125         if (_delegatingResponses)
126             _listener.onResponseContent(content);
127     }
128 
129     public void onResponseHeader(Buffer name, Buffer value) throws IOException
130     {
131         if (_delegatingResponses)
132             _listener.onResponseHeader(name,value);
133     }
134 
135     public void onResponseHeaderComplete() throws IOException
136     {
137         if (_delegatingResponses)
138             _listener.onResponseHeaderComplete();
139     }
140 
141     public void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
142     {
143         if (_delegatingResponses)
144         {
145             _listener.onResponseStatus(version,status,reason);
146         }
147         else
148         {
149             _version = version;
150             _status = status;
151             _reason = reason;
152         }
153     }
154 
155     public void onRetry()
156     {
157         if (_delegatingRequests)
158             _listener.onRetry();
159     }
160     
161     
162     
163 }