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.size() > 0) {
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);
}
List<? extends Part> parts = getParts(invocation, ImmutableMultimap.<String, Object> builder()
.putAll(tokenValues).putAll(formParams).build());
if (parts.size() > 0) {
if (formParams.size() > 0) {
parts = newLinkedList(concat(transform(formParams.entries(), ENTRY_TO_PART), parts));
}
payload = new MultipartForm(MultipartForm.BOUNDARY, parts);
} else if (formParams.size() > 0) {
payload = Payloads.newUrlEncodedFormPayload(transformValues(formParams, NullableToStringFunction.INSTANCE));
} else if (headers.containsKey(CONTENT_TYPE) && !HttpRequest.NON_PAYLOAD_METHODS.contains(requestMethod)) {
if (payload == null)
payload = Payloads.newByteArrayPayload(new byte[] {});
payload.getContentMetadata().setContentType(get(headers.get(CONTENT_TYPE), 0));
}
if (payload != null) {
requestBuilder.payload(payload);
}
GeneratedHttpRequest request = requestBuilder.build();