1
15 package com.amazonaws.http.apache.utils;
16
17 import com.amazonaws.Request;
18 import com.amazonaws.SdkClientException;
19 import com.amazonaws.http.HttpResponse;
20 import com.amazonaws.http.settings.HttpClientSettings;
21 import com.amazonaws.util.FakeIOException;
22 import com.amazonaws.util.ReflectionMethodInvoker;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.http.Header;
27 import org.apache.http.HttpEntity;
28 import org.apache.http.HttpHost;
29 import org.apache.http.HttpStatus;
30 import org.apache.http.auth.AuthScope;
31 import org.apache.http.auth.Credentials;
32 import org.apache.http.auth.NTCredentials;
33 import org.apache.http.client.AuthCache;
34 import org.apache.http.client.CredentialsProvider;
35 import org.apache.http.client.config.RequestConfig;
36 import org.apache.http.client.methods.HttpRequestBase;
37 import org.apache.http.client.protocol.HttpClientContext;
38 import org.apache.http.entity.BufferedHttpEntity;
39 import org.apache.http.entity.StringEntity;
40 import org.apache.http.impl.auth.BasicScheme;
41 import org.apache.http.impl.client.BasicAuthCache;
42 import org.apache.http.impl.client.BasicCredentialsProvider;
43
44 import java.io.IOException;
45 import java.io.UnsupportedEncodingException;
46 import java.util.Map;
47 import org.apache.http.protocol.HttpContext;
48
49 public class ApacheUtils {
50 private static final Log log = LogFactory.getLog(ApacheUtils.class);
51
52 private static final ReflectionMethodInvoker<RequestConfig.Builder, RequestConfig.Builder> normalizeUriInvoker;
53
54 static {
55
56
57 normalizeUriInvoker =
58 new ReflectionMethodInvoker<RequestConfig.Builder, RequestConfig.Builder>(RequestConfig.Builder.class,
59 RequestConfig.Builder.class,
60 "setNormalizeUri",
61 boolean.class);
62
63 try {
64 normalizeUriInvoker.initialize();
65 } catch (NoSuchMethodException ignored) {
66 noSuchMethodThrownByNormalizeUriInvoker();
67 }
68 }
69
70 private final boolean normalizeUriMethodNotFound = false;
71
72
78 public static boolean isRequestSuccessful(org.apache.http.HttpResponse response) {
79 int status = response.getStatusLine().getStatusCode();
80 return status / 100 == HttpStatus.SC_OK / 100;
81 }
82
83
95 public static HttpResponse createResponse(Request<?> request,
96 HttpRequestBase method,
97 org.apache.http.HttpResponse apacheHttpResponse,
98 HttpContext context) throws IOException {
99 HttpResponse httpResponse = new HttpResponse(request, method, context);
100
101 if (apacheHttpResponse.getEntity() != null) {
102 httpResponse.setContent(apacheHttpResponse.getEntity().getContent());
103 }
104
105 httpResponse.setStatusCode(apacheHttpResponse.getStatusLine().getStatusCode());
106 httpResponse.setStatusText(apacheHttpResponse.getStatusLine().getReasonPhrase());
107 for (Header header : apacheHttpResponse.getAllHeaders()) {
108 httpResponse.addHeader(header.getName(), header.getValue());
109 }
110
111 return httpResponse;
112 }
113
114
121 public static HttpEntity newStringEntity(String s) {
122 try {
123 return new StringEntity(s);
124 } catch (UnsupportedEncodingException e) {
125 throw new SdkClientException("Unable to create HTTP entity: " + e.getMessage(), e);
126 }
127 }
128
129
137 public static HttpEntity newBufferedHttpEntity(HttpEntity entity) throws
138 FakeIOException {
139 try {
140 return new BufferedHttpEntity(entity);
141 } catch (FakeIOException e) {
142
143 throw e;
144 } catch (IOException e) {
145 throw new SdkClientException("Unable to create HTTP entity: " + e.getMessage(), e);
146 }
147 }
148
149
152 public static HttpClientContext newClientContext(HttpClientSettings settings,
153 Map<String, ? extends Object>
154 attributes) {
155 final HttpClientContext clientContext = new HttpClientContext();
156
157 if (attributes != null && !attributes.isEmpty()) {
158 for (Map.Entry<String, ?> entry : attributes.entrySet()) {
159 clientContext.setAttribute(entry.getKey(), entry.getValue());
160 }
161 }
162
163 addPreemptiveAuthenticationProxy(clientContext, settings);
164
165 RequestConfig.Builder builder = RequestConfig.custom();
166 disableNormalizeUri(builder);
167
168 clientContext.setRequestConfig(builder.build());
169 clientContext.setAttribute(HttpContextUtils.DISABLE_SOCKET_PROXY_PROPERTY, settings.disableSocketProxy());
170 return clientContext;
171
172 }
173
174
187 public static void disableNormalizeUri(RequestConfig.Builder requestConfigBuilder) {
188
189 if (normalizeUriInvoker.isInitialized()) {
190 try {
191 normalizeUriInvoker.invoke(requestConfigBuilder, false);
192 } catch (NoSuchMethodException ignored) {
193 noSuchMethodThrownByNormalizeUriInvoker();
194 }
195 }
196 }
197
198
201 public static CredentialsProvider newProxyCredentialsProvider
202 (HttpClientSettings settings) {
203 final CredentialsProvider provider = new BasicCredentialsProvider();
204 provider.setCredentials(newAuthScope(settings), newNTCredentials(settings));
205 return provider;
206 }
207
208
211 private static Credentials newNTCredentials(HttpClientSettings settings) {
212 return new NTCredentials(settings.getProxyUsername(),
213 settings.getProxyPassword(),
214 settings.getProxyWorkstation(),
215 settings.getProxyDomain());
216 }
217
218
221 private static AuthScope newAuthScope(HttpClientSettings settings) {
222 return new AuthScope(settings.getProxyHost(), settings.getProxyPort());
223 }
224
225 private static void addPreemptiveAuthenticationProxy(HttpClientContext clientContext,
226 HttpClientSettings settings) {
227
228 if (settings.isPreemptiveBasicProxyAuth()) {
229 HttpHost targetHost = new HttpHost(settings.getProxyHost(), settings
230 .getProxyPort());
231 final CredentialsProvider credsProvider = newProxyCredentialsProvider(settings);
232
233 AuthCache authCache = new BasicAuthCache();
234
235 BasicScheme basicAuth = new BasicScheme();
236 authCache.put(targetHost, basicAuth);
237
238 clientContext.setCredentialsProvider(credsProvider);
239 clientContext.setAuthCache(authCache);
240 }
241 }
242
243
244 private static void noSuchMethodThrownByNormalizeUriInvoker() {
245
246 log.warn("NoSuchMethodException was thrown when disabling normalizeUri. This indicates you are using "
247 + "an old version (< 4.5.8) of Apache http client. It is recommended to use http client "
248 + "version >= 4.5.9 to avoid the breaking change introduced in apache client 4.5.7 and "
249 + "the latency in exception handling. See https:
250 + " for more information");
251 }
252 }
253