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