View Javadoc

1   /*
2    * Copyright (c) 2006-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.smtp.logging;
14  
15  import org.abstracthorizon.danube.connection.Connection;
16  import org.abstracthorizon.danube.support.logging.patternsupport.PatternProcessor;
17  import org.abstracthorizon.danube.support.logging.util.StringUtil;
18  import org.abstracthorizon.mercury.smtp.SMTPSession;
19  import org.abstracthorizon.mercury.smtp.filter.MailSessionData;
20  import org.abstracthorizon.mercury.smtp.util.Path;
21  
22  /**
23   * This processor adds following pattern codes:
24   *
25   * <ul>
26   * <li><code>%b</code> - bytes sent excluding headers or &quot;-&quot; if nothing</li>
27   * <li><code>%B</code> - bytes sent excluding headers or 0 if nothing</li>
28   * <li><code>%E</code> - helo/ehlo string</li>
29   * <li><code>%r</code> - return code
30   * <li><code>%S</code> - source mailbox (MAIL FROM directive)
31   * <li><code>%R</code> - recipients
32   * </ul>
33   *
34   * @author Daniel Sendula
35   */
36  public class SMTPSPAMPatternProcessor implements PatternProcessor {
37  
38      /** Cached index of bytes sent */
39      protected int bytesSentIndex = -1;
40  
41      /** Cached index of bytes sent with zero */
42      protected int bytesSent0Index = -1;
43  
44      /** Cached index of ehlo source domain string */
45      protected int ehloIndex = -1;
46  
47      /** Cached index of return code */
48      protected int returnCodeIndex = -1;
49  
50      /** Cached index of source */
51      protected int sourceIndex = -1;
52  
53      /** Cached index of recipients */
54      protected int recipientsIndex = -1;
55  
56  
57      /**
58       * Constructor
59       */
60      public SMTPSPAMPatternProcessor() {
61      }
62  
63      /**
64       * Checks if parameters are present and if so replaces it and caches their indexes
65       * @param index next index to be used
66       * @param message message to be altered
67       */
68      public int init(int index, StringBuffer message) {
69          bytesSentIndex = -1;
70          bytesSent0Index = -1;
71          ehloIndex = -1;
72  
73          if (message.indexOf("%b") >= 0) {
74              StringUtil.replaceAll(message, "%b", "{" + index + "}");
75              bytesSentIndex = index;
76              index = index + 1;
77          }
78  
79          if (message.indexOf("%B") >= 0) {
80              StringUtil.replaceAll(message, "%B", "{" + index + "}");
81              bytesSent0Index = index;
82              index = index + 1;
83          }
84  
85          if (message.indexOf("%E") >= 0) {
86              StringUtil.replaceAll(message, "%E", "{" + index + "}");
87              ehloIndex = index;
88              index = index + 1;
89          }
90  
91          if (message.indexOf("%R") >= 0) {
92              StringUtil.replaceAll(message, "%R", "{" + index + "}");
93              recipientsIndex = index;
94              index = index + 1;
95          }
96  
97          if (message.indexOf("%r") >= 0) {
98              StringUtil.replaceAll(message, "%r", "{" + index + "}");
99              returnCodeIndex = index;
100             index = index + 1;
101         }
102 
103         if (message.indexOf("%S") >= 0) {
104             StringUtil.replaceAll(message, "%S", "{" + index + "}");
105             sourceIndex = index;
106             index = index + 1;
107         }
108 
109         return index;
110     }
111 
112     /**
113      * Adds parameter values to cached index positions
114      * @param connection connection
115      * @param array array
116      */
117     public void process(Connection connection, Object[] array) {
118         SMTPSession smtpSession = (SMTPSession)connection.adapt(SMTPSession.class);
119         MailSessionData data = smtpSession.getMailSessionData();
120 
121         if (bytesSentIndex >= 0) {
122             long b = data.getTotalBytes();
123             if (b <= 0) {
124                 array[bytesSentIndex] = "-";
125             } else {
126                 array[bytesSentIndex] = Long.toString(b);
127             }
128         }
129 
130         if (bytesSent0Index >= 0) {
131             array[bytesSent0Index] = Long.toString(data.getTotalBytes());
132         }
133 
134         if (ehloIndex >= 0) {
135             array[ehloIndex] = data.getSourceDomain();
136         }
137 
138         if (returnCodeIndex >= 0) {
139             int code = data.getReturnCode();
140             if (code == 250) {
141                 code = 1;
142             }
143             array[returnCodeIndex] = Integer.toString(code);
144         }
145 
146         if (sourceIndex >= 0) {
147             Path source = data.getSourceMailbox();
148             if (source == null) {
149                 array[sourceIndex] = "-";
150             } else {
151                 array[sourceIndex] = source.toMailboxString();
152             }
153         }
154 
155         if (recipientsIndex >= 0) {
156             StringBuffer recipients = new StringBuffer();
157             boolean first = true;
158             if (data.getDestinationMailboxes() != null) {
159                 for (Path path : data.getDestinationMailboxes()) {
160                     if (first) {
161                         first = false;
162                     } else {
163                         recipients.append(',');
164                     }
165                     recipients.append(path.toMailboxString());
166                 }
167                 if (recipients.length() == 0) {
168                     array[recipientsIndex] = "-";
169                 } else {
170                     array[recipientsIndex] = recipients;
171                 }
172             } else {
173                 array[recipientsIndex] = "-";
174             }
175         }
176     }
177 }