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      @Override
67      public boolean eofDetected(final InputStream wrapped)
68          throws IOException {
69  
70          try {
71              if (attemptReuse) {
72                  // there may be some cleanup required, such as
73                  // reading trailers after the response body:
74                  wrapped.close();
75                  managedConn.markReusable();
76              }
77          } finally {
78              managedConn.releaseConnection();
79          }
80          return false;
81      }
82  
83      @Override
84      public boolean streamClosed(final InputStream wrapped)
85          throws IOException {
86  
87          try {
88              if (attemptReuse) {
89                  // this assumes that closing the stream will
90                  // consume the remainder of the response body:
91                  wrapped.close();
92                  managedConn.markReusable();
93              }
94          } finally {
95              managedConn.releaseConnection();
96          }
97          return false;
98      }
99  
100     @Override
101     public boolean streamAbort(final InputStream wrapped)
102         throws IOException {
103 
104         managedConn.abortConnection();
105         return false;
106     }
107 
108 }