View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.client;
20  
21  import java.nio.ByteBuffer;
22  import java.util.List;
23  
24  import org.eclipse.jetty.client.api.Request;
25  import org.eclipse.jetty.util.log.Log;
26  import org.eclipse.jetty.util.log.Logger;
27  
28  public class RequestNotifier
29  {
30      private static final Logger LOG = Log.getLogger(ResponseNotifier.class);
31  
32      private final HttpClient client;
33  
34      public RequestNotifier(HttpClient client)
35      {
36          this.client = client;
37      }
38  
39      @SuppressWarnings("ForLoopReplaceableByForEach")
40      public void notifyQueued(Request request)
41      {
42          // Optimized to avoid allocations of iterator instances
43          List<Request.RequestListener> requestListeners = request.getRequestListeners(null);
44          for (int i = 0; i < requestListeners.size(); ++i)
45          {
46              Request.RequestListener listener = requestListeners.get(i);
47              if (listener instanceof Request.QueuedListener)
48                  notifyQueued((Request.QueuedListener)listener, request);
49          }
50          List<Request.Listener> listeners = client.getRequestListeners();
51          for (int i = 0; i < listeners.size(); ++i)
52          {
53              Request.Listener listener = listeners.get(i);
54              notifyQueued(listener, request);
55          }
56      }
57  
58      private void notifyQueued(Request.QueuedListener listener, Request request)
59      {
60          try
61          {
62              listener.onQueued(request);
63          }
64          catch (Exception x)
65          {
66              LOG.info("Exception while notifying listener " + listener, x);
67          }
68      }
69  
70      @SuppressWarnings("ForLoopReplaceableByForEach")
71      public void notifyBegin(Request request)
72      {
73          // Optimized to avoid allocations of iterator instances
74          List<Request.RequestListener> requestListeners = request.getRequestListeners(null);
75          for (int i = 0; i < requestListeners.size(); ++i)
76          {
77              Request.RequestListener listener = requestListeners.get(i);
78              if (listener instanceof Request.BeginListener)
79                  notifyBegin((Request.BeginListener)listener, request);
80          }
81          List<Request.Listener> listeners = client.getRequestListeners();
82          for (int i = 0; i < listeners.size(); ++i)
83          {
84              Request.Listener listener = listeners.get(i);
85              notifyBegin(listener, request);
86          }
87      }
88  
89      private void notifyBegin(Request.BeginListener listener, Request request)
90      {
91          try
92          {
93              listener.onBegin(request);
94          }
95          catch (Exception x)
96          {
97              LOG.info("Exception while notifying listener " + listener, x);
98          }
99      }
100 
101     @SuppressWarnings("ForLoopReplaceableByForEach")
102     public void notifyHeaders(Request request)
103     {
104         // Optimized to avoid allocations of iterator instances
105         List<Request.RequestListener> requestListeners = request.getRequestListeners(null);
106         for (int i = 0; i < requestListeners.size(); ++i)
107         {
108             Request.RequestListener listener = requestListeners.get(i);
109             if (listener instanceof Request.HeadersListener)
110                 notifyHeaders((Request.HeadersListener)listener, request);
111         }
112         List<Request.Listener> listeners = client.getRequestListeners();
113         for (int i = 0; i < listeners.size(); ++i)
114         {
115             Request.Listener listener = listeners.get(i);
116             notifyHeaders(listener, request);
117         }
118     }
119 
120     private void notifyHeaders(Request.HeadersListener listener, Request request)
121     {
122         try
123         {
124             listener.onHeaders(request);
125         }
126         catch (Exception x)
127         {
128             LOG.info("Exception while notifying listener " + listener, x);
129         }
130     }
131 
132     @SuppressWarnings("ForLoopReplaceableByForEach")
133     public void notifyCommit(Request request)
134     {
135         // Optimized to avoid allocations of iterator instances
136         List<Request.RequestListener> requestListeners = request.getRequestListeners(null);
137         for (int i = 0; i < requestListeners.size(); ++i)
138         {
139             Request.RequestListener listener = requestListeners.get(i);
140             if (listener instanceof Request.CommitListener)
141                 notifyCommit((Request.CommitListener)listener, request);
142         }
143         List<Request.Listener> listeners = client.getRequestListeners();
144         for (int i = 0; i < listeners.size(); ++i)
145         {
146             Request.Listener listener = listeners.get(i);
147             notifyCommit(listener, request);
148         }
149     }
150 
151     private void notifyCommit(Request.CommitListener listener, Request request)
152     {
153         try
154         {
155             listener.onCommit(request);
156         }
157         catch (Exception x)
158         {
159             LOG.info("Exception while notifying listener " + listener, x);
160         }
161     }
162 
163     @SuppressWarnings("ForLoopReplaceableByForEach")
164     public void notifyContent(Request request, ByteBuffer content)
165     {
166         // Slice the buffer to avoid that listeners peek into data they should not look at.
167         content = content.slice();
168         // Optimized to avoid allocations of iterator instances.
169         List<Request.RequestListener> requestListeners = request.getRequestListeners(null);
170         for (int i = 0; i < requestListeners.size(); ++i)
171         {
172             Request.RequestListener listener = requestListeners.get(i);
173             if (listener instanceof Request.ContentListener)
174             {
175                 // The buffer was sliced, so we always clear it (position=0, limit=capacity)
176                 // before passing it to the listener that may consume it.
177                 content.clear();
178                 notifyContent((Request.ContentListener)listener, request, content);
179             }
180         }
181         List<Request.Listener> listeners = client.getRequestListeners();
182         for (int i = 0; i < listeners.size(); ++i)
183         {
184             Request.Listener listener = listeners.get(i);
185             // The buffer was sliced, so we always clear it (position=0, limit=capacity)
186             // before passing it to the listener that may consume it.
187             content.clear();
188             notifyContent(listener, request, content);
189         }
190     }
191 
192     private void notifyContent(Request.ContentListener listener, Request request, ByteBuffer content)
193     {
194         try
195         {
196             listener.onContent(request, content);
197         }
198         catch (Exception x)
199         {
200             LOG.info("Exception while notifying listener " + listener, x);
201         }
202     }
203 
204     @SuppressWarnings("ForLoopReplaceableByForEach")
205     public void notifySuccess(Request request)
206     {
207         // Optimized to avoid allocations of iterator instances
208         List<Request.RequestListener> requestListeners = request.getRequestListeners(null);
209         for (int i = 0; i < requestListeners.size(); ++i)
210         {
211             Request.RequestListener listener = requestListeners.get(i);
212             if (listener instanceof Request.SuccessListener)
213                 notifySuccess((Request.SuccessListener)listener, request);
214         }
215         List<Request.Listener> listeners = client.getRequestListeners();
216         for (int i = 0; i < listeners.size(); ++i)
217         {
218             Request.Listener listener = listeners.get(i);
219             notifySuccess(listener, request);
220         }
221     }
222 
223     private void notifySuccess(Request.SuccessListener listener, Request request)
224     {
225         try
226         {
227             listener.onSuccess(request);
228         }
229         catch (Exception x)
230         {
231             LOG.info("Exception while notifying listener " + listener, x);
232         }
233     }
234 
235     @SuppressWarnings("ForLoopReplaceableByForEach")
236     public void notifyFailure(Request request, Throwable failure)
237     {
238         // Optimized to avoid allocations of iterator instances
239         List<Request.RequestListener> requestListeners = request.getRequestListeners(null);
240         for (int i = 0; i < requestListeners.size(); ++i)
241         {
242             Request.RequestListener listener = requestListeners.get(i);
243             if (listener instanceof Request.FailureListener)
244                 notifyFailure((Request.FailureListener)listener, request, failure);
245         }
246         List<Request.Listener> listeners = client.getRequestListeners();
247         for (int i = 0; i < listeners.size(); ++i)
248         {
249             Request.Listener listener = listeners.get(i);
250             notifyFailure(listener, request, failure);
251         }
252     }
253 
254     private void notifyFailure(Request.FailureListener listener, Request request, Throwable failure)
255     {
256         try
257         {
258             listener.onFailure(request, failure);
259         }
260         catch (Exception x)
261         {
262             LOG.info("Exception while notifying listener " + listener, x);
263         }
264     }
265 }