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.AtomicMarkableReference;
31  
32  import org.apache.http.HttpRequest;
33  import org.apache.http.client.utils.CloneUtils;
34  import org.apache.http.concurrent.Cancellable;
35  import org.apache.http.conn.ClientConnectionRequest;
36  import org.apache.http.conn.ConnectionReleaseTrigger;
37  import org.apache.http.message.AbstractHttpMessage;
38  
39  @SuppressWarnings("deprecation")
40  public abstract class AbstractExecutionAwareRequest extends AbstractHttpMessage implements
41          HttpExecutionAware, AbortableHttpRequest, Cloneable, HttpRequest {
42  
43      private final AtomicMarkableReference<Cancellable> cancellableRef;
44  
45      protected AbstractExecutionAwareRequest() {
46          super();
47          this.cancellableRef = new AtomicMarkableReference<Cancellable>(null, false);
48      }
49  
50      /**
51       * @deprecated Use {@link #setCancellable(Cancellable)}
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      /**
68       * @deprecated Use {@link #setCancellable(Cancellable)}
69       */
70      @Override
71      @Deprecated
72      public void setReleaseTrigger(final ConnectionReleaseTrigger releaseTrigger) {
73          setCancellable(new Cancellable() {
74  
75              @Override
76              public boolean cancel() {
77                  try {
78                      releaseTrigger.abortConnection();
79                      return true;
80                  } catch (final IOException ex) {
81                      return false;
82                  }
83              }
84  
85          });
86      }
87  
88      @Override
89      public void abort() {
90          while (!cancellableRef.isMarked()) {
91              final Cancellable actualCancellable = cancellableRef.getReference();
92              if (cancellableRef.compareAndSet(actualCancellable, actualCancellable, false, true)) {
93                  if (actualCancellable != null) {
94                      actualCancellable.cancel();
95                  }
96              }
97          }
98      }
99  
100     @Override
101     public boolean isAborted() {
102         return this.cancellableRef.isMarked();
103     }
104 
105     /**
106      * @since 4.2
107      */
108     @Override
109     public void setCancellable(final Cancellable cancellable) {
110         final Cancellable actualCancellable = cancellableRef.getReference();
111         if (!cancellableRef.compareAndSet(actualCancellable, cancellable, false, false)) {
112             cancellable.cancel();
113         }
114     }
115 
116     @Override
117     public Object clone() throws CloneNotSupportedException {
118         final AbstractExecutionAwareRequestche/http/client/methods/AbstractExecutionAwareRequest.html#AbstractExecutionAwareRequest">AbstractExecutionAwareRequest clone = (AbstractExecutionAwareRequest) super.clone();
119         clone.headergroup = CloneUtils.cloneObject(this.headergroup);
120         clone.params = CloneUtils.cloneObject(this.params);
121         return clone;
122     }
123 
124     /**
125      * @since 4.2
126      *
127      * @deprecated Do not use.
128      */
129     @Deprecated
130     public void completed() {
131         this.cancellableRef.set(null, false);
132     }
133 
134     /**
135      * Resets internal state of the request making it reusable.
136      *
137      * @since 4.2
138      */
139     public void reset() {
140         for (;;) {
141             final boolean marked = cancellableRef.isMarked();
142             final Cancellable actualCancellable = cancellableRef.getReference();
143             if (actualCancellable != null) {
144                 actualCancellable.cancel();
145             }
146             if (cancellableRef.compareAndSet(actualCancellable, null, marked, false)) {
147                 break;
148             }
149         }
150     }
151 
152 }