View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * 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  package org.apache.http.conn;
28  
29  import java.io.IOException;
30  import java.io.InputStream;
31  
32  import org.apache.http.annotation.NotThreadSafe;
33  import org.apache.http.util.Args;
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   * @deprecated (4.3) do not use.
42   */
43  @Deprecated
44  @NotThreadSafe
45  public class BasicEofSensorWatcher implements EofSensorWatcher {
46  
47      /** The connection to auto-release. */
48      protected final ManagedClientConnection managedConn;
49  
50      /** Whether to keep the connection alive. */
51      protected final boolean attemptReuse;
52  
53      /**
54       * Creates a new watcher for auto-releasing a connection.
55       *
56       * @param conn      the connection to auto-release
57       * @param reuse     whether the connection should be re-used
58       */
59      public BasicEofSensorWatcher(final ManagedClientConnection conn,
60                                   final boolean reuse) {
61          Args.notNull(conn, "Connection");
62          managedConn = conn;
63          attemptReuse = reuse;
64      }
65  
66      public boolean eofDetected(final 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(final 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(final InputStream wrapped)
99          throws IOException {
100 
101         managedConn.abortConnection();
102         return false;
103     }
104 
105 }