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.io;
29  
30  import java.io.IOException;
31  import java.io.OutputStream;
32  import java.util.Iterator;
33  
34  import org.apache.hc.core5.http.FormattedHeader;
35  import org.apache.hc.core5.http.Header;
36  import org.apache.hc.core5.http.HttpException;
37  import org.apache.hc.core5.http.HttpMessage;
38  import org.apache.hc.core5.http.io.HttpMessageWriter;
39  import org.apache.hc.core5.http.io.SessionOutputBuffer;
40  import org.apache.hc.core5.http.message.BasicLineFormatter;
41  import org.apache.hc.core5.http.message.LineFormatter;
42  import org.apache.hc.core5.util.Args;
43  import org.apache.hc.core5.util.CharArrayBuffer;
44  
45  /**
46   * Abstract base class for HTTP message writers that serialize output to
47   * an instance of {@link org.apache.hc.core5.http.io.SessionOutputBuffer}.
48   *
49   * @since 4.0
50   */
51  public abstract class AbstractMessageWriter<T extends HttpMessage> implements HttpMessageWriter<T> {
52  
53      private final CharArrayBuffer lineBuf;
54      private final LineFormatter lineFormatter;
55  
56      /**
57       * Creates an instance of AbstractMessageWriter.
58       *
59       * @param formatter the line formatter If {@code null} {@link BasicLineFormatter#INSTANCE}
60       *   will be used.
61       *
62       * @since 4.3
63       */
64      public AbstractMessageWriter(final LineFormatter formatter) {
65          super();
66          this.lineFormatter = formatter != null ? formatter : BasicLineFormatter.INSTANCE;
67          this.lineBuf = new CharArrayBuffer(128);
68      }
69  
70      LineFormatter getLineFormatter() {
71          return this.lineFormatter;
72      }
73  
74      /**
75       * Subclasses must override this method to write out the first header line
76       * based on the {@link HttpMessage} passed as a parameter.
77       *
78       * @param message the message whose first line is to be written out.
79       * @param lineBuf line buffer
80       * @throws IOException in case of an I/O error.
81       */
82      protected abstract void writeHeadLine(T message, CharArrayBuffer lineBuf) throws IOException;
83  
84      @Override
85      public void write(final T message, final SessionOutputBuffer buffer, final OutputStream outputStream) throws IOException, HttpException {
86          Args.notNull(message, "HTTP message");
87          Args.notNull(buffer, "Session output buffer");
88          Args.notNull(outputStream, "Output stream");
89          writeHeadLine(message, this.lineBuf);
90          buffer.writeLine(this.lineBuf, outputStream);
91          for (final Iterator<Header> it = message.headerIterator(); it.hasNext(); ) {
92              final Header header = it.next();
93              if (header instanceof FormattedHeader) {
94                  final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer();
95                  buffer.writeLine(chbuffer, outputStream);
96              } else {
97                  this.lineBuf.clear();
98                  lineFormatter.formatHeader(this.lineBuf, header);
99                  buffer.writeLine(this.lineBuf, outputStream);
100             }
101         }
102         this.lineBuf.clear();
103         buffer.writeLine(this.lineBuf, outputStream);
104     }
105 
106 }