2008-11-19 08:22:15 +01:00
|
|
|
/*
|
|
|
|
* JBoss, Home of Professional Open Source
|
2009-06-19 19:48:17 +02:00
|
|
|
*
|
|
|
|
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
|
|
|
|
* by the @author tags. See the COPYRIGHT.txt in the distribution for a
|
2008-11-19 08:22:15 +01:00
|
|
|
* full listing of individual contributors.
|
|
|
|
*
|
|
|
|
* This is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2.1 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this software; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
|
|
|
|
*/
|
|
|
|
package org.jboss.netty.handler.codec.http;
|
|
|
|
|
2009-02-12 07:37:22 +01:00
|
|
|
import static org.jboss.netty.buffer.ChannelBuffers.*;
|
2008-11-19 08:22:15 +01:00
|
|
|
import static org.jboss.netty.handler.codec.http.HttpCodecUtil.*;
|
|
|
|
|
2009-06-15 09:50:53 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2008-11-19 08:22:15 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.jboss.netty.buffer.ChannelBuffer;
|
|
|
|
import org.jboss.netty.buffer.ChannelBuffers;
|
2008-12-17 08:38:32 +01:00
|
|
|
import org.jboss.netty.channel.Channel;
|
2008-11-19 08:22:15 +01:00
|
|
|
import org.jboss.netty.channel.ChannelHandlerContext;
|
|
|
|
import org.jboss.netty.channel.ChannelPipelineCoverage;
|
2008-12-17 08:38:32 +01:00
|
|
|
import org.jboss.netty.handler.codec.oneone.OneToOneEncoder;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2009-06-19 17:35:19 +02:00
|
|
|
* Encodes an {@link HttpMessage} or an {@link HttpChunk} into
|
|
|
|
* a {@link ChannelBuffer}.
|
|
|
|
*
|
|
|
|
* <h3>Extensibility</h3>
|
|
|
|
*
|
|
|
|
* Please note that this encoder is designed to be extended to implement
|
|
|
|
* a protocol derived from HTTP, such as
|
|
|
|
* <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
|
|
|
|
* <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
|
|
|
|
* To implement the encoder of such a derived protocol, extend this class and
|
|
|
|
* implement all abstract methods properly.
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
|
|
|
* @author The Netty Project (netty-dev@lists.jboss.org)
|
|
|
|
* @author Andy Taylor (andy.taylor@jboss.org)
|
|
|
|
* @author Trustin Lee (tlee@redhat.com)
|
2008-12-03 10:00:29 +01:00
|
|
|
* @version $Rev$, $Date$
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2009-05-08 10:22:01 +02:00
|
|
|
@ChannelPipelineCoverage("all")
|
2008-12-17 08:38:32 +01:00
|
|
|
public abstract class HttpMessageEncoder extends OneToOneEncoder {
|
|
|
|
|
2009-02-12 08:03:35 +01:00
|
|
|
private static final ChannelBuffer LAST_CHUNK = copiedBuffer("0\r\n\r\n", "ASCII");
|
2009-02-12 08:01:26 +01:00
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a new instance.
|
|
|
|
*/
|
|
|
|
protected HttpMessageEncoder() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
@Override
|
2008-12-17 08:38:32 +01:00
|
|
|
protected Object encode(ChannelHandlerContext ctx, Channel channel, Object msg) throws Exception {
|
2009-02-12 07:37:22 +01:00
|
|
|
if (msg instanceof HttpMessage) {
|
|
|
|
HttpMessage request = (HttpMessage) msg;
|
|
|
|
ChannelBuffer header = ChannelBuffers.dynamicBuffer(
|
|
|
|
channel.getConfig().getBufferFactory());
|
|
|
|
encodeInitialLine(header, request);
|
|
|
|
encodeHeaders(header, request);
|
|
|
|
header.writeBytes(CRLF);
|
|
|
|
|
|
|
|
ChannelBuffer content = request.getContent();
|
2009-04-02 11:10:57 +02:00
|
|
|
if (!content.readable()) {
|
2009-02-12 07:37:22 +01:00
|
|
|
return header; // no content
|
|
|
|
} else {
|
|
|
|
return wrappedBuffer(header, content);
|
|
|
|
}
|
2009-03-04 14:45:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msg instanceof HttpChunk) {
|
2009-02-12 07:37:22 +01:00
|
|
|
HttpChunk chunk = (HttpChunk) msg;
|
2009-02-12 08:01:26 +01:00
|
|
|
if (chunk.isLast()) {
|
|
|
|
return LAST_CHUNK.duplicate();
|
|
|
|
} else {
|
|
|
|
ChannelBuffer content = chunk.getContent();
|
|
|
|
int contentLength = content.readableBytes();
|
2009-02-12 07:37:22 +01:00
|
|
|
|
2009-02-12 08:01:26 +01:00
|
|
|
return wrappedBuffer(
|
2009-02-12 08:03:35 +01:00
|
|
|
copiedBuffer(Integer.toHexString(contentLength), "ASCII"),
|
2009-02-12 08:01:26 +01:00
|
|
|
wrappedBuffer(CRLF),
|
|
|
|
content.slice(content.readerIndex(), contentLength),
|
|
|
|
wrappedBuffer(CRLF));
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-02-12 07:37:22 +01:00
|
|
|
|
|
|
|
// Unknown message type.
|
|
|
|
return msg;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 17:35:19 +02:00
|
|
|
private void encodeHeaders(ChannelBuffer buf, HttpMessage message) {
|
2008-11-19 08:22:15 +01:00
|
|
|
Set<String> headers = message.getHeaderNames();
|
2009-06-15 09:50:53 +02:00
|
|
|
try {
|
|
|
|
for (String header : headers) {
|
|
|
|
List<String> values = message.getHeaders(header);
|
|
|
|
for (String value : values) {
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-06-15 09:50:53 +02:00
|
|
|
buf.writeBytes(header.getBytes("ASCII"));
|
|
|
|
buf.writeByte(COLON);
|
|
|
|
buf.writeByte(SP);
|
|
|
|
buf.writeBytes(value.getBytes("ASCII"));
|
|
|
|
buf.writeBytes(CRLF);
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2009-06-15 09:50:53 +02:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw (Error) new Error().initCause(e);
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void encodeInitialLine(ChannelBuffer buf, HttpMessage message) throws Exception;
|
|
|
|
}
|