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  /**
38   * Base class for HTTP method subclasses.
39   */
40  public class HttpUriRequestBase extends BasicClassicHttpRequest implements HttpUriRequest, CancellableDependency {
41  
42      private static final long serialVersionUID = 1L;
43  
44      private final AtomicMarkableReference<Cancellable> cancellableRef;
45      private RequestConfig requestConfig;
46  
47      /**
48       * Constructs a new instance with the given method and request URI.
49       *
50       * @param method request method.
51       * @param requestUri request URI.
52       */
53      public HttpUriRequestBase(final String method, final URI requestUri) {
54          super(method, requestUri);
55          this.cancellableRef = new AtomicMarkableReference<>(null, false);
56      }
57  
58      @Override
59      public boolean cancel() {
60          while (!cancellableRef.isMarked()) {
61              final Cancellable actualCancellable = cancellableRef.getReference();
62              if (cancellableRef.compareAndSet(actualCancellable, actualCancellable, false, true)) {
63                  if (actualCancellable != null) {
64                      actualCancellable.cancel();
65                  }
66                  return true;
67              }
68          }
69          return false;
70      }
71  
72      @Override
73      public boolean isCancelled() {
74          return cancellableRef.isMarked();
75      }
76  
77      /**
78       * @since 4.2
79       */
80      @Override
81      public void setDependency(final Cancellable cancellable) {
82          final Cancellable actualCancellable = cancellableRef.getReference();
83          if (!cancellableRef.compareAndSet(actualCancellable, cancellable, false, false)) {
84              cancellable.cancel();
85          }
86      }
87  
88      /**
89       * Resets internal state of the request making it reusable.
90       *
91       * @since 4.2
92       */
93      public void reset() {
94          for (;;) {
95              final boolean marked = cancellableRef.isMarked();
96              final Cancellable actualCancellable = cancellableRef.getReference();
97              if (actualCancellable != null) {
98                  actualCancellable.cancel();
99              }
100             if (cancellableRef.compareAndSet(actualCancellable, null, marked, false)) {
101                 break;
102             }
103         }
104     }
105 
106     @Override
107     public void abort() throws UnsupportedOperationException {
108         cancel();
109     }
110 
111     @Override
112     public boolean isAborted() {
113         return isCancelled();
114     }
115 
116     /**
117      * Sets the request configuration.
118      *
119      * @param requestConfig the request configuration.
120      */
121     public void setConfig(final RequestConfig requestConfig) {
122         this.requestConfig = requestConfig;
123     }
124 
125     @Override
126     public RequestConfig getConfig() {
127         return requestConfig;
128     }
129 
130     @Override
131     public String toString() {
132         final StringBuilder sb = new StringBuilder();
133         sb.append(getMethod()).append(" ").append(getRequestUri());
134         return sb.toString();
135     }
136 
137 }