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.stream;
021    
022    import java.util.BitSet;
023    
024    import org.apache.james.mime4j.MimeException;
025    import org.apache.james.mime4j.io.MaxHeaderLengthLimitException;
026    import org.apache.james.mime4j.util.ByteArrayBuffer;
027    
028    /**
029     * Default implementation of {@link FieldBuilder}.
030     *
031     */
032    public class DefaultFieldBuilder implements FieldBuilder {
033    
034        private static final BitSet FIELD_CHARS = new BitSet();
035    
036        static {
037            for (int i = 0x21; i <= 0x39; i++) {
038                FIELD_CHARS.set(i);
039            }
040            for (int i = 0x3b; i <= 0x7e; i++) {
041                FIELD_CHARS.set(i);
042            }
043        }
044    
045        private final ByteArrayBuffer buf;
046        private final int maxlen;
047    
048        public DefaultFieldBuilder(int maxlen) {
049            this.buf = new ByteArrayBuffer(1024);
050            this.maxlen = maxlen;
051        }
052    
053        public void reset() {
054            this.buf.clear();
055        }
056    
057        public void append(final ByteArrayBuffer line) throws MaxHeaderLengthLimitException {
058            if (line == null) {
059                return;
060            }
061            int len = line.length();
062            if (this.maxlen > 0 && this.buf.length() + len >= this.maxlen) {
063                throw new MaxHeaderLengthLimitException("Maximum header length limit exceeded");
064            }
065            this.buf.append(line.buffer(), 0, line.length());
066        }
067    
068        public RawField build() throws MimeException {
069            int len = this.buf.length();
070            if (len > 0) {
071                if (this.buf.byteAt(len - 1) == '\n') {
072                    len --;
073                }
074                if (this.buf.byteAt(len - 1) == '\r') {
075                    len --;
076                }
077            }
078            ByteArrayBuffer copy = new ByteArrayBuffer(this.buf.buffer(), len, false);
079            RawField field = RawFieldParser.DEFAULT.parseField(copy);
080            String name = field.getName();
081            for (int i = 0; i < name.length(); i++) {
082                char ch = name.charAt(i);
083                if (!FIELD_CHARS.get(ch)) {
084                    throw new MimeException("MIME field name contains illegal characters: "
085                            + field.getName());
086                }
087            }
088            return field;
089        }
090    
091        public ByteArrayBuffer getRaw() {
092            return this.buf;
093        }
094    
095    }