1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.smtp;
24  
25  import com.liferay.portal.kernel.smtp.MessageListener;
26  import com.liferay.portal.kernel.smtp.MessageListenerException;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  import org.subethamail.smtp.TooMuchDataException;
35  
36  /**
37   * <a href="SubEthaMessageListenerImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class SubEthaMessageListenerImpl
43      implements org.subethamail.smtp.MessageListener {
44  
45      public SubEthaMessageListenerImpl(MessageListener listener) {
46          _listener = listener;
47      }
48  
49      public boolean accept(String from, String recipient) {
50          if (_log.isDebugEnabled()) {
51              _log.debug("Listener " + _listener.getClass().getName());
52              _log.debug("From " + from);
53              _log.debug("Recipient " + recipient);
54          }
55  
56          boolean value = _listener.accept(from, recipient);
57  
58          if (_log.isDebugEnabled()) {
59              _log.debug("Accept " + value);
60          }
61  
62          return value;
63      }
64  
65      public void deliver(String from, String recipient, InputStream data)
66          throws IOException, TooMuchDataException {
67  
68          try {
69              if (_log.isDebugEnabled()) {
70                  _log.debug("Listener " + _listener.getClass().getName());
71                  _log.debug("From " + from);
72                  _log.debug("Recipient " + recipient);
73                  _log.debug("Data " + data);
74              }
75  
76              _listener.deliver(from, recipient, data);
77          }
78          catch (MessageListenerException mle) {
79              Throwable cause = mle.getCause();
80  
81              if (cause instanceof IOException) {
82                  throw (IOException)cause;
83              }
84              else if (cause instanceof TooMuchDataException) {
85                  throw (TooMuchDataException)cause;
86              }
87              else if (cause != null) {
88                  throw new IOException(cause.getMessage());
89              }
90              else {
91                  throw new IOException(mle.getMessage());
92              }
93          }
94      }
95  
96      public String getId() {
97          return _listener.getId();
98      }
99  
100     public boolean equals(Object obj) {
101         if (obj == null) {
102             return false;
103         }
104 
105         SubEthaMessageListenerImpl listener = null;
106 
107         try {
108             listener = (SubEthaMessageListenerImpl)obj;
109         }
110         catch (ClassCastException cce) {
111             return false;
112         }
113 
114         String id = listener.getId();
115 
116         if (getId().equals(id)) {
117             return true;
118         }
119         else {
120             return false;
121         }
122     }
123 
124     private static Log _log =
125         LogFactory.getLog(SubEthaMessageListenerImpl.class);
126 
127     private MessageListener _listener;
128 
129 }