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      @Override
54      @Deprecated
55      public void setConnectionRequest(final ClientConnectionRequest connRequest) {
56          setCancellable(new Cancellable() {
57  
58              @Override
59              public boolean cancel() {
60                  connRequest.abortRequest();
61                  return true;
62              }
63  
64          });
65      }
66  
67      @Override
68      @Deprecated
69      public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
70          setCancellable(new Cancellable() {
71  
72              @Override
73              public boolean cancel() {
74                  try {
75                      releaseTrigger.abortConnection();
76                      return true;
77                  } catch (final IOException ex) {
78                      return false;
79                  }
80              }
81  
82          });
83      }
84  
85      @Override
86      public void abort() {
87          if (this.aborted.compareAndSet(false, true)) {
88              final Cancellable cancellable = this.cancellableRef.getAndSet(null);
89              if (cancellable != null) {
90                  cancellable.cancel();
91              }
92          }
93      }
94  
95      @Override
96      public boolean isAborted() {
97          return this.aborted.get();
98      }
99  
100     /**
101      * @since 4.2
102      */
103     @Override
104     public void setCancellable(final Cancellable cancellable) {
105         if (!this.aborted.get()) {
106             this.cancellableRef.set(cancellable);
107         }
108     }
109 
110     @Override
111     public Object clone() throws CloneNotSupportedException {
112         final AbstractExecutionAwareRequest clone = (AbstractExecutionAwareRequest) super.clone();
113         clone.headergroup = CloneUtils.cloneObject(this.headergroup);
114         clone.params = CloneUtils.cloneObject(this.params);
115         return clone;
116     }
117 
118     /**
119      * @since 4.2
120      */
121     public void completed() {
122         this.cancellableRef.set(null);
123     }
124 
125     /**
126      * Resets internal state of the request making it reusable.
127      *
128      * @since 4.2
129      */
130     public void reset() {
131         final Cancellable cancellable = this.cancellableRef.getAndSet(null);
132         if (cancellable != null) {
133             cancellable.cancel();
134         }
135         this.aborted.set(false);
136     }
137 
138 }