View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.impl.nio;
29  
30  import java.io.IOException;
31  import java.util.Iterator;
32  
33  import org.apache.hc.core5.http.FormattedHeader;
34  import org.apache.hc.core5.http.Header;
35  import org.apache.hc.core5.http.HttpException;
36  import org.apache.hc.core5.http.HttpMessage;
37  import org.apache.hc.core5.http.message.BasicLineFormatter;
38  import org.apache.hc.core5.http.message.LineFormatter;
39  import org.apache.hc.core5.http.nio.SessionOutputBuffer;
40  import org.apache.hc.core5.http.nio.NHttpMessageWriter;
41  import org.apache.hc.core5.util.Args;
42  import org.apache.hc.core5.util.CharArrayBuffer;
43  
44  /**
45   * Abstract {@link NHttpMessageWriter} that serves as a base for all message
46   * writer implementations.
47   *
48   * @since 4.0
49   */
50  public abstract class AbstractMessageWriter<T extends HttpMessage> implements NHttpMessageWriter<T> {
51  
52      private final CharArrayBuffer lineBuf;
53      private final LineFormatter lineFormatter;
54  
55      /**
56       * Creates an instance of AbstractMessageWriter.
57       *
58       * @param formatter the line formatter If {@code null} {@link BasicLineFormatter#INSTANCE}
59       *   will be used.
60       *
61       * @since 4.3
62       */
63      public AbstractMessageWriter(final LineFormatter formatter) {
64          super();
65          this.lineFormatter = (formatter != null) ? formatter : BasicLineFormatter.INSTANCE;
66          this.lineBuf = new CharArrayBuffer(64);
67      }
68  
69      LineFormatter getLineFormatter() {
70          return this.lineFormatter;
71      }
72  
73      @Override
74      public void reset() {
75      }
76  
77      /**
78       * Writes out the first line of {@link HttpMessage}.
79       *
80       * @param message HTTP message.
81       */
82      protected abstract void writeHeadLine(T message, CharArrayBuffer buffer) throws IOException;
83  
84      @Override
85      public void write(final T message, final SessionOutputBuffer sessionBuffer) throws IOException, HttpException {
86          Args.notNull(message, "HTTP message");
87          Args.notNull(sessionBuffer, "Session output buffer");
88  
89          writeHeadLine(message, this.lineBuf);
90          sessionBuffer.writeLine(this.lineBuf);
91          for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
92              final Header header = it.next();
93              if (header instanceof FormattedHeader) {
94                  final CharArrayBuffer buffer = ((FormattedHeader) header).getBuffer();
95                  sessionBuffer.writeLine(buffer);
96              } else {
97                  this.lineBuf.clear();
98                  this.lineFormatter.formatHeader(this.lineBuf, header);
99                  sessionBuffer.writeLine(this.lineBuf);
100             }
101         }
102         this.lineBuf.clear();
103         sessionBuffer.writeLine(this.lineBuf);
104     }
105 
106 }