KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > binding > mail > listeners > SessionDescriptorBuilder


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: SessionDescriptorBuilder.java 154 27 sept. 06 ofabre $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.binding.mail.listeners;
23
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import javax.jbi.JBIException;
30
31 import org.objectweb.petals.binding.mail.MailBCImpl;
32 import org.objectweb.petals.component.common.util.MessageExchangeWrapper;
33 import org.objectweb.petals.tools.jbicommon.util.StringHelper;
34
35 /**
36  * This class is used to extract information from an SMTP URI String
37  *
38  * @author ofabre - EBM Websourcing
39  *
40  */

41 public class SessionDescriptorBuilder {
42
43     protected Logger JavaDoc log;
44
45     /**
46      * @param address
47      * address extracted from a "provides" node of the jbi
48      * descriptor.
49      * <p>
50      * Address conforms to the following pattern (a valid URI
51      * pattern):
52      * <p>
53      * smtp://<i>user</i>:<i>password</i>@<i>host</i>:<i>port</i>/from:<i>fromAddress</i>/to:<i>toAddress</i>.
54      * <p>
55      * <i>fromAddress</i> and <i>toAddress</i> are classical email
56      * addresses like : foo@bar.com.
57      * @param log
58      * the component logger
59      */

60     public SessionDescriptorBuilder(Logger JavaDoc log) {
61         super();
62         this.log = log;
63     }
64
65     /**
66      * Retrieve mail session properties from the given address and message
67      * exchange.
68      *
69      * @param address
70      * the address uri
71      * @param exchangeWrapper
72      * the message exchange wrapper used to extract an optionnal
73      * destination address
74      * @return a Map of properties extracted from the given address.
75      * @throws JBIException
76      * if address isn't a valid URI or if checking fails
77      */

78     public SessionDescriptor build(String JavaDoc address,
79         MessageExchangeWrapper exchangeWrapper) throws JBIException {
80         String JavaDoc msg = "";
81         SessionDescriptor sessionDescriptor = null;
82
83         // Transform String address into URI address
84
URI JavaDoc addressURI;
85         try {
86             addressURI = new URI JavaDoc(address);
87         } catch (URISyntaxException JavaDoc e) {
88             msg = "The given address isn't a valid URI: " + address;
89             log.severe(msg);
90             throw new JBIException(msg);
91         }
92
93         /*
94          * Retrieve properties values from address URI
95          */

96         // Retrieve user and password
97
String JavaDoc username = null;
98         String JavaDoc password = null;
99         String JavaDoc userInfo = addressURI.getRawUserInfo();
100         if (!StringHelper.isNullOrEmpty(userInfo)) {
101             if (userInfo.indexOf(":") > 0) {
102                 username = addressURI.getUserInfo().split(":")[0];
103                 password = addressURI.getUserInfo().split(":")[1];
104             } else {
105                 username = addressURI.getUserInfo();
106             }
107         }
108
109         // Retrieve host and port
110
String JavaDoc hostname = addressURI.getHost();
111         int port = addressURI.getPort();
112
113         // Retrieve protocol (smtp, imap, pop3)
114
String JavaDoc scheme = addressURI.getScheme();
115
116         // Retrieve "from" and "to" addresses
117
String JavaDoc fromAddress = "";
118         String JavaDoc toAddress = "";
119         String JavaDoc path = addressURI.getPath();
120         List JavaDoc<String JavaDoc> pathElements = StringHelper.splitPathElements(path);
121         for (String JavaDoc string : pathElements) {
122             if (string.indexOf(MailBCImpl.FROM_PATHELEMENT) >= 0) {
123                 fromAddress = string.substring(string
124                     .indexOf(MailBCImpl.FROM_PATHELEMENT)
125                     + MailBCImpl.FROM_PATHELEMENT.length());
126             } else if (string.indexOf(MailBCImpl.TO_PATHELEMENT) >= 0) {
127                 toAddress = string.substring(string
128                     .indexOf(MailBCImpl.TO_PATHELEMENT)
129                     + MailBCImpl.TO_PATHELEMENT.length());
130             }
131         }
132         // if the destination address is null, try to find it in the message
133
// exchange, in case of an SMTP scheme
134
if (MailBCImpl.MAIL_SCHEME_SMTP.equalsIgnoreCase(scheme)
135             && StringHelper.isNullOrEmpty(toAddress)) {
136             toAddress = (String JavaDoc) exchangeWrapper
137                 .getProperty(MailBCImpl.DESTINATION_ADDRESS);
138         }
139
140         String JavaDoc query = addressURI.getQuery();
141         // Retrieve period parameter ("checking for new mail" interval)
142
String JavaDoc period = StringHelper.extractValueForAttribute(query,
143             MailBCImpl.PERIOD_QUERYELEMENT, MailBCImpl.QUERY_SEPARATOR);
144
145         // Retrieve folder parameter (the folder to search for new mails)
146
String JavaDoc folder = StringHelper.extractValueForAttribute(query,
147             MailBCImpl.FOLDER_QUERYELEMENT, MailBCImpl.QUERY_SEPARATOR);
148
149         // Check obligatory properties
150
checkProperties(hostname, toAddress, scheme);
151
152         // Build the SessionDescriptor, use default value if
153
// null or empty
154
sessionDescriptor = fillProperties(hostname, port, username, password,
155             fromAddress, toAddress, period, folder, scheme);
156
157         return sessionDescriptor;
158     }
159
160     /**
161      * Check the properties extracted from the address URI. Only hostname is
162      * obligatory for all schemes. Only SMTP, POP3 and IMAP schemes are
163      * supported. "toAddress" is need for SMTP scheme.
164      *
165      * @param hostname
166      * @param toAddress
167      * @throws JBIException
168      * if a checking step failed
169      */

170     protected void checkProperties(String JavaDoc hostname, String JavaDoc toAddress,
171         String JavaDoc scheme) throws JBIException {
172         String JavaDoc msg = "";
173         if (StringHelper.isNullOrEmpty(hostname)) {
174             msg = "The host name of the requested external "
175                 + "service must be specified";
176             log.severe(msg);
177             throw new JBIException(msg);
178         }
179         if (!(MailBCImpl.MAIL_SCHEME_IMAP.equalsIgnoreCase(scheme)
180             || MailBCImpl.MAIL_SCHEME_POP3.equalsIgnoreCase(scheme) || MailBCImpl.MAIL_SCHEME_SMTP
181             .equalsIgnoreCase(scheme))) {
182             msg = "The specified scheme isn't supported : " + scheme;
183             log.severe(msg);
184             throw new JBIException(msg);
185         }
186         if (MailBCImpl.MAIL_SCHEME_SMTP.equals(scheme)) {
187             if (StringHelper.isNullOrEmpty(toAddress)) {
188                 msg = "Destination email address must be specified "
189                     + "in the Service Unit descriptor ('provides' node) or in the "
190                     + MailBCImpl.DESTINATION_ADDRESS
191                     + " Message Exchange property";
192                 log.severe(msg);
193                 throw new JBIException(msg);
194             }
195         }
196     }
197
198     /**
199      * Build a {@link SessionDescriptor}, use default value if null or empty
200      *
201      * @param hostname
202      * the hostname, non null and non empty
203      * @param port
204      * the port, default value : "25"
205      * @param username
206      * the username, default value : no default
207      * @param password
208      * the password, default value : no default
209      * @param fromAddress
210      * the return address, default value : no default
211      * @param toAddress
212      * the destination address, non null and non empty
213      * @param period
214      * time elapse between two new mail checkings, default value :
215      * "60000" ms
216      * @param folder
217      * the folder to check for new mails, default value : "INBOX"
218      * @param scheme
219      * the access or transport protocol "smtp", "imap" or "pop3", non
220      * null and non empty
221      * @return the {@link SessionDescriptor}
222      */

223     protected SessionDescriptor fillProperties(String JavaDoc hostname, int port,
224         String JavaDoc username, String JavaDoc password, String JavaDoc fromAddress, String JavaDoc toAddress,
225         String JavaDoc period, String JavaDoc folder, String JavaDoc scheme) {
226         SessionDescriptor sessionDescriptor = new SessionDescriptor();
227
228         sessionDescriptor.setHostname(hostname);
229         if (port == -1) {
230             if (MailBCImpl.MAIL_SCHEME_POP3.equals(scheme)) {
231                 sessionDescriptor.setPort(MailBCImpl.MAIL_POP3_PORT_DEFAULT);
232             } else if (MailBCImpl.MAIL_SCHEME_IMAP.equals(scheme)) {
233                 sessionDescriptor.setPort(MailBCImpl.MAIL_IMAP_PORT_DEFAULT);
234             } else {
235                 sessionDescriptor.setPort(MailBCImpl.MAIL_SMTP_PORT_DEFAULT);
236             }
237         } else {
238             sessionDescriptor.setPort(new Integer JavaDoc(port).toString());
239         }
240         sessionDescriptor.setUsername(username);
241         sessionDescriptor.setPassword(password);
242         sessionDescriptor.setFromAddress(fromAddress);
243         sessionDescriptor.setToAddress(toAddress);
244         if (!StringHelper.isNullOrEmpty(period)) {
245             sessionDescriptor.setPeriod(period);
246         } else {
247             sessionDescriptor.setPeriod(MailBCImpl.PERIOD_DEFAULT);
248         }
249         if (!StringHelper.isNullOrEmpty(folder)) {
250             sessionDescriptor.setFolder(folder);
251         } else {
252             sessionDescriptor.setFolder(MailBCImpl.FOLDER_DEFAULT);
253         }
254         sessionDescriptor.setScheme(scheme);
255
256         return sessionDescriptor;
257     }
258 }
259
Popular Tags