001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.http;
027:
028: import org.jicarilla.http.util.NioUtil;
029:
030: import java.io.IOException;
031: import java.nio.ByteBuffer;
032: import java.nio.channels.SocketChannel;
033: import java.nio.channels.WritableByteChannel;
034: import java.util.Iterator;
035:
036: /**
037: * Rather small component that knows how to write HTTPMessage
038: * instances back to a channel.
039: *
040: * This is a IoC type-3 compatible component.
041: *
042: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
043: * @version $Id: HTTPMessageWriterImpl.java,v 1.4 2004/02/26 16:51:55 lsimons Exp $
044: */
045: public class HTTPMessageWriterImpl implements HTTPMessageWriter {
046: // ----------------------------------------------------------------------
047: // Properties
048: // ----------------------------------------------------------------------
049: public final static ByteBuffer SP = NioUtil.toByteBuffer(" ");
050: public final static ByteBuffer CRLF = NioUtil.toByteBuffer("\r\n");
051: public final static ByteBuffer COLON_SP = NioUtil
052: .toByteBuffer(": ");
053: static {
054: SP.rewind();
055: CRLF.rewind();
056: COLON_SP.rewind();
057: }
058:
059: // ----------------------------------------------------------------------
060: // Work Interface: HTTPMessageWriter
061: // ----------------------------------------------------------------------
062:
063: public void write(final HTTPMessage m, final WritableByteChannel c)
064: throws HTTPException, IOException {
065: write(m, c, false);
066: }
067:
068: public void write(final HTTPMessage m, final WritableByteChannel c,
069: final boolean close) throws HTTPException, IOException {
070: writeStartLine(c, m);
071: writeHeaders(m, c);
072: writeBody(m, c);
073: closeChannel(close, c);
074: }
075:
076: // ----------------------------------------------------------------------
077: // Helper methods
078: // ----------------------------------------------------------------------
079:
080: protected static void writeStartLine(final WritableByteChannel c,
081: final HTTPMessage m) throws IOException {
082: c.write(m.getField1());
083: SP.rewind();
084: c.write(SP);
085: c.write(m.getField2());
086: SP.rewind();
087: c.write(SP);
088: c.write(m.getField3());
089: CRLF.rewind();
090: c.write(CRLF);
091: }
092:
093: protected static void writeHeaders(final HTTPMessage m,
094: final WritableByteChannel c) throws IOException {
095: final Iterator it = m.getHeaders().iterator();
096: while (it.hasNext()) {
097: final HTTPField field = (HTTPField) it.next();
098:
099: c.write(field.getName());
100: COLON_SP.rewind();
101: c.write(COLON_SP);
102: c.write(field.getValue());
103: CRLF.rewind();
104: c.write(CRLF);
105: }
106: CRLF.rewind();
107: c.write(CRLF);
108: }
109:
110: protected static void writeBody(final HTTPMessage m,
111: final WritableByteChannel c) throws IOException {
112: final ByteBuffer[] bp = m.getBodyParts();
113: for (int i = 0; i < bp.length; i++) {
114: bp[i].rewind();
115: c.write(bp[i]);
116: }
117: }
118:
119: protected static void closeChannel(final boolean close,
120: final WritableByteChannel c) {
121: if (close) {
122: try {
123: if (c instanceof SocketChannel)
124: ((SocketChannel) c).socket().close();
125: else
126: c.close();
127: } catch (IOException ioe) {
128: }
129: }
130: }
131: }
|