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