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  
28  package org.apache.http.client.fluent;
29  
30  import java.net.URI;
31  import java.util.concurrent.atomic.AtomicBoolean;
32  import java.util.concurrent.atomic.AtomicReference;
33  
34  import org.apache.http.HttpVersion;
35  import org.apache.http.ProtocolVersion;
36  import org.apache.http.RequestLine;
37  import org.apache.http.annotation.NotThreadSafe;
38  import org.apache.http.client.config.RequestConfig;
39  import org.apache.http.client.methods.Configurable;
40  import org.apache.http.client.methods.HttpExecutionAware;
41  import org.apache.http.client.methods.HttpUriRequest;
42  import org.apache.http.concurrent.Cancellable;
43  import org.apache.http.message.AbstractHttpMessage;
44  import org.apache.http.message.BasicRequestLine;
45  import org.apache.http.util.Args;
46  
47  @NotThreadSafe
48  class InternalHttpRequest extends AbstractHttpMessage
49          implements HttpUriRequest, HttpExecutionAware, Configurable {
50  
51      private final String method;
52      private ProtocolVersion version;
53      private URI uri;
54      private RequestConfig config;
55  
56      private final AtomicBoolean aborted;
57      private final AtomicReference<Cancellable> cancellableRef;
58  
59      InternalHttpRequest(final String method, final URI requestURI) {
60          Args.notBlank(method, "Method");
61          Args.notNull(requestURI, "Request URI");
62          this.method = method;
63          this.uri = requestURI;
64          this.aborted = new AtomicBoolean(false);
65          this.cancellableRef = new AtomicReference<Cancellable>(null);
66      }
67  
68      public void setProtocolVersion(final ProtocolVersion version) {
69          this.version = version;
70      }
71  
72      public ProtocolVersion getProtocolVersion() {
73          return version != null ? version : HttpVersion.HTTP_1_1;
74      }
75  
76      public String getMethod() {
77          return this.method;
78      }
79  
80      public URI getURI() {
81          return this.uri;
82      }
83  
84      public void abort() throws UnsupportedOperationException {
85          if (this.aborted.compareAndSet(false, true)) {
86              final Cancellable cancellable = this.cancellableRef.getAndSet(null);
87              if (cancellable != null) {
88                  cancellable.cancel();
89              }
90          }
91      }
92  
93      public boolean isAborted() {
94          return this.aborted.get();
95      }
96  
97      public void setCancellable(final Cancellable cancellable) {
98          if (!this.aborted.get()) {
99              this.cancellableRef.set(cancellable);
100         }
101     }
102 
103     public RequestLine getRequestLine() {
104         final String method = getMethod();
105         final ProtocolVersion ver = getProtocolVersion();
106         final URI uri = getURI();
107         String uritext = null;
108         if (uri != null) {
109             uritext = uri.toASCIIString();
110         }
111         if (uritext == null || uritext.length() == 0) {
112             uritext = "/";
113         }
114         return new BasicRequestLine(method, uritext, ver);
115     }
116 
117     public RequestConfig getConfig() {
118         return config;
119     }
120 
121     public void setConfig(final RequestConfig config) {
122         this.config = config;
123     }
124 
125     public void setURI(final URI uri) {
126         this.uri = uri;
127     }
128 
129     @Override
130     public String toString() {
131         return getMethod() + " " + getURI() + " " + getProtocolVersion();
132     }
133 
134 }