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.http.config;
29  
30  import org.apache.http.util.Args;
31  
32  /**
33   * HTTP Message constraints: line length and header count.
34   * <p/>
35   * Please note that line length is defined in bytes and not characters.
36   * This is only relevant however when using non-standard HTTP charsets
37   * for protocol elements such as UTF-8.
38   *
39   * @since 4.3
40   */
41  public class MessageConstraints implements Cloneable {
42  
43      public static final MessageConstraints DEFAULT = new Builder().build();
44  
45      private final int maxLineLength;
46      private final int maxHeaderCount;
47  
48      MessageConstraints(final int maxLineLength, final int maxHeaderCount) {
49          super();
50          this.maxLineLength = maxLineLength;
51          this.maxHeaderCount = maxHeaderCount;
52      }
53  
54      public int getMaxLineLength() {
55          return maxLineLength;
56      }
57  
58      public int getMaxHeaderCount() {
59          return maxHeaderCount;
60      }
61  
62      @Override
63      protected MessageConstraints clone() throws CloneNotSupportedException {
64          return (MessageConstraints) super.clone();
65      }
66  
67      @Override
68      public String toString() {
69          final StringBuilder builder = new StringBuilder();
70          builder.append("[maxLineLength=").append(maxLineLength)
71                  .append(", maxHeaderCount=").append(maxHeaderCount)
72                  .append("]");
73          return builder.toString();
74      }
75  
76      public static MessageConstraints lineLen(final int max) {
77          return new MessageConstraints(Args.notNegative(max, "Max line length"), -1);
78      }
79  
80      public static MessageConstraints.Builder custom() {
81          return new Builder();
82      }
83  
84      public static MessageConstraints.Builder copy(final MessageConstraints config) {
85          Args.notNull(config, "Message constraints");
86          return new Builder()
87              .setMaxHeaderCount(config.getMaxHeaderCount())
88              .setMaxLineLength(config.getMaxLineLength());
89      }
90  
91      public static class Builder {
92  
93          private int maxLineLength;
94          private int maxHeaderCount;
95  
96          Builder() {
97              this.maxLineLength = -1;
98              this.maxHeaderCount = -1;
99          }
100 
101         public Builder setMaxLineLength(final int maxLineLength) {
102             this.maxLineLength = maxLineLength;
103             return this;
104         }
105 
106         public Builder setMaxHeaderCount(final int maxHeaderCount) {
107             this.maxHeaderCount = maxHeaderCount;
108             return this;
109         }
110 
111         public MessageConstraints build() {
112             return new MessageConstraints(maxLineLength, maxHeaderCount);
113         }
114 
115     }
116 
117 }