[6785] | 1 | /**************************************************************** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
| 3 | * or more contributor license agreements. See the NOTICE file * |
---|
| 4 | * distributed with this work for additional information * |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file * |
---|
| 6 | * to you under the Apache License, Version 2.0 (the * |
---|
| 7 | * "License"); you may not use this file except in compliance * |
---|
| 8 | * with the License. You may obtain a copy of the License at * |
---|
| 9 | * * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
| 11 | * * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, * |
---|
| 13 | * software distributed under the License is distributed on an * |
---|
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
| 15 | * KIND, either express or implied. See the License for the * |
---|
| 16 | * specific language governing permissions and limitations * |
---|
| 17 | * under the License. * |
---|
| 18 | ****************************************************************/ |
---|
| 19 | |
---|
| 20 | package org.apache.james.mime4j.stream; |
---|
| 21 | |
---|
| 22 | import org.apache.james.mime4j.MimeException; |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * MIME entity configuration |
---|
| 26 | */ |
---|
| 27 | public final class MimeEntityConfig implements Cloneable { |
---|
| 28 | |
---|
| 29 | private boolean strictParsing; |
---|
| 30 | private int maxLineLen; |
---|
| 31 | private int maxHeaderCount; |
---|
| 32 | private int maxHeaderLen; |
---|
| 33 | private long maxContentLen; |
---|
| 34 | private boolean countLineNumbers; |
---|
| 35 | private String headlessParsing; |
---|
| 36 | private boolean malformedHeaderStartsBody; |
---|
| 37 | |
---|
| 38 | public MimeEntityConfig() { |
---|
| 39 | this.strictParsing = false; |
---|
| 40 | this.countLineNumbers = false; |
---|
| 41 | this.malformedHeaderStartsBody = false; |
---|
| 42 | this.maxLineLen = 1000; |
---|
| 43 | this.maxHeaderCount = 1000; |
---|
| 44 | this.maxHeaderLen = 10000; |
---|
| 45 | this.maxContentLen = -1; |
---|
| 46 | this.headlessParsing = null; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * @see #setMalformedHeaderStartsBody(boolean) |
---|
| 51 | * |
---|
| 52 | * @return true if malformed header should "end" the headers and be |
---|
| 53 | * part of the body |
---|
| 54 | */ |
---|
| 55 | public boolean isMalformedHeaderStartsBody() { |
---|
| 56 | return malformedHeaderStartsBody; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Define the behaviour for dealing with malformed headers while in lenient |
---|
| 61 | * mode |
---|
| 62 | * |
---|
| 63 | * @param malformedHeaderStartsBody <code>true</code> to make the parser |
---|
| 64 | * interpret a malformed header as end of the headers and |
---|
| 65 | * as part of the body (as if the CRLF separator was missing). |
---|
| 66 | * <code>false</code> to simply ignore malformed headers and |
---|
| 67 | * continue parsing headers from the following line. |
---|
| 68 | */ |
---|
| 69 | public void setMalformedHeaderStartsBody(boolean malformedHeaderStartsBody) { |
---|
| 70 | this.malformedHeaderStartsBody = malformedHeaderStartsBody; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Returns the value of the strict parsing mode |
---|
| 75 | * @see #setStrictParsing(boolean) |
---|
| 76 | * |
---|
| 77 | * @return value of the strict parsing mode |
---|
| 78 | */ |
---|
| 79 | public boolean isStrictParsing() { |
---|
| 80 | return this.strictParsing; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Defines whether minor violations of the MIME specification should be |
---|
| 85 | * tolerated or should result in a {@link MimeException}. If this parameter |
---|
| 86 | * is set to <code>true</code>, a strict interpretation of the MIME |
---|
| 87 | * specification will be enforced, If this parameter is set to <code>false</code> |
---|
| 88 | * minor violations will result in a warning in the log. |
---|
| 89 | * <p> |
---|
| 90 | * Default value: <code>false</code> |
---|
| 91 | * |
---|
| 92 | * @param strictParsing value of the strict parsing mode |
---|
| 93 | */ |
---|
| 94 | public void setStrictParsing(boolean strictParsing) { |
---|
| 95 | this.strictParsing = strictParsing; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * Returns the maximum line length limit |
---|
| 100 | * @see #setMaxLineLen(int) |
---|
| 101 | * |
---|
| 102 | * @return value of the the maximum line length limit |
---|
| 103 | */ |
---|
| 104 | public int getMaxLineLen() { |
---|
| 105 | return this.maxLineLen; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | /** |
---|
| 109 | * Sets the maximum line length limit. Parsing of a MIME entity will be terminated |
---|
| 110 | * with a {@link MimeException} if a line is encountered that exceeds the maximum |
---|
| 111 | * length limit. If this parameter is set to a non positive value the line length |
---|
| 112 | * check will be disabled. |
---|
| 113 | * <p> |
---|
| 114 | * Default value: <code>1000</code> |
---|
| 115 | * |
---|
| 116 | * @param maxLineLen maximum line length limit |
---|
| 117 | */ |
---|
| 118 | public void setMaxLineLen(int maxLineLen) { |
---|
| 119 | this.maxLineLen = maxLineLen; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * Returns the maximum header limit |
---|
| 124 | * @see #setMaxHeaderCount(int) |
---|
| 125 | * |
---|
| 126 | * @return value of the the maximum header limit |
---|
| 127 | */ |
---|
| 128 | public int getMaxHeaderCount() { |
---|
| 129 | return this.maxHeaderCount; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * Sets the maximum header limit. Parsing of a MIME entity will be terminated |
---|
| 134 | * with a {@link MimeException} if the number of headers exceeds the maximum |
---|
| 135 | * limit. If this parameter is set to a non positive value the header limit check |
---|
| 136 | * will be disabled. |
---|
| 137 | * <p> |
---|
| 138 | * Default value: <code>1000</code> |
---|
| 139 | * |
---|
| 140 | * @param maxHeaderCount maximum header limit |
---|
| 141 | */ |
---|
| 142 | public void setMaxHeaderCount(int maxHeaderCount) { |
---|
| 143 | this.maxHeaderCount = maxHeaderCount; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * Returns the maximum header length limit |
---|
| 148 | * @see #setMaxHeaderLen(int) |
---|
| 149 | * |
---|
| 150 | * @return value of the maximum header length limit |
---|
| 151 | */ |
---|
| 152 | public int getMaxHeaderLen() { |
---|
| 153 | return maxHeaderLen; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * Sets the maximum header length limit. Parsing of a MIME entity will be terminated |
---|
| 158 | * with a {@link MimeException} if the total length of a header exceeds this limit. |
---|
| 159 | * If this parameter is set to a non positive value the header length check will be |
---|
| 160 | * disabled. |
---|
| 161 | * <p> |
---|
| 162 | * A message header may be folded across multiple lines. This configuration parameter |
---|
| 163 | * is used to limit the total length of a header, i.e. the sum of the length of all |
---|
| 164 | * lines the header spans across (including line terminators). |
---|
| 165 | * <p> |
---|
| 166 | * Default value: <code>10000</code> |
---|
| 167 | * |
---|
| 168 | * @param maxHeaderLen maximum header length limit |
---|
| 169 | */ |
---|
| 170 | public void setMaxHeaderLen(int maxHeaderLen) { |
---|
| 171 | this.maxHeaderLen = maxHeaderLen; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * Returns the maximum content length limit |
---|
| 176 | * @see #setMaxContentLen(long) |
---|
| 177 | * |
---|
| 178 | * @return value of the the maximum content length limit |
---|
| 179 | */ |
---|
| 180 | public long getMaxContentLen() { |
---|
| 181 | return maxContentLen; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /** |
---|
| 185 | * Sets the maximum content length limit. Parsing of a MIME entity will be terminated |
---|
| 186 | * with a {@link MimeException} if a content body exceeds the maximum length limit. |
---|
| 187 | * If this parameter is set to a non positive value the content length |
---|
| 188 | * check will be disabled. |
---|
| 189 | * <p> |
---|
| 190 | * Default value: <code>-1</code> |
---|
| 191 | * |
---|
| 192 | * @param maxContentLen maximum content length limit |
---|
| 193 | */ |
---|
| 194 | public void setMaxContentLen(long maxContentLen) { |
---|
| 195 | this.maxContentLen = maxContentLen; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * Returns the value of the line number counting mode. |
---|
| 200 | * |
---|
| 201 | * @return value of the line number counting mode. |
---|
| 202 | */ |
---|
| 203 | public boolean isCountLineNumbers() { |
---|
| 204 | return countLineNumbers; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | * Defines whether the parser should count line numbers. If enabled line |
---|
| 209 | * numbers are included in the debug output. |
---|
| 210 | * <p> |
---|
| 211 | * Default value: <code>false</code> |
---|
| 212 | * |
---|
| 213 | * @param countLineNumbers |
---|
| 214 | * value of the line number counting mode. |
---|
| 215 | */ |
---|
| 216 | public void setCountLineNumbers(boolean countLineNumbers) { |
---|
| 217 | this.countLineNumbers = countLineNumbers; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | /** |
---|
| 221 | * Returns the value of the default content type. |
---|
| 222 | * When not null, indicates that the parsing should be headless. |
---|
| 223 | * |
---|
| 224 | * @return default content type when parsing headless, |
---|
| 225 | * null otherwise |
---|
| 226 | * @see org.apache.james.mime4j.parser.MimeStreamParser#parse(java.io.InputStream) |
---|
| 227 | */ |
---|
| 228 | public String getHeadlessParsing() { |
---|
| 229 | return headlessParsing; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * Defines a default content type. |
---|
| 234 | * When not null, indicates that the parsing should be headless. |
---|
| 235 | * <p> |
---|
| 236 | * Default value: <code>null</code> |
---|
| 237 | * |
---|
| 238 | * @param contentType |
---|
| 239 | * value of the default content type when parsing headless, |
---|
| 240 | * null otherwise |
---|
| 241 | * @see org.apache.james.mime4j.parser.MimeStreamParser#parse(java.io.InputStream) |
---|
| 242 | */ |
---|
| 243 | public void setHeadlessParsing(String contentType) { |
---|
| 244 | this.headlessParsing = contentType; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | @Override |
---|
| 248 | public MimeEntityConfig clone() { |
---|
| 249 | try { |
---|
| 250 | return (MimeEntityConfig) super.clone(); |
---|
| 251 | } catch (CloneNotSupportedException e) { |
---|
| 252 | // this shouldn't happen, since we are Cloneable |
---|
| 253 | throw new InternalError(); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | @Override |
---|
| 258 | public String toString() { |
---|
| 259 | return "[strict parsing: " + strictParsing + ", max line length: " |
---|
| 260 | + maxLineLen + ", max header count: " + maxHeaderCount |
---|
| 261 | + ", max content length: " + maxContentLen |
---|
| 262 | + ", count line numbers: " + countLineNumbers + "]"; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | } |
---|