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   *
35   * @since 4.3
36   */
37  public class MessageConstraints implements Cloneable {
38  
39      public static final MessageConstraints DEFAULT = new Builder().build();
40  
41      private final int maxLineLength;
42      private final int maxHeaderCount;
43  
44      MessageConstraints(final int maxLineLength, final int maxHeaderCount) {
45          super();
46          this.maxLineLength = maxLineLength;
47          this.maxHeaderCount = maxHeaderCount;
48      }
49  
50      public int getMaxLineLength() {
51          return maxLineLength;
52      }
53  
54      public int getMaxHeaderCount() {
55          return maxHeaderCount;
56      }
57  
58      @Override
59      protected MessageConstraints clone() throws CloneNotSupportedException {
60          return (MessageConstraints) super.clone();
61      }
62  
63      @Override
64      public String toString() {
65          final StringBuilder builder = new StringBuilder();
66          builder.append("[maxLineLength=").append(maxLineLength)
67                  .append(", maxHeaderCount=").append(maxHeaderCount)
68                  .append("]");
69          return builder.toString();
70      }
71  
72      public static MessageConstraints lineLen(final int max) {
73          return new MessageConstraints(Args.notNegative(max, "Max line length"), -1);
74      }
75  
76      public static MessageConstraints.Builder custom() {
77          return new Builder();
78      }
79  
80      public static MessageConstraints.Builder copy(final MessageConstraints config) {
81          Args.notNull(config, "Message constraints");
82          return new Builder()
83              .setMaxHeaderCount(config.getMaxHeaderCount())
84              .setMaxLineLength(config.getMaxLineLength());
85      }
86  
87      public static class Builder {
88  
89          private int maxLineLength;
90          private int maxHeaderCount;
91  
92          Builder() {
93              this.maxLineLength = -1;
94              this.maxHeaderCount = -1;
95          }
96  
97          public Builder setMaxLineLength(final int maxLineLength) {
98              this.maxLineLength = maxLineLength;
99              return this;
100         }
101 
102         public Builder setMaxHeaderCount(final int maxHeaderCount) {
103             this.maxHeaderCount = maxHeaderCount;
104             return this;
105         }
106 
107         public MessageConstraints build() {
108             return new MessageConstraints(maxLineLength, maxHeaderCount);
109         }
110 
111     }
112 
113 }