001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.mime4j.parser;
021    
022    import org.apache.james.mime4j.MimeException;
023    
024    /**
025     * MIME entity configuration
026     */
027    public final class MimeEntityConfig implements Cloneable {
028    
029        private boolean maximalBodyDescriptor;
030        private boolean strictParsing;
031        private int maxLineLen;
032        private int maxHeaderCount;
033        private long maxContentLen;
034        private boolean countLineNumbers;
035        
036        public MimeEntityConfig() {
037            this.maximalBodyDescriptor = false;
038            this.strictParsing = false;
039            this.maxLineLen = 1000;
040            this.maxHeaderCount = 1000;
041            this.maxContentLen = -1;
042            this.countLineNumbers = false;
043        }
044        
045        public boolean isMaximalBodyDescriptor() {
046            return this.maximalBodyDescriptor;
047        }
048        
049        public void setMaximalBodyDescriptor(boolean maximalBodyDescriptor) {
050            this.maximalBodyDescriptor = maximalBodyDescriptor;
051        }
052        
053        /**
054         * Defines whether minor violations of the MIME specification should be 
055         * tolerated or should result in a {@link MimeException}. If this parameter
056         * is set to <code>true</code>, a strict interpretation of the MIME 
057         * specification will be enforced, If this parameter is set to <code>false</code>
058         * minor violations will result in a warning in the log.
059         * 
060         * @param strictParsing value of the strict parsing mode
061         */
062        public void setStrictParsing(boolean strictParsing) {
063            this.strictParsing = strictParsing;
064        }
065    
066        /**
067         * Returns the value of the strict parsing mode
068         * @see #setStrictParsing(boolean)
069         * 
070         * @return value of the strict parsing mode
071         */
072        public boolean isStrictParsing() {
073            return this.strictParsing;
074        }
075        
076        /**
077         * Sets the maximum line length limit. Parsing of a MIME entity will be terminated 
078         * with a {@link MimeException} if a line is encountered that exceeds the maximum
079         * length limit. If this parameter is set to a non positive value the line length
080         * check will be disabled.
081         * 
082         * @param maxLineLen maximum line length limit
083         */
084        public void setMaxLineLen(int maxLineLen) {
085            this.maxLineLen = maxLineLen;
086        }
087        
088        /** 
089         * Returns the maximum line length limit
090         * @see #setMaxLineLen(int)
091         * 
092         * @return value of the the maximum line length limit
093         */
094        public int getMaxLineLen() {
095            return this.maxLineLen;
096        }
097    
098        /**
099         * Sets the maximum header limit. Parsing of a MIME entity will be terminated 
100         * with a {@link MimeException} if the number of headers exceeds the maximum
101         * limit. If this parameter is set to a non positive value the header limit check 
102         * will be disabled.
103         * 
104         * @param maxHeaderCount maximum header limit
105         */
106        public void setMaxHeaderCount(int maxHeaderCount) {
107            this.maxHeaderCount = maxHeaderCount;
108        }
109    
110        /** 
111         * Returns the maximum header limit
112         * @see #setMaxHeaderCount(int)
113         * 
114         * @return value of the the maximum header limit
115         */
116        public int getMaxHeaderCount() {
117            return this.maxHeaderCount;
118        }
119    
120        /**
121         * Sets the maximum content length limit. Parsing of a MIME entity will be terminated 
122         * with a {@link MimeException} if a content body exceeds the maximum length limit. 
123         * If this parameter is set to a non positive value the content length
124         * check will be disabled.
125         * 
126         * @param maxContentLen maximum content length limit
127         */
128        public void setMaxContentLen(long maxContentLen) {
129            this.maxContentLen = maxContentLen;
130        }
131    
132        /** 
133         * Returns the maximum content length limit
134         * @see #setMaxContentLen(long)
135         * 
136         * @return value of the the maximum content length limit
137         */
138        public long getMaxContentLen() {
139            return maxContentLen;
140        }
141    
142        /**
143         * Defines whether the parser should count line numbers. If enabled line
144         * numbers are included in the debug output.
145         * 
146         * @param countLineNumbers
147         *            value of the line number counting mode.
148         */
149        public void setCountLineNumbers(boolean countLineNumbers) {
150            this.countLineNumbers = countLineNumbers;
151        }
152    
153        /**
154         * Returns the value of the line number counting mode.
155         * 
156         * @return value of the line number counting mode.
157         */
158        public boolean isCountLineNumbers() {
159            return countLineNumbers;
160        }
161    
162        @Override
163        public MimeEntityConfig clone() {
164            try {
165                return (MimeEntityConfig) super.clone();
166            } catch (CloneNotSupportedException e) {
167                // this shouldn't happen, since we are Cloneable
168                throw new InternalError();
169            }
170        }
171        
172        @Override
173        public String toString() {
174            return "[max body descriptor: " + maximalBodyDescriptor
175                    + ", strict parsing: " + strictParsing + ", max line length: "
176                    + maxLineLen + ", max header count: " + maxHeaderCount
177                    + ", max content length: " + maxContentLen
178                    + ", count line numbers: " + countLineNumbers + "]";
179        }
180        
181    }