1 9 package org.jboss.net.axis.transport.mailto.client; 10 11 import java.io.IOException ; 12 import java.util.Date ; 13 14 import javax.mail.AuthenticationFailedException ; 15 import javax.mail.FetchProfile ; 16 import javax.mail.Flags ; 17 import javax.mail.Folder ; 18 import javax.mail.Message ; 19 import javax.mail.MessagingException ; 20 import javax.mail.NoSuchProviderException ; 21 import javax.mail.Session ; 22 import javax.mail.Store ; 23 24 import org.apache.axis.AxisFault; 25 import org.apache.axis.MessageContext; 26 import org.apache.axis.message.addressing.AddressingHeaders; 27 import org.apache.axis.message.addressing.Constants; 28 import org.apache.axis.message.addressing.MessageID; 29 30 31 42 public class SyncMailSender extends BaseMailSender 43 { 44 public static final String FOLDER_NAME = "FolderName"; 46 public static final String USERNAME = "Username"; 47 public static final String PASSWORD = "Password"; 48 49 public static final String INBOX = "INBOX"; 51 52 public static final String TIMEOUT = "Timeout"; 53 public static final long DEFAULT_TIMEOUT = 1800000; 55 protected long getTimeout() 56 { 57 long timeout = DEFAULT_TIMEOUT; 58 String to = (String ) getOption(TIMEOUT); 59 if (to != null) 60 { 61 try 62 { 63 timeout = Long.parseLong(to) * 60 * 1000; 64 } catch (NumberFormatException e) 65 { 66 log.warn(to + "is not a valid number we will use 30 minutes instead.", e); 68 } 69 } 70 return timeout; 71 } 72 protected void checkResponse(MessageContext ctx) throws AxisFault 73 { 74 String msgID = getMessageID(ctx); 75 String folderName = getFolderName(); 76 long timeout = getTimeout(); 77 78 79 Session mail = getMailSession(); 80 Store store = null; 81 Folder folder = null; 82 83 try 84 { 85 store = mail.getStore(); 86 if (log.isDebugEnabled()) 87 log.debug(store.getClass().getName()); 88 } 89 catch (NoSuchProviderException e) 90 { 91 log.fatal("The mail session does not have a default provider! Check the mail.store.protocal " 93 + "parameter in the wsdd file", e); 94 throw new AxisFault("No mail store provider has been configured.", e); 95 } 96 97 long end = new Date ().getTime() + timeout; 98 while (end > new Date ().getTime()) 99 { 100 101 try 102 { 103 String user = (String ) getOption(USERNAME); 105 String password = (String ) getOption(PASSWORD); 106 if (user != null && password != null) 107 store.connect(null, user, password); 108 else 110 store.connect(); 111 } 112 catch (AuthenticationFailedException e) 113 { 114 log.fatal("The User and/or Password defined in the mail-service.xml file could be wrong or missing", e); 116 throw new AxisFault("Authentication Failed", e); 117 } 118 catch (MessagingException e) 119 { 120 log.fatal("Unable to connect to the mail store.", e); 122 throw new AxisFault("Unable to connect to the mail store", e); 123 } 124 catch (IllegalStateException e) 125 { 126 log.warn("The store is already connected!", e); 129 } 130 131 try 132 { 133 folder = store.getFolder(folderName); 134 135 if (log.isDebugEnabled()) 136 log.debug(folder.getClass().getName()); 137 138 if (!folder.exists()) 140 { 141 log.fatal("The folder '" + folderName + "' doe not exist."); 142 closeStore(store); 144 return; 145 } 146 } 147 catch (MessagingException e) 148 { 149 log.fatal("Unable to retrieve the folder '" + folderName + "' from the store.", e); 150 closeStore(store); 151 throw new AxisFault("Unable to retrieve the folder '" + folderName + "' from the store.", e); 152 } 153 154 try 155 { 156 folder.open(Folder.READ_WRITE); 157 } 158 catch (MessagingException e) 159 { 160 log.fatal("Failed to open the folder'" + folderName + "'.", e); 162 closeStore(store); 163 throw new AxisFault("Failed to open the folder'" + folderName + "'.", e); 164 } 165 try 166 { 167 Message msg = fetchMessage(ctx, msgID, folder); 168 169 if (msg != null) 171 { 172 org.apache.axis.Message soapMsg = new org.apache.axis.Message(msg.getInputStream(), false, msg 173 .getContentType(), null); 174 ctx.setResponseMessage(soapMsg); 175 ctx.setPastPivot(true); 176 msg.setFlag(Flags.Flag.DELETED, true); 177 break; 178 } 179 } 180 catch (IOException e) 181 { 182 log.fatal(e); 183 throw new AxisFault("IOException", e); 184 } 185 catch (MessagingException e) 186 { 187 log.fatal(e); 188 throw new AxisFault("messagingException", e); 189 } 190 finally 191 { 192 closeFolder(folder); 194 closeStore(store); 195 } 196 try 197 { 198 Thread.sleep(60000); 200 } 201 catch (InterruptedException e1) 202 { 203 break; 205 } 206 } 207 } 208 209 protected String getFolderName() 210 { 211 String folder = (String ) getOption(FOLDER_NAME); 212 if (folder == null) folder = INBOX; 213 return folder; 214 } 215 216 private Message fetchMessage(MessageContext ctx, String msgID, Folder folder) throws AxisFault 217 { 218 Message [] messages = new Message [0]; 219 Message resMsg = null; 220 221 try 222 { 223 if (log.isDebugEnabled()) 224 log.debug("\nMessageCount: " + folder.getMessageCount() + "\nNewMessageCount: " 225 + folder.getNewMessageCount() + "\nUnreadMessageCount: " + folder.getUnreadMessageCount()); 226 227 if (folder.getMessageCount() > 0) 228 { 229 messages = folder.getMessages(); 230 FetchProfile fp = new FetchProfile (); 231 fp.add(FetchProfile.Item.CONTENT_INFO); 232 233 fp.add(FetchProfile.Item.ENVELOPE); 235 236 folder.fetch(messages, fp); 237 238 for (int i = 0; i < messages.length; i++) 239 { 240 Message message = messages[i]; 241 String inReplyTo = null; 242 try 243 { 244 inReplyTo = message.getHeader(HEADER_IN_REPLY_TO)[0]; 245 } 246 catch (NullPointerException e) 247 { 248 continue; 250 } 251 252 if (msgID.equals(inReplyTo)) 253 { 254 resMsg = message; 255 break; 256 } 257 } 258 } 259 else if (log.isDebugEnabled()) 260 log.debug("getMailMessages(Session mail)\n\t no mail!"); 261 } 262 catch (NoSuchProviderException e) 263 { 264 log.error("NoSuchProviderException: getMailMessages(Session mail)\n", e); 265 throw new AxisFault("NoSuchProviderException", e); 266 } 267 catch (MessagingException e) 268 { 269 log.error("MessagingException: getMailMessages(Session mail)\n", e); 270 throw new AxisFault("MessagingException", e); 271 } 272 273 return resMsg; 274 } 275 276 private void closeFolder(Folder folder) 277 { 278 try 279 { 280 folder.close(true); 282 } 283 catch (Exception ignore) 284 { 285 } 286 return; 287 } 288 289 private void closeStore(Store store) 290 { 291 try 292 { 293 if (store != null) 294 store.close(); 295 } 296 catch (Exception ignore) 297 { 298 } 299 return; 300 } 301 302 private String getMessageID(MessageContext ctx) 303 { 304 String id = "no message-id"; 305 306 if (ctx.containsProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS)) 307 { 308 AddressingHeaders headers = (AddressingHeaders) ctx.getProperty(Constants.ENV_ADDRESSING_REQUEST_HEADERS); 309 MessageID msgid = headers.getMessageID(); 310 if (msgid != null) 311 { 312 id = msgid.toString(); 313 } 314 } 315 return id; 316 } 317 318 } 319 | Popular Tags |