[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 java.util.BitSet; |
---|
| 23 | |
---|
| 24 | import org.apache.james.mime4j.MimeException; |
---|
| 25 | import org.apache.james.mime4j.io.MaxHeaderLengthLimitException; |
---|
| 26 | import org.apache.james.mime4j.util.ByteArrayBuffer; |
---|
| 27 | |
---|
| 28 | public class DefaultFieldBuilder implements FieldBuilder { |
---|
| 29 | |
---|
| 30 | private static final BitSet FIELD_CHARS = new BitSet(); |
---|
| 31 | |
---|
| 32 | static { |
---|
| 33 | for (int i = 0x21; i <= 0x39; i++) { |
---|
| 34 | FIELD_CHARS.set(i); |
---|
| 35 | } |
---|
| 36 | for (int i = 0x3b; i <= 0x7e; i++) { |
---|
| 37 | FIELD_CHARS.set(i); |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | private final ByteArrayBuffer buf; |
---|
| 42 | private final int maxlen; |
---|
| 43 | |
---|
| 44 | public DefaultFieldBuilder(int maxlen) { |
---|
| 45 | this.buf = new ByteArrayBuffer(1024); |
---|
| 46 | this.maxlen = maxlen; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public void reset() { |
---|
| 50 | this.buf.clear(); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | public void append(final ByteArrayBuffer line) throws MaxHeaderLengthLimitException { |
---|
| 54 | if (line == null) { |
---|
| 55 | return; |
---|
| 56 | } |
---|
| 57 | int len = line.length(); |
---|
| 58 | if (this.maxlen > 0 && this.buf.length() + len >= this.maxlen) { |
---|
| 59 | throw new MaxHeaderLengthLimitException("Maximum header length limit exceeded"); |
---|
| 60 | } |
---|
| 61 | this.buf.append(line.buffer(), 0, line.length()); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | public RawField build() throws MimeException { |
---|
| 65 | int len = this.buf.length(); |
---|
| 66 | if (len > 0) { |
---|
| 67 | if (this.buf.byteAt(len - 1) == '\n') { |
---|
| 68 | len --; |
---|
| 69 | } |
---|
| 70 | if (this.buf.byteAt(len - 1) == '\r') { |
---|
| 71 | len --; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | ByteArrayBuffer copy = new ByteArrayBuffer(this.buf.buffer(), len, false); |
---|
| 75 | RawField field = RawFieldParser.DEFAULT.parseField(copy); |
---|
| 76 | String name = field.getName(); |
---|
| 77 | for (int i = 0; i < name.length(); i++) { |
---|
| 78 | char ch = name.charAt(i); |
---|
| 79 | if (!FIELD_CHARS.get(ch)) { |
---|
| 80 | throw new MimeException("MIME field name contains illegal characters: " |
---|
| 81 | + field.getName()); |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | return field; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | public ByteArrayBuffer getRaw() { |
---|
| 88 | return this.buf; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | } |
---|