View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.http2.client.http;
20  
21  import org.eclipse.jetty.client.HttpChannel;
22  import org.eclipse.jetty.client.HttpDestination;
23  import org.eclipse.jetty.client.HttpExchange;
24  import org.eclipse.jetty.client.HttpReceiver;
25  import org.eclipse.jetty.client.HttpSender;
26  import org.eclipse.jetty.client.api.Result;
27  import org.eclipse.jetty.http2.ErrorCode;
28  import org.eclipse.jetty.http2.api.Session;
29  import org.eclipse.jetty.http2.api.Stream;
30  import org.eclipse.jetty.http2.frames.ResetFrame;
31  import org.eclipse.jetty.util.Callback;
32  
33  public class HttpChannelOverHTTP2 extends HttpChannel
34  {
35      private final HttpConnectionOverHTTP2 connection;
36      private final Session session;
37      private final HttpSenderOverHTTP2 sender;
38      private final HttpReceiverOverHTTP2 receiver;
39      private Stream stream;
40  
41      public HttpChannelOverHTTP2(HttpDestination destination, HttpConnectionOverHTTP2 connection, Session session)
42      {
43          super(destination);
44          this.connection = connection;
45          this.session = session;
46          this.sender = new HttpSenderOverHTTP2(this);
47          this.receiver = new HttpReceiverOverHTTP2(this);
48      }
49  
50      public Session getSession()
51      {
52          return session;
53      }
54  
55      public Stream.Listener getStreamListener()
56      {
57          return receiver;
58      }
59  
60      @Override
61      protected HttpSender getHttpSender()
62      {
63          return sender;
64      }
65  
66      @Override
67      protected HttpReceiver getHttpReceiver()
68      {
69          return receiver;
70      }
71  
72      public Stream getStream()
73      {
74          return stream;
75      }
76  
77      public void setStream(Stream stream)
78      {
79          this.stream = stream;
80      }
81  
82      @Override
83      public void send()
84      {
85          HttpExchange exchange = getHttpExchange();
86          if (exchange != null)
87              sender.send(exchange);
88      }
89  
90      @Override
91      public void release()
92      {
93          connection.release(this);
94      }
95  
96      @Override
97      public boolean abort(HttpExchange exchange, Throwable requestFailure, Throwable responseFailure)
98      {
99          boolean aborted = super.abort(exchange, requestFailure, responseFailure);
100         if (aborted)
101         {
102             Stream stream = getStream();
103             if (stream != null)
104                 stream.reset(new ResetFrame(stream.getId(), ErrorCode.CANCEL_STREAM_ERROR.code), Callback.NOOP);
105         }
106         return aborted;
107     }
108 
109     @Override
110     public void exchangeTerminated(HttpExchange exchange, Result result)
111     {
112         super.exchangeTerminated(exchange, result);
113         release();
114     }
115 }