Multimap<String, Object> tokenValues = LinkedHashMultimap.create();
tokenValues.put(Constants.PROPERTY_API_VERSION, apiVersion);
tokenValues.put(Constants.PROPERTY_BUILD_VERSION, buildVersion);
// URI template in rfc6570 form
UriBuilder uriBuilder = uriBuilder(endpoint.get().toString());
overridePathEncoding(uriBuilder, invocation);
if (caller != null)
tokenValues.putAll(addPathAndGetTokens(caller, uriBuilder));
tokenValues.putAll(addPathAndGetTokens(invocation, uriBuilder));
Multimap<String, Object> formParams;
if (caller != null) {
formParams = addFormParams(tokenValues, caller);
formParams.putAll(addFormParams(tokenValues, invocation));
} else {
formParams = addFormParams(tokenValues, invocation);
}
Multimap<String, Object> queryParams = addQueryParams(tokenValues, invocation);
Multimap<String, String> headers;
if (caller != null) {
headers = buildHeaders(tokenValues, caller);
headers.putAll(buildHeaders(tokenValues, invocation));
} else {
headers = buildHeaders(tokenValues, invocation);
}
if (r != null)
headers.putAll(r.getHeaders());
if (shouldAddHostHeader(invocation)) {
StringBuilder hostHeader = new StringBuilder(endpoint.get().getHost());
if (endpoint.get().getPort() != -1)
hostHeader.append(":").append(endpoint.get().getPort());
headers.put(HOST, hostHeader.toString());
}
Payload payload = null;
for (HttpRequestOptions options : findOptionsIn(invocation)) {
injector.injectMembers(options); // TODO test case
for (Entry<String, String> header : options.buildRequestHeaders().entries()) {
headers.put(header.getKey(), replaceTokens(header.getValue(), tokenValues));
}
for (Entry<String, String> query : options.buildQueryParameters().entries()) {
queryParams.put(query.getKey(), replaceTokens(query.getValue(), tokenValues));
}
for (Entry<String, String> form : options.buildFormParameters().entries()) {
formParams.put(form.getKey(), replaceTokens(form.getValue(), tokenValues));
}
String pathSuffix = options.buildPathSuffix();
if (pathSuffix != null) {
uriBuilder.appendPath(pathSuffix);
}
String stringPayload = options.buildStringPayload();
if (stringPayload != null)
payload = Payloads.newStringPayload(stringPayload);
}
if (!queryParams.isEmpty()) {
uriBuilder.query(queryParams);
}
requestBuilder.headers(filterOutContentHeaders(headers));
requestBuilder.endpoint(uriBuilder.build(convertUnsafe(tokenValues)));
if (payload == null) {
PayloadEnclosing payloadEnclosing = findOrNull(invocation.getArgs(), PayloadEnclosing.class);
payload = (payloadEnclosing != null) ? payloadEnclosing.getPayload() : findOrNull(invocation.getArgs(),
Payload.class);