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      @Override
73      public ProtocolVersion getProtocolVersion() {
74          return version != null ? version : HttpVersion.HTTP_1_1;
75      }
76  
77      @Override
78      public String getMethod() {
79          return this.method;
80      }
81  
82      @Override
83      public URI getURI() {
84          return this.uri;
85      }
86  
87      @Override
88      public void abort() throws UnsupportedOperationException {
89          if (this.aborted.compareAndSet(false, true)) {
90              final Cancellable cancellable = this.cancellableRef.getAndSet(null);
91              if (cancellable != null) {
92                  cancellable.cancel();
93              }
94          }
95      }
96  
97      @Override
98      public boolean isAborted() {
99          return this.aborted.get();
100     }
101 
102     @Override
103     public void setCancellable(final Cancellable cancellable) {
104         if (!this.aborted.get()) {
105             this.cancellableRef.set(cancellable);
106         }
107     }
108 
109     @Override
110     public RequestLine getRequestLine() {
111         final ProtocolVersion ver = getProtocolVersion();
112         final URI uriCopy = getURI();
113         String uritext = null;
114         if (uriCopy != null) {
115             uritext = uriCopy.toASCIIString();
116         }
117         if (uritext == null || uritext.isEmpty()) {
118             uritext = "/";
119         }
120         return new BasicRequestLine(getMethod(), uritext, ver);
121     }
122 
123     @Override
124     public RequestConfig getConfig() {
125         return config;
126     }
127 
128     public void setConfig(final RequestConfig config) {
129         this.config = config;
130     }
131 
132     public void setURI(final URI uri) {
133         this.uri = uri;
134     }
135 
136     @Override
137     public String toString() {
138         return getMethod() + " " + getURI() + " " + getProtocolVersion();
139     }
140 
141 }