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.client.methods;
28  
29  import java.io.IOException;
30  import java.util.concurrent.atomic.AtomicBoolean;
31  import java.util.concurrent.atomic.AtomicReference;
32  
33  import org.apache.http.HttpRequest;
34  import org.apache.http.client.utils.CloneUtils;
35  import org.apache.http.concurrent.Cancellable;
36  import org.apache.http.conn.ClientConnectionRequest;
37  import org.apache.http.conn.ConnectionReleaseTrigger;
38  import org.apache.http.message.AbstractHttpMessage;
39  
40  @SuppressWarnings("deprecation")
41  public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage implements
42          HttpExecutionAware, AbortableHttpRequest, Cloneable, HttpRequest {
43  
44      private final AtomicBoolean aborted;
45      private final AtomicReference<Cancellable> cancellableRef;
46  
47      protected AbstractExecutionAwareRequest() {
48          super();
49          this.aborted = new AtomicBoolean(false);
50          this.cancellableRef = new AtomicReference<Cancellable>(null);
51      }
52  
53      @Deprecated
54      public void setConnectionRequest(final ClientConnectionRequest connRequest) {
55          setCancellable(new Cancellable() {
56  
57              public boolean cancel() {
58                  connRequest.abortRequest();
59                  return true;
60              }
61  
62          });
63      }
64  
65      @Deprecated
66      public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
67          setCancellable(new Cancellable() {
68  
69              public boolean cancel() {
70                  try {
71                      releaseTrigger.abortConnection();
72                      return true;
73                  } catch (final IOException ex) {
74                      return false;
75                  }
76              }
77  
78          });
79      }
80  
81      public void abort() {
82          if (this.aborted.compareAndSet(false, true)) {
83              final Cancellable cancellable = this.cancellableRef.getAndSet(null);
84              if (cancellable != null) {
85                  cancellable.cancel();
86              }
87          }
88      }
89  
90      public boolean isAborted() {
91          return this.aborted.get();
92      }
93  
94      /**
95       * @since 4.2
96       */
97      public void setCancellable(final Cancellable cancellable) {
98          if (!this.aborted.get()) {
99              this.cancellableRef.set(cancellable);
100         }
101     }
102 
103     @Override
104     public Object clone() throws CloneNotSupportedException {
105         final AbstractExecutionAwareRequest clone = (AbstractExecutionAwareRequest) super.clone();
106         clone.headergroup = CloneUtils.cloneObject(this.headergroup);
107         clone.params = CloneUtils.cloneObject(this.params);
108         return clone;
109     }
110 
111     /**
112      * @since 4.2
113      */
114     public void completed() {
115         this.cancellableRef.set(null);
116     }
117 
118     /**
119      * Resets internal state of the request making it reusable.
120      *
121      * @since 4.2
122      */
123     public void reset() {
124         final Cancellable cancellable = this.cancellableRef.getAndSet(null);
125         if (cancellable != null) {
126             cancellable.cancel();
127         }
128         this.aborted.set(false);
129     }
130 
131 }