[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.util.ByteSequence; |
---|
| 26 | import org.apache.james.mime4j.util.ContentUtil; |
---|
| 27 | import org.apache.james.mime4j.util.MimeUtil; |
---|
| 28 | |
---|
| 29 | /** |
---|
| 30 | * The basic immutable MIME field. |
---|
| 31 | */ |
---|
| 32 | public class RawField { |
---|
| 33 | |
---|
| 34 | private static final BitSet fieldChars = new BitSet(); |
---|
| 35 | |
---|
| 36 | static { |
---|
| 37 | for (int i = 0x21; i <= 0x39; i++) { |
---|
| 38 | fieldChars.set(i); |
---|
| 39 | } |
---|
| 40 | for (int i = 0x3b; i <= 0x7e; i++) { |
---|
| 41 | fieldChars.set(i); |
---|
| 42 | } |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | private ByteSequence raw; |
---|
| 47 | private int colonIdx; |
---|
| 48 | private int headerNameEndIdx; |
---|
| 49 | |
---|
| 50 | private String name; |
---|
| 51 | private String body; |
---|
| 52 | private final boolean obsoleteSyntax; |
---|
| 53 | |
---|
| 54 | public RawField(String name, String body) { |
---|
| 55 | this.name = name; |
---|
| 56 | this.body = body; |
---|
| 57 | this.raw = null; |
---|
| 58 | this.obsoleteSyntax = false; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | /** |
---|
| 62 | * @param raw bytes |
---|
| 63 | * @throws MimeException on malformed data |
---|
| 64 | */ |
---|
| 65 | public RawField(ByteSequence raw) throws MimeException { |
---|
| 66 | this.raw = raw; |
---|
| 67 | |
---|
| 68 | colonIdx = -1; |
---|
| 69 | boolean obsolete = false; |
---|
| 70 | for (int i = 0; i < raw.length(); i++) { |
---|
| 71 | if (!fieldChars.get(raw.byteAt(i) & 0xff)) { |
---|
| 72 | headerNameEndIdx = i; |
---|
| 73 | for (; i < raw.length(); i++) { |
---|
| 74 | int j = raw.byteAt(i) & 0xff; |
---|
| 75 | if (j == ':') { |
---|
| 76 | colonIdx = i; |
---|
| 77 | break; |
---|
| 78 | } else if (j != 0x20 && j != 0x09) { |
---|
| 79 | throw new MimeException("Invalid header. Unexpected char after colon: "+j); |
---|
| 80 | } else { |
---|
| 81 | obsolete = true; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | break; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | if (colonIdx == -1) throw new MimeException("Invalid header. No colon found."); |
---|
| 88 | obsoleteSyntax = obsolete; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public String getName() { |
---|
| 92 | if (name == null) { |
---|
| 93 | name = parseName(); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | return name; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | public String getBody() { |
---|
| 100 | if (body == null) { |
---|
| 101 | body = parseBody(); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return body; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | public ByteSequence getRaw() { |
---|
| 108 | if (raw == null) { |
---|
| 109 | raw = ContentUtil.encode(MimeUtil.fold(name+": "+body, 0)); |
---|
| 110 | } |
---|
| 111 | return raw; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | @Override |
---|
| 115 | public String toString() { |
---|
| 116 | return getName() + ": " + getBody(); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | private String parseName() { |
---|
| 120 | // make sure we ignore ending WSP (obsolete rfc822 syntax) |
---|
| 121 | return ContentUtil.decode(raw, 0, headerNameEndIdx); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | private String parseBody() { |
---|
| 125 | int offset = colonIdx + 1; |
---|
| 126 | // if the header body starts with a space we remove it. |
---|
| 127 | if (raw.length() > offset + 1 && (raw.byteAt(offset) & 0xff) == 0x20) offset++; |
---|
| 128 | int length = raw.length() - offset; |
---|
| 129 | return MimeUtil.unfold(ContentUtil.decode(raw, offset, length)); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | public boolean isObsoleteSyntax() { |
---|
| 133 | return obsoleteSyntax; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | } |
---|