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 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.getProtocol());
53          buffer.append('/');
54          buffer.append(Integer.toString(version.getMajor()));
55          buffer.append('.');
56          buffer.append(Integer.toString(version.getMinor()));
57      }
58  
59      @Override
60      public void formatRequestLine(final CharArrayBuffer buffer, final RequestLine reqline) {
61          Args.notNull(buffer, "Char array buffer");
62          Args.notNull(reqline, "Request line");
63          buffer.append(reqline.getMethod());
64          buffer.append(' ');
65          buffer.append(reqline.getUri());
66          buffer.append(' ');
67          formatProtocolVersion(buffer, reqline.getProtocolVersion());
68      }
69  
70      @Override
71      public void formatStatusLine(final CharArrayBuffer buffer, final StatusLine statline) {
72          Args.notNull(buffer, "Char array buffer");
73          Args.notNull(statline, "Status line");
74  
75          formatProtocolVersion(buffer, statline.getProtocolVersion());
76          buffer.append(' ');
77          buffer.append(Integer.toString(statline.getStatusCode()));
78          buffer.append(' '); // keep whitespace even if reason phrase is empty
79          final String reasonPhrase = statline.getReasonPhrase();
80          if (reasonPhrase != null) {
81              buffer.append(reasonPhrase);
82          }
83      }
84  
85      @Override
86      public void formatHeader(final CharArrayBuffer buffer, final Header header) {
87          Args.notNull(buffer, "Char array buffer");
88          Args.notNull(header, "Header");
89  
90          buffer.append(header.getName());
91          buffer.append(": ");
92          final String value = header.getValue();
93          if (value != null) {
94              buffer.ensureCapacity(buffer.length() + value.length());
95              for (int valueIndex = 0; valueIndex < value.length(); valueIndex++) {
96                  char valueChar = value.charAt(valueIndex);
97                  if (valueChar == '\r'
98                          || valueChar == '\n'
99                          || valueChar == '\f'
100                         || valueChar == 0x0b) {
101                     valueChar = ' ';
102                 }
103                 buffer.append(valueChar);
104             }
105         }
106     }
107 
108 }