* The {@link Series} to copy the headers to.
*/
@SuppressWarnings("unchecked")
public static void addRequestHeaders(Request request,
Series<Parameter> headers) {
ClientInfo clientInfo = request.getClientInfo();
if (!clientInfo.getAcceptedMediaTypes().isEmpty()) {
addHeader(HeaderConstants.HEADER_ACCEPT,
PreferenceWriter.write(clientInfo.getAcceptedMediaTypes()),
headers);
} else {
addHeader(HeaderConstants.HEADER_ACCEPT, MediaType.ALL.getName(),
headers);
}
if (!clientInfo.getAcceptedCharacterSets().isEmpty()) {
addHeader(HeaderConstants.HEADER_ACCEPT_CHARSET,
PreferenceWriter.write(clientInfo
.getAcceptedCharacterSets()), headers);
}
if (!clientInfo.getAcceptedEncodings().isEmpty()) {
addHeader(HeaderConstants.HEADER_ACCEPT_ENCODING,
PreferenceWriter.write(clientInfo.getAcceptedEncodings()),
headers);
}
if (!clientInfo.getAcceptedLanguages().isEmpty()) {
addHeader(HeaderConstants.HEADER_ACCEPT_LANGUAGE,
PreferenceWriter.write(clientInfo.getAcceptedLanguages()),
headers);
}
if (!clientInfo.getExpectations().isEmpty()) {
addHeader(HeaderConstants.HEADER_EXPECT,
ExpectationWriter.write(clientInfo.getExpectations()),
headers);
}
if (clientInfo.getFrom() != null) {
addHeader(HeaderConstants.HEADER_FROM, request.getClientInfo()
.getFrom(), headers);
}
// Manually add the host name and port when it is potentially
// different from the one specified in the target resource reference.
Reference hostRef = (request.getResourceRef().getBaseRef() != null) ? request
.getResourceRef().getBaseRef() : request.getResourceRef();
if (hostRef.getHostDomain() != null) {
String host = hostRef.getHostDomain();
int hostRefPortValue = hostRef.getHostPort();
if ((hostRefPortValue != -1)
&& (hostRefPortValue != request.getProtocol()
.getDefaultPort())) {
host = host + ':' + hostRefPortValue;
}
addHeader(HeaderConstants.HEADER_HOST, host, headers);
}
Conditions conditions = request.getConditions();
addHeader(HeaderConstants.HEADER_IF_MATCH,
TagWriter.write(conditions.getMatch()), headers);
addHeader(HeaderConstants.HEADER_IF_NONE_MATCH,
TagWriter.write(conditions.getNoneMatch()), headers);
if (conditions.getModifiedSince() != null) {
addHeader(HeaderConstants.HEADER_IF_MODIFIED_SINCE,
DateWriter.write(conditions.getModifiedSince()), headers);
}
if (conditions.getRangeTag() != null
&& conditions.getRangeDate() != null) {
Context.getCurrentLogger()
.log(Level.WARNING,
"Unable to format the HTTP If-Range header due to the presence of both entity tag and modification date.");
} else if (conditions.getRangeTag() != null) {
addHeader(HeaderConstants.HEADER_IF_RANGE,
TagWriter.write(conditions.getRangeTag()), headers);
} else if (conditions.getRangeDate() != null) {
addHeader(HeaderConstants.HEADER_IF_RANGE,
DateWriter.write(conditions.getRangeDate()), headers);
}
if (conditions.getUnmodifiedSince() != null) {
addHeader(HeaderConstants.HEADER_IF_UNMODIFIED_SINCE,
DateWriter.write(conditions.getUnmodifiedSince()), headers);
}
if (request.getMaxForwards() > -1) {
addHeader(HeaderConstants.HEADER_MAX_FORWARDS,
Integer.toString(request.getMaxForwards()), headers);
}
if (!request.getRanges().isEmpty()) {
addHeader(HeaderConstants.HEADER_RANGE,
RangeWriter.write(request.getRanges()), headers);
}
if (request.getReferrerRef() != null) {
addHeader(HeaderConstants.HEADER_REFERRER, request.getReferrerRef()
.toString(), headers);
}
if (request.getClientInfo().getAgent() != null) {
addHeader(HeaderConstants.HEADER_USER_AGENT, request
.getClientInfo().getAgent(), headers);
} else {
addHeader(HeaderConstants.HEADER_USER_AGENT, Engine.VERSION_HEADER,
headers);
}
if (clientInfo.getExpectations().size() > 0) {
addHeader(HeaderConstants.HEADER_ACCEPT_ENCODING,
PreferenceWriter.write(clientInfo.getAcceptedEncodings()),
headers);
}
// ----------------------------------
// 3) Add supported extension headers