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
020package org.apache.james.mime4j.field;
021
022import java.util.Collections;
023
024import org.apache.james.mime4j.codec.DecodeMonitor;
025import org.apache.james.mime4j.dom.FieldParser;
026import org.apache.james.mime4j.dom.address.AddressList;
027import org.apache.james.mime4j.dom.address.Mailbox;
028import org.apache.james.mime4j.dom.field.AddressListField;
029import org.apache.james.mime4j.field.address.LenientAddressBuilder;
030import org.apache.james.mime4j.stream.Field;
031import org.apache.james.mime4j.stream.ParserCursor;
032import org.apache.james.mime4j.stream.RawField;
033import org.apache.james.mime4j.util.ByteSequence;
034import org.apache.james.mime4j.util.ContentUtil;
035
036/**
037 * Address list field such as <code>To</code> or <code>Reply-To</code>.
038 */
039public class AddressListFieldLenientImpl extends AbstractField implements AddressListField {
040
041    private boolean parsed = false;
042
043    private AddressList addressList;
044
045    AddressListFieldLenientImpl(final Field rawField, final DecodeMonitor monitor) {
046        super(rawField, monitor);
047    }
048
049    public AddressList getAddressList() {
050        if (!parsed)
051            parse();
052
053        return addressList;
054    }
055
056    private void parse() {
057        parsed = true;
058        RawField f = getRawField();
059        ByteSequence buf = f.getRaw();
060        int pos = f.getDelimiterIdx() + 1;
061        if (buf == null) {
062            String body = f.getBody();
063            if (body == null) {
064                addressList = new AddressList(Collections.<Mailbox>emptyList(), true);
065                return;
066            }
067            buf = ContentUtil.encode(body);
068            pos = 0;
069        }
070        ParserCursor cursor = new ParserCursor(pos, buf.length());
071        addressList = LenientAddressBuilder.DEFAULT.parseAddressList(buf, cursor);
072    }
073
074    public static final FieldParser<AddressListField> PARSER = new FieldParser<AddressListField>() {
075
076        public AddressListField parse(final Field rawField, final DecodeMonitor monitor) {
077            return new AddressListFieldLenientImpl(rawField, monitor);
078        }
079
080    };
081
082}