View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  package org.eclipse.jetty.client;
14  
15  import java.io.IOException;
16  
17  import org.eclipse.jetty.http.HttpFields;
18  import org.eclipse.jetty.io.Buffer;
19  
20  /**
21   * An exchange that caches response status and fields for later use.
22   * 
23   * 
24   *
25   */
26  public class CachedExchange extends HttpExchange
27  {
28      int _responseStatus;
29      HttpFields _responseFields;
30  
31      public CachedExchange(boolean cacheFields)
32      {
33          if (cacheFields)
34              _responseFields = new HttpFields();
35      }
36  
37      /* ------------------------------------------------------------ */
38      public int getResponseStatus()
39      {
40          if (_status < HttpExchange.STATUS_PARSING_HEADERS)
41              throw new IllegalStateException("Response not received");
42          return _responseStatus;
43      }
44  
45      /* ------------------------------------------------------------ */
46      public HttpFields getResponseFields()
47      {
48          if (_status < HttpExchange.STATUS_PARSING_CONTENT)
49              throw new IllegalStateException("Headers not complete");
50          return _responseFields;
51      }
52  
53      /* ------------------------------------------------------------ */
54      protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
55      {
56          _responseStatus = status;
57          super.onResponseStatus(version,status,reason);
58      }
59  
60      /* ------------------------------------------------------------ */
61      protected void onResponseHeader(Buffer name, Buffer value) throws IOException
62      {
63          if (_responseFields != null)
64              _responseFields.add(name,value);
65          super.onResponseHeader(name,value);
66      }
67  
68  }