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.hc.client5.http.classic.methods;
28  
29  import java.net.URI;
30  import java.util.concurrent.atomic.AtomicMarkableReference;
31  
32  import org.apache.hc.client5.http.config.RequestConfig;
33  import org.apache.hc.core5.concurrent.Cancellable;
34  import org.apache.hc.core5.concurrent.CancellableDependency;
35  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
36  
37  public class HttpUriRequestBase extends BasicClassicHttpRequest implements HttpUriRequest, CancellableDependency {
38  
39      private static final long serialVersionUID = 1L;
40  
41      private final AtomicMarkableReference<Cancellable> cancellableRef;
42      private RequestConfig requestConfig;
43  
44      public HttpUriRequestBase(final String method, final URI requestUri) {
45          super(method, requestUri);
46          this.cancellableRef = new AtomicMarkableReference<>(null, false);
47      }
48  
49      @Override
50      public boolean cancel() {
51          while (!cancellableRef.isMarked()) {
52              final Cancellable actualCancellable = cancellableRef.getReference();
53              if (cancellableRef.compareAndSet(actualCancellable, actualCancellable, false, true)) {
54                  if (actualCancellable != null) {
55                      actualCancellable.cancel();
56                  }
57                  return true;
58              }
59          }
60          return false;
61      }
62  
63      @Override
64      public boolean isCancelled() {
65          return cancellableRef.isMarked();
66      }
67  
68      /**
69       * @since 4.2
70       */
71      @Override
72      public void setDependency(final Cancellable cancellable) {
73          final Cancellable actualCancellable = cancellableRef.getReference();
74          if (!cancellableRef.compareAndSet(actualCancellable, cancellable, false, false)) {
75              cancellable.cancel();
76          }
77      }
78  
79      /**
80       * Resets internal state of the request making it reusable.
81       *
82       * @since 4.2
83       */
84      public void reset() {
85          for (;;) {
86              final boolean marked = cancellableRef.isMarked();
87              final Cancellable actualCancellable = cancellableRef.getReference();
88              if (actualCancellable != null) {
89                  actualCancellable.cancel();
90              }
91              if (cancellableRef.compareAndSet(actualCancellable, null, marked, false)) {
92                  break;
93              }
94          }
95      }
96  
97      @Override
98      public void abort() throws UnsupportedOperationException {
99          cancel();
100     }
101 
102     @Override
103     public boolean isAborted() {
104         return isCancelled();
105     }
106 
107     public void setConfig(final RequestConfig requestConfig) {
108         this.requestConfig = requestConfig;
109     }
110 
111     @Override
112     public RequestConfig getConfig() {
113         return requestConfig;
114     }
115 
116     @Override
117     public String toString() {
118         final StringBuilder sb = new StringBuilder();
119         sb.append(getMethod()).append(" ").append(getRequestUri());
120         return sb.toString();
121     }
122 
123 }