{
@Override
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg)
throws Exception
{
RestRequest request = (RestRequest) msg;
HttpMethod nettyMethod = HttpMethod.valueOf(request.getMethod());
URL url = new URL(request.getURI().toString());
String path = url.getFile();
// RFC 2616, section 5.1.2:
// Note that the absolute path cannot be empty; if none is present in the original URI,
// it MUST be given as "/" (the server root).
if (path.isEmpty())
{
path = "/";
}
HttpRequest nettyRequest =
new DefaultHttpRequest(HttpVersion.HTTP_1_1, nettyMethod, path);
nettyRequest.setHeader(HttpHeaders.Names.HOST, url.getAuthority());
for (Map.Entry<String, String> e : request.getHeaders().entrySet())
{
nettyRequest.setHeader(e.getKey(), e.getValue());
}
final ByteString entity = request.getEntity();
ChannelBuffer buf = ChannelBuffers.wrappedBuffer(entity.asByteBuffer());
nettyRequest.setContent(buf);
nettyRequest.setHeader(HttpHeaders.Names.CONTENT_LENGTH, entity.length());
return nettyRequest;