View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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.server;
20  
21  import java.io.IOException;
22  import java.nio.ByteBuffer;
23  
24  import org.eclipse.jetty.util.BufferUtil;
25  import org.eclipse.jetty.util.Callback;
26  import org.eclipse.jetty.util.SharedBlockingCallback;
27  import org.eclipse.jetty.util.SharedBlockingCallback.Blocker;
28  import org.eclipse.jetty.util.log.Log;
29  import org.eclipse.jetty.util.log.Logger;
30  
31  public class HttpInputOverHTTP extends HttpInput<ByteBuffer> implements Callback
32  {
33      private static final Logger LOG = Log.getLogger(HttpInputOverHTTP.class);
34      private final SharedBlockingCallback _readBlocker = new SharedBlockingCallback();
35      private final HttpConnection _httpConnection;
36      private ByteBuffer _content;
37  
38      /**
39       * @param httpConnection
40       */
41      public HttpInputOverHTTP(HttpConnection httpConnection)
42      {
43          _httpConnection = httpConnection;
44      }
45  
46      @Override
47      public void recycle()
48      {
49          synchronized (lock())
50          {
51              super.recycle();
52              _content=null;
53          }
54      }
55  
56      @Override
57      protected void blockForContent() throws IOException
58      {
59          while(true)
60          {
61              try (Blocker blocker=_readBlocker.acquire())
62              {            
63                  _httpConnection.fillInterested(blocker);
64                  if (LOG.isDebugEnabled())
65                      LOG.debug("{} block readable on {}",this,blocker);
66                  blocker.block();
67              }
68  
69              Object content=getNextContent();
70              if (content!=null || isFinished())
71                  break;
72          }
73      }
74  
75      @Override
76      public String toString()
77      {
78          return String.format("%s@%x",getClass().getSimpleName(),hashCode());
79      }
80  
81      @Override
82      protected ByteBuffer nextContent() throws IOException
83      {
84          // If we have some content available, return it
85          if (BufferUtil.hasContent(_content))
86              return _content;
87  
88          // No - then we are going to need to parse some more content
89          _content=null;
90          _httpConnection.parseContent();
91          
92          // If we have some content available, return it
93          if (BufferUtil.hasContent(_content))
94              return _content;
95  
96          return null;
97  
98      }
99  
100     @Override
101     protected int remaining(ByteBuffer item)
102     {
103         return item.remaining();
104     }
105 
106     @Override
107     protected int get(ByteBuffer item, byte[] buffer, int offset, int length)
108     {
109         int l = Math.min(item.remaining(), length);
110         item.get(buffer, offset, l);
111         return l;
112     }
113 
114     @Override
115     protected void consume(ByteBuffer item, int length)
116     {
117         item.position(item.position()+length);
118     }
119 
120     @Override
121     public void content(ByteBuffer item)
122     {
123         if (BufferUtil.hasContent(_content))
124             throw new IllegalStateException();
125         _content=item;
126     }
127 
128     @Override
129     protected void unready()
130     {
131         _httpConnection.fillInterested(this);
132     }
133 
134     @Override
135     public void succeeded()
136     {
137         _httpConnection.getHttpChannel().getState().onReadPossible();
138     }
139 
140     @Override
141     public void failed(Throwable x)
142     {
143         super.failed(x);
144         _httpConnection.getHttpChannel().getState().onReadPossible();
145     }
146 }