String body = exchange.getIn().getBody(String.class);
Form form = new Form();
// add the body as the key in the form with null value
form.add(body, null);
MediaType mediaType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, MediaType.class);
if (mediaType == null) {
mediaType = MediaType.APPLICATION_WWW_FORM;
}
LOG.debug("Populate Restlet request from exchange body: {} using media type {}", body, mediaType);
// login and password are filtered by header filter strategy
String login = exchange.getIn().getHeader(RestletConstants.RESTLET_LOGIN, String.class);
String password = exchange.getIn().getHeader(RestletConstants.RESTLET_PASSWORD, String.class);
if (login != null && password != null) {
ChallengeResponse authentication = new ChallengeResponse(ChallengeScheme.HTTP_BASIC, login, password);
request.setChallengeResponse(authentication);
LOG.debug("Basic HTTP Authentication has been applied");
}
for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (!headerFilterStrategy.applyFilterToCamelHeaders(key, value, exchange)) {
// Use forms only for GET and POST/x-www-form-urlencoded
if (request.getMethod() == Method.GET || (request.getMethod() == Method.POST && mediaType == MediaType.APPLICATION_WWW_FORM)) {
if (key.startsWith("org.restlet.")) {
// put the org.restlet headers in attributes
request.getAttributes().put(key, value);
} else {
// put the user stuff in the form
form.add(key, value.toString());
}
} else {
// For non-form post put all the headers in attributes
request.getAttributes().put(key, value);
}
LOG.debug("Populate Restlet request from exchange header: {} value: {}", key, value);
}
}
LOG.debug("Using Content Type: {} for POST data: {}", mediaType, body);
// Only URL Encode for GET and form POST
if (request.getMethod() == Method.GET || (request.getMethod() == Method.POST && mediaType == MediaType.APPLICATION_WWW_FORM)) {
request.setEntity(form.getWebRepresentation());
} else {
if (body == null) {
request.setEntity(null);
} else {
request.setEntity(body, mediaType);
}
}
MediaType acceptedMediaType = exchange.getIn().getHeader(Exchange.ACCEPT_CONTENT_TYPE, MediaType.class);
if (acceptedMediaType != null) {
request.getClientInfo().getAcceptedMediaTypes().add(new Preference<MediaType>(acceptedMediaType));
}
}