View Javadoc

1   /*
2    * $Revision $
3    * ====================================================================
4    *
5    *  Licensed to the Apache Software Foundation (ASF) under one or more
6    *  contributor license agreements.  See the NOTICE file distributed with
7    *  this work for additional information regarding copyright ownership.
8    *  The ASF licenses this file to You under the Apache License, Version 2.0
9    *  (the "License"); you may not use this file except in compliance with
10   *  the License.  You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   *  Unless required by applicable law or agreed to in writing, software
15   *  distributed under the License is distributed on an "AS IS" BASIS,
16   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   *  See the License for the specific language governing permissions and
18   *  limitations under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.conn;
29  
30  import java.io.InputStream;
31  import java.io.IOException;
32  
33  import org.apache.http.annotation.NotThreadSafe;
34  
35  /**
36   * Basic implementation of {@link EofSensorWatcher}. The underlying connection
37   * is released on close or EOF.
38   *
39   * @since 4.0
40   */
41  @NotThreadSafe
42  public class BasicEofSensorWatcher implements EofSensorWatcher {
43  
44      /** The connection to auto-release. */
45      protected final ManagedClientConnection managedConn;
46  
47      /** Whether to keep the connection alive. */
48      protected final boolean attemptReuse;
49  
50      /**
51       * Creates a new watcher for auto-releasing a connection.
52       *
53       * @param conn      the connection to auto-release
54       * @param reuse     whether the connection should be re-used
55       */
56      public BasicEofSensorWatcher(ManagedClientConnection conn,
57                                   boolean reuse) {
58          if (conn == null)
59              throw new IllegalArgumentException
60                  ("Connection may not be null.");
61  
62          managedConn = conn;
63          attemptReuse = reuse;
64      }
65  
66      public boolean eofDetected(InputStream wrapped)
67          throws IOException {
68  
69          try {
70              if (attemptReuse) {
71                  // there may be some cleanup required, such as
72                  // reading trailers after the response body:
73                  wrapped.close();
74                  managedConn.markReusable();
75              }
76          } finally {
77              managedConn.releaseConnection();
78          }
79          return false;
80      }
81  
82      public boolean streamClosed(InputStream wrapped)
83          throws IOException {
84  
85          try {
86              if (attemptReuse) {
87                  // this assumes that closing the stream will
88                  // consume the remainder of the response body:
89                  wrapped.close();
90                  managedConn.markReusable();
91              }
92          } finally {
93              managedConn.releaseConnection();
94          }
95          return false;
96      }
97  
98      public boolean streamAbort(InputStream wrapped)
99          throws IOException {
100 
101         managedConn.abortConnection();
102         return false;
103     }
104 
105 }