001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.impl.nio.codecs;
029
030import java.io.IOException;
031import java.util.Iterator;
032
033import org.apache.http.Header;
034import org.apache.http.HttpException;
035import org.apache.http.HttpMessage;
036import org.apache.http.message.BasicLineFormatter;
037import org.apache.http.message.LineFormatter;
038import org.apache.http.nio.NHttpMessageWriter;
039import org.apache.http.nio.reactor.SessionOutputBuffer;
040import org.apache.http.params.HttpParams;
041import org.apache.http.util.Args;
042import org.apache.http.util.CharArrayBuffer;
043
044/**
045 * Abstract {@link NHttpMessageWriter} that serves as a base for all message
046 * writer implementations.
047 *
048 * @since 4.0
049 */
050@SuppressWarnings("deprecation")
051public abstract class AbstractMessageWriter<T extends HttpMessage> implements NHttpMessageWriter<T> {
052
053    protected final SessionOutputBuffer sessionBuffer;
054    protected final CharArrayBuffer lineBuf;
055    protected final LineFormatter lineFormatter;
056
057    /**
058     * Creates an instance of this class.
059     *
060     * @param buffer the session output buffer.
061     * @param formatter the line formatter.
062     * @param params HTTP parameters.
063     *
064     * @deprecated (4.3) use
065     *   {@link AbstractMessageWriter#AbstractMessageWriter(SessionOutputBuffer, LineFormatter)}
066     */
067    @Deprecated
068    public AbstractMessageWriter(final SessionOutputBuffer buffer,
069                                 final LineFormatter formatter,
070                                 final HttpParams params) {
071        super();
072        Args.notNull(buffer, "Session input buffer");
073        this.sessionBuffer = buffer;
074        this.lineBuf = new CharArrayBuffer(64);
075        this.lineFormatter = (formatter != null) ? formatter : BasicLineFormatter.INSTANCE;
076    }
077
078    /**
079     * Creates an instance of AbstractMessageWriter.
080     *
081     * @param buffer the session output buffer.
082     * @param formatter the line formatter If {@code null} {@link BasicLineFormatter#INSTANCE}
083     *   will be used.
084     *
085     * @since 4.3
086     */
087    public AbstractMessageWriter(
088            final SessionOutputBuffer buffer,
089            final LineFormatter formatter) {
090        super();
091        this.sessionBuffer = Args.notNull(buffer, "Session input buffer");
092        this.lineFormatter = (formatter != null) ? formatter : BasicLineFormatter.INSTANCE;
093        this.lineBuf = new CharArrayBuffer(64);
094    }
095
096    @Override
097    public void reset() {
098    }
099
100    /**
101     * Writes out the first line of {@link HttpMessage}.
102     *
103     * @param message HTTP message.
104     */
105    protected abstract void writeHeadLine(T message) throws IOException;
106
107    @Override
108    public void write(final T message) throws IOException, HttpException {
109        Args.notNull(message, "HTTP message");
110        writeHeadLine(message);
111        for (final Iterator<?> it = message.headerIterator(); it.hasNext(); ) {
112            final Header header = (Header) it.next();
113            this.sessionBuffer.writeLine
114                (lineFormatter.formatHeader(this.lineBuf, header));
115        }
116        this.lineBuf.clear();
117        this.sessionBuffer.writeLine(this.lineBuf);
118    }
119
120}