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