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.http;
20  
21  import java.util.Enumeration;
22  
23  import org.eclipse.jetty.client.HttpChannel;
24  import org.eclipse.jetty.client.HttpExchange;
25  import org.eclipse.jetty.client.api.Result;
26  import org.eclipse.jetty.http.HttpFields;
27  import org.eclipse.jetty.http.HttpHeader;
28  import org.eclipse.jetty.http.HttpHeaderValue;
29  
30  public class HttpChannelOverHTTP extends HttpChannel
31  {
32      private final HttpConnectionOverHTTP connection;
33      private final HttpSenderOverHTTP sender;
34      private final HttpReceiverOverHTTP receiver;
35  
36      public HttpChannelOverHTTP(HttpConnectionOverHTTP connection)
37      {
38          super(connection.getHttpDestination());
39          this.connection = connection;
40          this.sender = new HttpSenderOverHTTP(this);
41          this.receiver = new HttpReceiverOverHTTP(this);
42      }
43  
44      public HttpConnectionOverHTTP getHttpConnection()
45      {
46          return connection;
47      }
48  
49      @Override
50      public void send()
51      {
52          HttpExchange exchange = getHttpExchange();
53          if (exchange != null)
54              sender.send(exchange);
55      }
56  
57      @Override
58      public void proceed(HttpExchange exchange, boolean proceed)
59      {
60          sender.proceed(exchange, proceed);
61      }
62  
63      @Override
64      public boolean abort(Throwable cause)
65      {
66          // We want the return value to be that of the response
67          // because if the response has already successfully
68          // arrived then we failed to abort the exchange
69          sender.abort(cause);
70          return receiver.abort(cause);
71      }
72  
73      public void receive()
74      {
75          receiver.receive();
76      }
77  
78      @Override
79      public void exchangeTerminated(Result result)
80      {
81          super.exchangeTerminated(result);
82  
83          if (result.isSucceeded())
84          {
85              HttpFields responseHeaders = result.getResponse().getHeaders();
86              Enumeration<String> values = responseHeaders.getValues(HttpHeader.CONNECTION.asString(), ",");
87              if (values != null)
88              {
89                  while (values.hasMoreElements())
90                  {
91                      if (HttpHeaderValue.CLOSE.asString().equalsIgnoreCase(values.nextElement()))
92                      {
93                          connection.close();
94                          return;
95                      }
96                  }
97              }
98              connection.release();
99          }
100         else
101         {
102             connection.close();
103         }
104     }
105 
106     @Override
107     public String toString()
108     {
109         return String.format("%s@%x", getClass().getSimpleName(), hashCode());
110     }
111 }