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.message;
29  
30  import org.apache.hc.core5.annotation.Contract;
31  import org.apache.hc.core5.annotation.ThreadingBehavior;
32  import org.apache.hc.core5.http.Header;
33  import org.apache.hc.core5.http.ProtocolVersion;
34  import org.apache.hc.core5.util.Args;
35  import org.apache.hc.core5.util.CharArrayBuffer;
36  
37  /**
38   * Default {@link org.apache.hc.core5.http.message.LineFormatter} implementation.
39   *
40   * @since 4.0
41   */
42  @Contract(threading = ThreadingBehavior.IMMUTABLE)
43  public class BasicLineFormatter implements LineFormatter {
44  
45      public final static BasicLineFormattericLineFormatter.html#BasicLineFormatter">BasicLineFormatter INSTANCE = new BasicLineFormatter();
46  
47      public BasicLineFormatter() {
48          super();
49      }
50  
51      void formatProtocolVersion(final CharArrayBuffer buffer, final ProtocolVersion version) {
52          buffer.append(version.format());
53      }
54  
55      @Override
56      public void formatRequestLine(final CharArrayBuffer buffer, final RequestLine reqline) {
57          Args.notNull(buffer, "Char array buffer");
58          Args.notNull(reqline, "Request line");
59          buffer.append(reqline.getMethod());
60          buffer.append(' ');
61          buffer.append(reqline.getUri());
62          buffer.append(' ');
63          formatProtocolVersion(buffer, reqline.getProtocolVersion());
64      }
65  
66      @Override
67      public void formatStatusLine(final CharArrayBuffer buffer, final StatusLine statusLine) {
68          Args.notNull(buffer, "Char array buffer");
69          Args.notNull(statusLine, "Status line");
70  
71          formatProtocolVersion(buffer, statusLine.getProtocolVersion());
72          buffer.append(' ');
73          buffer.append(Integer.toString(statusLine.getStatusCode()));
74          buffer.append(' '); // keep whitespace even if reason phrase is empty
75          final String reasonPhrase = statusLine.getReasonPhrase();
76          if (reasonPhrase != null) {
77              buffer.append(reasonPhrase);
78          }
79      }
80  
81      @Override
82      public void formatHeader(final CharArrayBuffer buffer, final Header header) {
83          Args.notNull(buffer, "Char array buffer");
84          Args.notNull(header, "Header");
85  
86          buffer.append(header.getName());
87          buffer.append(": ");
88          final String value = header.getValue();
89          if (value != null) {
90              buffer.ensureCapacity(buffer.length() + value.length());
91              for (int valueIndex = 0; valueIndex < value.length(); valueIndex++) {
92                  char valueChar = value.charAt(valueIndex);
93                  if (valueChar == '\r'
94                          || valueChar == '\n'
95                          || valueChar == '\f'
96                          || valueChar == 0x0b) {
97                      valueChar = ' ';
98                  }
99                  buffer.append(valueChar);
100             }
101         }
102     }
103 
104 }