001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.sieve;
017    
018    import java.io.IOException;
019    import java.lang.reflect.InvocationTargetException;
020    import java.util.ArrayList;
021    import java.util.HashSet;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.ListIterator;
025    import java.util.Set;
026    
027    import org.apache.james.mime4j.field.address.AddressList;
028    import org.apache.james.mime4j.field.address.Mailbox;
029    import org.apache.james.mime4j.field.address.MailboxList;
030    import org.apache.james.mime4j.field.address.parser.ParseException;
031    import org.apache.james.mime4j.message.Header;
032    import org.apache.james.mime4j.parser.Field;
033    import org.apache.jsieve.exception.InternetAddressException;
034    import org.apache.jsieve.exception.SieveException;
035    import org.apache.jsieve.mail.Action;
036    import org.apache.jsieve.mail.AddressImpl;
037    import org.apache.jsieve.mail.MailAdapter;
038    import org.apache.jsieve.mail.SieveMailException;
039    
040    import com.hs.mail.mailet.MailetContext;
041    import com.hs.mail.smtp.message.SmtpMessage;
042    
043    public class SieveMailAdapter implements MailAdapter {
044    
045            /**
046             * The Message being adapted.
047             */
048            private SmtpMessage fieldMessage;
049            /**
050             * List of Actions to perform.
051             */
052            private List fieldActions;
053        /**
054         * The MailetContext.
055         */
056        private MailetContext fieldMailetContext;
057            /**
058             * The sole intended recipient for the message being adapted;
059             */
060            private String soleRecipient;
061            /**
062             * The ID of sole intended recipient for the message being adapted;
063             */
064            private long soleRecipientID;
065            /**
066         * Constructor for SieveMailAdapter.
067         */
068            public SieveMailAdapter(MailetContext aMailetContext, String soleRecipient,
069                            long soleRecipientID) {
070                    super();
071                    setMailetContext(aMailetContext);
072                    setSoleRecipient(soleRecipient);
073                    setSoleRecipientID(soleRecipientID);
074            }
075    
076        /**
077         * @return Returns the fieldMessage.
078         */
079        public SmtpMessage getMessage() {
080                    return fieldMessage;
081            }
082    
083        /**
084         * @param fieldMessage The fieldMessage to set.
085         */
086            public void setMessage(SmtpMessage aMessage) {
087                    this.fieldMessage = aMessage;
088            clearActions();
089            }
090    
091            /**
092             * @return Returns the soleRecipient.
093             */
094            public String getSoleRecipient() {
095                    return soleRecipient;
096            }
097    
098            /**
099             * @param soleRecipient
100             *            The soleRecipient to set.
101             */
102            public void setSoleRecipient(String soleRecipient) {
103                    this.soleRecipient = soleRecipient;
104            }
105    
106            /**
107             * @return Returns the ID of soleRecipient.
108             */
109            public long getSoleRecipientID() {
110                    return soleRecipientID;
111            }
112    
113            /**
114             * @param soleRecipientID
115             *            The ID of soleRecipient to set.
116             */
117            public void setSoleRecipientID(long soleRecipientID) {
118                    this.soleRecipientID = soleRecipientID;
119            }
120    
121            /**
122             * Returns the List of actions.
123             * 
124             * @return List
125             */
126            public List getActions() {
127                    List actions = null;
128                    if (null == (actions = getActionsBasic())) {
129                            updateActions();
130                            return getActions();
131                    }
132                    return actions;
133            }
134    
135            /**
136             * Remove all actions.
137             * 
138             */
139            private void clearActions() {
140                    getActions().clear();
141            }
142    
143            /**
144             * Returns a new List of actions.
145             * 
146             * @return List
147             */
148            protected List computeActions() {
149                    return new ArrayList();
150            }
151    
152            /**
153             * Returns the List of actions.
154             * 
155             * @return List
156             */
157            private List getActionsBasic() {
158                    return fieldActions;
159            }
160    
161        /**
162         * Adds an Action.
163         * 
164         * @param action The action to set
165         */
166            public void addAction(Action action) {
167            getActions().add(action);
168            }
169    
170        /**
171         * @see org.apache.jsieve.mail.MailAdapter#executeActions()
172         */
173            public void executeActions() throws SieveException {
174                    ListIterator actionsIter = getActionsIterator();
175                    while (actionsIter.hasNext()) {
176                            Action action = (Action) actionsIter.next();
177                            try {
178                                    ActionDispatcher.getInstance().execute(action, this,
179                                                    getMailetContext());
180                            } catch (IllegalArgumentException e) {
181                    throw new SieveException(e.getMessage());
182                            } catch (NoSuchMethodException e) {
183                    throw new SieveException(e.getMessage());
184                            } catch (IllegalAccessException e) {
185                    throw new SieveException(e.getMessage());
186                            } catch (InvocationTargetException e) {
187                    throw new SieveException(e.getMessage());
188                            }
189                    }
190            }
191    
192            /**
193             * Sets the actions.
194             * 
195             * @param actions
196             *            The actions to set
197             */
198            protected void setActions(List actions) {
199                    fieldActions = actions;
200            }
201    
202            /**
203             * Updates the actions.
204             */
205            protected void updateActions() {
206                    setActions(computeActions());
207            }
208            
209        /**
210         * @see org.apache.jsieve.mail.MailAdapter#getActionsIterator()
211         */
212            public ListIterator getActionsIterator() {
213            return getActions().listIterator();
214            }
215    
216            public Object getContent() throws SieveMailException {
217                    // TODO Auto-generated method stub
218                    return null;
219            }
220    
221            public String getContentType() throws SieveMailException {
222                    // TODO Auto-generated method stub
223                    return null;
224            }
225    
226        /**
227         * @see org.apache.jsieve.mail.MailAdapter#getHeader(String)
228         */
229            public List getHeader(String name) throws SieveMailException {
230                    List<Field> fields = getHeader().getFields(name);
231                    List headers = new ArrayList(fields.size());
232                    for (Field field : fields) {
233                            headers.add(field.getBody());
234                    }
235                    return headers;
236            }
237    
238        /**
239         * @see org.apache.jsieve.mail.MailAdapter#getHeaderNames()
240         */
241            public List getHeaderNames() throws SieveMailException {
242                    List<Field> fields = getHeader().getFields();
243                    Set headerNames = new HashSet();
244                    for (Field field : fields) {
245                            headerNames.add(field.getName());
246                    }
247                    return new ArrayList(headerNames);
248            }
249    
250        /**
251         * @see org.apache.jsieve.mail.MailAdapter#getMatchingHeader(String)
252         */
253            public List getMatchingHeader(String name) throws SieveMailException {
254                    Iterator headerNamesIter = getHeaderNames().iterator();
255                    List matchedHeaderValues = new ArrayList(32);
256                    while (headerNamesIter.hasNext()) {
257                            String headerName = (String) headerNamesIter.next();
258                            if (headerName.trim().equalsIgnoreCase(name)) {
259                                    matchedHeaderValues.addAll(getHeader(headerName));
260                            }
261                    }
262                    return matchedHeaderValues;
263            }
264    
265        /**
266         * @see org.apache.jsieve.mail.MailAdapter#getSize()
267         */
268            public int getSize() throws SieveMailException {
269                    try {
270                            return (int) fieldMessage.getMailMessage().getSize();
271                    } catch (IOException e) {
272                            throw new SieveMailException(e);
273                    }
274            }
275    
276            public Address[] parseAddresses(String headerName)
277                            throws SieveMailException, InternetAddressException {
278                    try {
279                            Field field = getHeader().getField(headerName);
280                MailboxList list = AddressList.parse(field.getBody()).flatten();
281                final int size = list.size();
282                final Address[] results = new Address[size];
283                for (int i=0;i<size;i++) {
284                    final Mailbox mailbox = list.get(i);
285                    results[i] = new AddressImpl(mailbox.getLocalPart(), mailbox.getDomain());
286                }
287                return null;
288            } catch (ParseException e) {
289                throw new InternetAddressException(e);
290            }
291            }
292    
293            private Header getHeader() {
294                    try {
295                            return fieldMessage.getMailMessage().getHeader().getHeader();
296                    } catch (IOException e) {
297                            return null;
298                    }
299            }
300    
301            /**
302             * Returns the mailetContext.
303             * 
304             * @return MailetContext
305             */
306            public MailetContext getMailetContext() {
307                    return fieldMailetContext;
308            }
309    
310            /**
311             * Sets the mailetContext.
312             * 
313             * @param mailetContext
314             *            The mailetContext to set
315             */
316            protected void setMailetContext(MailetContext mailetContext) {
317                    fieldMailetContext = mailetContext;
318            }
319    
320    }