001/* 002 * ==================================================================== 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 * ==================================================================== 020 * 021 * This software consists of voluntary contributions made by many 022 * individuals on behalf of the Apache Software Foundation. For more 023 * information on the Apache Software Foundation, please see 024 * <http://www.apache.org/>. 025 * 026 */ 027 028package org.apache.http.config; 029 030import org.apache.http.util.Args; 031 032/** 033 * HTTP Message constraints: line length and header count. 034 * <p> 035 * Please note that line length is defined in bytes and not characters. 036 * This is only relevant however when using non-standard HTTP charsets 037 * for protocol elements such as UTF-8. 038 * </p> 039 * 040 * @since 4.3 041 */ 042public class MessageConstraints implements Cloneable { 043 044 public static final MessageConstraints DEFAULT = new Builder().build(); 045 046 private final int maxLineLength; 047 private final int maxHeaderCount; 048 049 MessageConstraints(final int maxLineLength, final int maxHeaderCount) { 050 super(); 051 this.maxLineLength = maxLineLength; 052 this.maxHeaderCount = maxHeaderCount; 053 } 054 055 public int getMaxLineLength() { 056 return maxLineLength; 057 } 058 059 public int getMaxHeaderCount() { 060 return maxHeaderCount; 061 } 062 063 @Override 064 protected MessageConstraints clone() throws CloneNotSupportedException { 065 return (MessageConstraints) super.clone(); 066 } 067 068 @Override 069 public String toString() { 070 final StringBuilder builder = new StringBuilder(); 071 builder.append("[maxLineLength=").append(maxLineLength) 072 .append(", maxHeaderCount=").append(maxHeaderCount) 073 .append("]"); 074 return builder.toString(); 075 } 076 077 public static MessageConstraints lineLen(final int max) { 078 return new MessageConstraints(Args.notNegative(max, "Max line length"), -1); 079 } 080 081 public static MessageConstraints.Builder custom() { 082 return new Builder(); 083 } 084 085 public static MessageConstraints.Builder copy(final MessageConstraints config) { 086 Args.notNull(config, "Message constraints"); 087 return new Builder() 088 .setMaxHeaderCount(config.getMaxHeaderCount()) 089 .setMaxLineLength(config.getMaxLineLength()); 090 } 091 092 public static class Builder { 093 094 private int maxLineLength; 095 private int maxHeaderCount; 096 097 Builder() { 098 this.maxLineLength = -1; 099 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}