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.AddressBuilder; 027import org.apache.james.mime4j.field.address.ParseException; 028import org.apache.james.mime4j.stream.Field; 029 030/** 031 * Mailbox field such as <code>Sender</code> or <code>Resent-Sender</code>. 032 */ 033public class MailboxFieldImpl extends AbstractField implements MailboxField { 034 private boolean parsed = false; 035 036 private Mailbox mailbox; 037 private ParseException parseException; 038 039 MailboxFieldImpl(Field rawField, DecodeMonitor monitor) { 040 super(rawField, monitor); 041 } 042 043 /** 044 * @see org.apache.james.mime4j.dom.field.MailboxField#getMailbox() 045 */ 046 public Mailbox getMailbox() { 047 if (!parsed) 048 parse(); 049 050 return mailbox; 051 } 052 053 /** 054 * @see org.apache.james.mime4j.dom.field.MailboxField#getParseException() 055 */ 056 @Override 057 public ParseException getParseException() { 058 if (!parsed) 059 parse(); 060 061 return parseException; 062 } 063 064 private void parse() { 065 String body = getBody(); 066 067 try { 068 mailbox = AddressBuilder.DEFAULT.parseMailbox(body, monitor); 069 } catch (ParseException e) { 070 parseException = e; 071 } 072 073 parsed = true; 074 } 075 076 public static final FieldParser<MailboxField> PARSER = new FieldParser<MailboxField>() { 077 078 public MailboxField parse(final Field rawField, final DecodeMonitor monitor) { 079 return new MailboxFieldImpl(rawField, monitor); 080 } 081 082 }; 083}