URL currentURL = setupURL(message);
// The need to cache the request is off by default
boolean needToCacheRequest = false;
HTTPClientPolicy csPolicy = getClient(message);
HttpURLConnectionFactory f = getConnectionFactory(currentURL);
HttpURLConnection connection = f.createConnection(getProxy(csPolicy), currentURL);
connection.setDoOutput(true);
long timeout = csPolicy.getConnectionTimeout();
if (timeout > Integer.MAX_VALUE) {
timeout = Integer.MAX_VALUE;
}
connection.setConnectTimeout((int)timeout);
timeout = csPolicy.getReceiveTimeout();
if (timeout > Integer.MAX_VALUE) {
timeout = Integer.MAX_VALUE;
}
connection.setReadTimeout((int)timeout);
connection.setUseCaches(false);
// We implement redirects in this conduit. We do not
// rely on the underlying URLConnection implementation
// because of trust issues.
connection.setInstanceFollowRedirects(false);
// If the HTTP_REQUEST_METHOD is not set, the default is "POST".
String httpRequestMethod =
(String)message.get(Message.HTTP_REQUEST_METHOD);
if (null != httpRequestMethod) {
connection.setRequestMethod(httpRequestMethod);
} else {
connection.setRequestMethod("POST");
}
boolean isChunking = false;
int chunkThreshold = 0;
// We must cache the request if we have basic auth supplier
// without preemptive basic auth.
if (authSupplier != null) {
String auth = authSupplier.getPreemptiveAuthorization(
this, currentURL, message);
if (auth == null || authSupplier.requiresRequestCaching()) {
needToCacheRequest = true;
isChunking = false;
LOG.log(Level.FINE,
"Auth Supplier, but no Premeptive User Pass or Digest auth (nonce may be stale)"
+ " We must cache request.");
}
message.put("AUTH_VALUE", auth);
}
if (csPolicy.isAutoRedirect()) {
needToCacheRequest = true;
LOG.log(Level.FINE, "AutoRedirect is turned on.");
}
if (csPolicy.getMaxRetransmits() > 0) {
needToCacheRequest = true;
LOG.log(Level.FINE, "MaxRetransmits is set > 0.");
}
// DELETE does not work and empty PUTs cause misleading exceptions
// if chunking is enabled
// TODO : ensure chunking can be enabled for non-empty PUTs - if requested
if (connection.getRequestMethod().equals("POST")
&& csPolicy.isAllowChunking()) {
//TODO: The chunking mode be configured or at least some
// documented client constant.
//use -1 and allow the URL connection to pick a default value
isChunking = true;
chunkThreshold = csPolicy.getChunkingThreshold();
if (chunkThreshold <= 0) {
chunkThreshold = 0;
connection.setChunkedStreamingMode(-1);
}
}