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 020 package org.apache.james.mime4j.field; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 import org.apache.james.mime4j.field.address.AddressList; 025 import org.apache.james.mime4j.field.address.Mailbox; 026 import org.apache.james.mime4j.field.address.MailboxList; 027 import org.apache.james.mime4j.field.address.parser.ParseException; 028 import org.apache.james.mime4j.util.ByteSequence; 029 030 /** 031 * Mailbox field such as <code>Sender</code> or <code>Resent-Sender</code>. 032 */ 033 public class MailboxField extends AbstractField { 034 private static Log log = LogFactory.getLog(MailboxField.class); 035 036 private boolean parsed = false; 037 038 private Mailbox mailbox; 039 private ParseException parseException; 040 041 MailboxField(final String name, final String body, final ByteSequence raw) { 042 super(name, body, raw); 043 } 044 045 public Mailbox getMailbox() { 046 if (!parsed) 047 parse(); 048 049 return mailbox; 050 } 051 052 @Override 053 public ParseException getParseException() { 054 if (!parsed) 055 parse(); 056 057 return parseException; 058 } 059 060 private void parse() { 061 String body = getBody(); 062 063 try { 064 MailboxList mailboxList = AddressList.parse(body).flatten(); 065 if (mailboxList.size() > 0) { 066 mailbox = mailboxList.get(0); 067 } 068 } catch (ParseException e) { 069 if (log.isDebugEnabled()) { 070 log.debug("Parsing value '" + body + "': " + e.getMessage()); 071 } 072 parseException = e; 073 } 074 075 parsed = true; 076 } 077 078 static final FieldParser PARSER = new FieldParser() { 079 public ParsedField parse(final String name, final String body, 080 final ByteSequence raw) { 081 return new MailboxField(name, body, raw); 082 } 083 }; 084 }