001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.synapse.transport.mail;
021:
022: import org.apache.synapse.transport.AbstractTransportTest;
023: import org.apache.synapse.transport.base.BaseConstants;
024: import org.apache.synapse.transport.mail.UtilsMailServer;
025: import org.apache.synapse.transport.mail.MailTransportSender;
026: import org.apache.axis2.client.Options;
027: import org.apache.axis2.client.ServiceClient;
028: import org.apache.axis2.addressing.EndpointReference;
029: import org.apache.axis2.Constants;
030: import org.apache.axis2.description.TransportOutDescription;
031: import org.apache.axis2.description.Parameter;
032: import org.apache.axis2.engine.AxisConfiguration;
033: import org.apache.axis2.context.ConfigurationContext;
034: import org.apache.log4j.Logger;
035: import org.apache.log4j.Level;
036: import org.apache.log4j.BasicConfigurator;
037: import org.apache.axiom.om.util.UUIDGenerator;
038: import org.apache.axiom.om.util.StAXUtils;
039: import org.apache.axiom.om.*;
040: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
041: import org.apache.axiom.om.xpath.AXIOMXPath;
042: import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
043: import org.apache.axiom.soap.SOAPEnvelope;
044: import org.apache.axiom.soap.SOAP12Constants;
045: import org.apache.commons.logging.Log;
046: import org.apache.commons.logging.LogFactory;
047:
048: import javax.mail.*;
049: import javax.mail.internet.MimeMessage;
050: import javax.mail.internet.InternetAddress;
051: import javax.xml.stream.XMLStreamReader;
052: import javax.xml.namespace.QName;
053: import javax.activation.DataHandler;
054: import javax.activation.CommandMap;
055: import javax.activation.MailcapCommandMap;
056: import java.util.Properties;
057: import java.util.Arrays;
058: import java.util.Date;
059: import java.io.StringReader;
060:
061: public class MailEchoRawXMLTest extends AbstractTransportTest {
062:
063: private static final Log log = LogFactory
064: .getLog(MailEchoRawXMLTest.class);
065:
066: private static final String KOREAN_TEXT = "\uc5ec\ubcf4\uc138\uc694 \uc138\uacc4!";
067: private static final String KOREAN_CHARSET = "ISO-2022-KR";
068: private static final String POX_MESSAGE = "<my:echoOMElement xmlns:my=\"http://ws.apache.org/namespaces/axis2\">"
069: + "<my:myValue>omTextValue</my:myValue></my:echoOMElement>";
070:
071: private Properties props = new Properties();
072: private String username = "synapse.test.0";
073: private String password = "mailpassword";
074:
075: public MailEchoRawXMLTest() {
076: //Logger.getLogger("org.apache.synapse.transport.mail").setLevel(Level.TRACE);
077: server = new UtilsMailServer();
078:
079: props.put("mail.pop3.host", "pop.gmail.com");
080: props.put("mail.pop3.port", "995");
081: props.put("mail.pop3.user", "synapse.test.0");
082:
083: props.put("mail.pop3.socketFactory.class",
084: "javax.net.ssl.SSLSocketFactory");
085: props.put("mail.pop3.socketFactory.fallback", "false");
086: props.put("mail.pop3.socketFactory.port", "995");
087:
088: props.put("mail.smtp.host", "smtp.gmail.com");
089: props.put("mail.smtp.port", "587");
090: props.put("mail.smtp.starttls.enable", "true");
091: props.put("mail.smtp.user", "synapse.test.1");
092: props.put("mail.smtp.password", "mailpassword");
093: props.put("mail.smtp.auth", "true");
094: }
095:
096: public void testRoundTripPOX() throws Exception {
097:
098: String msgId = UUIDGenerator.getUUID();
099:
100: Session session = Session.getInstance(props,
101: new Authenticator() {
102: public PasswordAuthentication getPasswordAuthentication() {
103: return new PasswordAuthentication(
104: "synapse.test.1", "mailpassword");
105: }
106: });
107: session.setDebug(log.isTraceEnabled());
108:
109: WSMimeMessage msg = new WSMimeMessage(session);
110: msg.setFrom(new InternetAddress("synapse.test.0@gmail.com"));
111: msg.setReplyTo(InternetAddress
112: .parse("synapse.test.0@gmail.com"));
113: InternetAddress[] address = { new InternetAddress(
114: "synapse.test.6@gmail.com") };
115: msg.setRecipients(Message.RecipientType.TO, address);
116: msg.setSubject("POX Roundtrip");
117: msg.setHeader(BaseConstants.SOAPACTION,
118: Constants.AXIS2_NAMESPACE_URI + "/echoOMElement");
119: msg.setSentDate(new Date());
120: msg.setHeader(MailConstants.MAIL_HEADER_MESSAGE_ID, msgId);
121: msg.setHeader(MailConstants.MAIL_HEADER_X_MESSAGE_ID, msgId);
122: msg.setText(POX_MESSAGE);
123: Transport.send(msg);
124:
125: Thread.yield();
126: Thread.sleep(1000 * 10);
127:
128: Object reply = null;
129: boolean replyNotFound = true;
130: int retryCount = 3;
131: while (replyNotFound) {
132: log.debug("Checking for response ... with MessageID : "
133: + msgId);
134: reply = getMessage(msgId);
135: if (reply != null) {
136: replyNotFound = false;
137: } else {
138: if (retryCount-- > 0) {
139: Thread.sleep(10000);
140: } else {
141: break;
142: }
143: }
144: }
145:
146: if (reply != null && reply instanceof String) {
147: log.debug("Result Body : " + reply);
148: XMLStreamReader reader = StAXUtils
149: .createXMLStreamReader(new StringReader(
150: (String) reply));
151: OMElement res = new StAXOMBuilder(reader)
152: .getDocumentElement();
153: if (res != null) {
154: AXIOMXPath xpath = new AXIOMXPath("//my:myValue");
155: xpath
156: .addNamespace("my",
157: "http://localhost/axis2/services/EchoXMLService");
158: Object result = xpath.evaluate(res);
159: if (result != null && result instanceof OMElement) {
160: assertEquals("omTextValue", ((OMElement) result)
161: .getText());
162: }
163: }
164: } else {
165: fail("Did not receive the reply mail");
166: }
167: }
168:
169: public void testRoundTripMultiPart() throws Exception {
170:
171: String msgId = UUIDGenerator.getUUID();
172: Options options = new Options();
173: options.setTo(new EndpointReference(
174: "mailto:synapse.test.6@gmail.com"));
175: options.setReplyTo(new EndpointReference(
176: "mailto:synapse.test.0@gmail.com"));
177: options.setAction(Constants.AXIS2_NAMESPACE_URI
178: + "/echoOMElement");
179: options.setMessageId(msgId);
180:
181: options.setProperty(MailConstants.TRANSPORT_MAIL_FORMAT,
182: MailConstants.TRANSPORT_FORMAT_MP);
183:
184: ServiceClient sender = new ServiceClient(getClientCfgCtx(),
185: null);
186: sender.setOptions(options);
187: sender.fireAndForget(createPayload());
188:
189: Thread.yield();
190: Thread.sleep(1000 * 10);
191:
192: Object reply = null;
193: boolean replyNotFound = true;
194: int retryCount = 3;
195: while (replyNotFound) {
196: log.debug("Checking for response ... with MessageID : "
197: + msgId);
198: reply = getMessage(msgId);
199: if (reply != null) {
200: replyNotFound = false;
201: } else {
202: if (retryCount-- > 0) {
203: Thread.sleep(10000);
204: } else {
205: break;
206: }
207: }
208: }
209:
210: if (reply != null && reply instanceof String) {
211: log.debug("Result Body : " + reply);
212: XMLStreamReader reader = StAXUtils
213: .createXMLStreamReader(new StringReader(
214: (String) reply));
215: SOAPEnvelope env = new StAXSOAPModelBuilder(reader)
216: .getSOAPEnvelope();
217: if (env != null) {
218: AXIOMXPath xpath = new AXIOMXPath("//my:myValue");
219: xpath
220: .addNamespace("my",
221: "http://localhost/axis2/services/EchoXMLService");
222: Object result = xpath.evaluate(env);
223: if (result != null && result instanceof OMElement) {
224: assertEquals("omTextValue", ((OMElement) result)
225: .getText());
226: }
227: }
228: } else {
229: fail("Did not receive the reply mail");
230: }
231: }
232:
233: public void testRoundTripMultiPartKorean() throws Exception {
234:
235: String msgId = UUIDGenerator.getUUID();
236: Options options = new Options();
237: options.setTo(new EndpointReference(
238: "mailto:synapse.test.6@gmail.com"));
239: options.setReplyTo(new EndpointReference(
240: "mailto:synapse.test.0@gmail.com"));
241: options.setAction(Constants.AXIS2_NAMESPACE_URI
242: + "/echoOMElement");
243: options.setMessageId(msgId);
244:
245: options.setProperty(MailConstants.TRANSPORT_MAIL_FORMAT,
246: MailConstants.TRANSPORT_FORMAT_MP);
247:
248: ServiceClient sender = new ServiceClient(getClientCfgCtx(),
249: null);
250: sender.setOptions(options);
251: sender.fireAndForget(createKoreanPayload());
252:
253: Thread.yield();
254: Thread.sleep(1000 * 10);
255:
256: Object reply = null;
257: boolean replyNotFound = true;
258: int retryCount = 3;
259: while (replyNotFound) {
260: log.debug("Checking for response ... with MessageID : "
261: + msgId);
262: reply = getMessage(msgId);
263: if (reply != null) {
264: replyNotFound = false;
265: } else {
266: if (retryCount-- > 0) {
267: Thread.sleep(10000);
268: } else {
269: break;
270: }
271: }
272: }
273:
274: if (reply != null && reply instanceof String) {
275: log.debug("Result Body : " + reply);
276: XMLStreamReader reader = StAXUtils
277: .createXMLStreamReader(new StringReader(
278: (String) reply));
279: SOAPEnvelope env = new StAXSOAPModelBuilder(reader)
280: .getSOAPEnvelope();
281: if (env != null) {
282: AXIOMXPath xpath = new AXIOMXPath("//my:myValue");
283: xpath
284: .addNamespace("my",
285: "http://localhost/axis2/services/EchoXMLService");
286: Object result = xpath.evaluate(env);
287: if (result != null && result instanceof OMElement) {
288: assertEquals("omTextValue", ((OMElement) result)
289: .getText());
290: }
291: }
292: } else {
293: fail("Did not receive the reply mail");
294: }
295: }
296:
297: public void testRoundTripPOPDefaultCharsetSOAP12() throws Exception {
298:
299: String msgId = UUIDGenerator.getUUID();
300: Options options = new Options();
301: options.setTo(new EndpointReference(
302: "mailto:synapse.test.6@gmail.com"));
303: options.setReplyTo(new EndpointReference(
304: "mailto:synapse.test.0@gmail.com"));
305: options.setAction(Constants.AXIS2_NAMESPACE_URI
306: + "/echoOMElement");
307: options
308: .setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
309: options.setMessageId(msgId);
310:
311: ServiceClient sender = new ServiceClient(getClientCfgCtx(),
312: null);
313: sender.setOptions(options);
314: sender.fireAndForget(createPayload());
315:
316: Thread.yield();
317: Thread.sleep(1000 * 10);
318:
319: Object reply = null;
320: boolean replyNotFound = true;
321: int retryCount = 3;
322: while (replyNotFound) {
323: log.debug("Checking for response ... with MessageID : "
324: + msgId);
325: reply = getMessage(msgId);
326: if (reply != null) {
327: replyNotFound = false;
328: } else {
329: if (retryCount-- > 0) {
330: Thread.sleep(10000);
331: } else {
332: break;
333: }
334: }
335: }
336:
337: if (reply != null && reply instanceof String) {
338: log.debug("Result Body : " + reply);
339: XMLStreamReader reader = StAXUtils
340: .createXMLStreamReader(new StringReader(
341: (String) reply));
342: SOAPEnvelope env = new StAXSOAPModelBuilder(reader)
343: .getSOAPEnvelope();
344: if (env != null) {
345: AXIOMXPath xpath = new AXIOMXPath("//my:myValue");
346: xpath
347: .addNamespace("my",
348: "http://localhost/axis2/services/EchoXMLService");
349: Object result = xpath.evaluate(env);
350: if (result != null && result instanceof OMElement) {
351: assertEquals("omTextValue", ((OMElement) result)
352: .getText());
353: }
354: }
355: } else {
356: fail("Did not receive the reply mail");
357: }
358: }
359:
360: public void testRoundTripIMAPUTF8Charset() throws Exception {
361:
362: String msgId = UUIDGenerator.getUUID();
363: Options options = new Options();
364: options.setTo(new EndpointReference(
365: "mailto:synapse.test.7@gmail.com"));
366: options.setReplyTo(new EndpointReference(
367: "mailto:synapse.test.0@gmail.com"));
368: options.setAction(Constants.AXIS2_NAMESPACE_URI
369: + "/echoOMElement");
370: options.setMessageId(msgId);
371:
372: ServiceClient sender = new ServiceClient(getClientCfgCtx(),
373: null);
374: sender.setOptions(options);
375: sender.fireAndForget(createKoreanPayload());
376:
377: Thread.yield();
378: Thread.sleep(1000 * 10);
379:
380: Object reply = null;
381: boolean replyNotFound = true;
382: int retryCount = 3;
383: while (replyNotFound) {
384: log.debug("Checking for response ... with MessageID : "
385: + msgId);
386: reply = getMessage(msgId);
387: if (reply != null) {
388: replyNotFound = false;
389: } else {
390: if (retryCount-- > 0) {
391: Thread.sleep(10000);
392: } else {
393: break;
394: }
395: }
396: }
397:
398: if (reply != null && reply instanceof String) {
399: log.debug("Result Body : " + reply);
400: XMLStreamReader reader = StAXUtils
401: .createXMLStreamReader(new StringReader(
402: (String) reply));
403: SOAPEnvelope env = new StAXSOAPModelBuilder(reader)
404: .getSOAPEnvelope();
405: if (env != null) {
406: AXIOMXPath xpath = new AXIOMXPath("//my:myValue");
407: xpath
408: .addNamespace("my",
409: "http://localhost/axis2/services/EchoXMLService");
410: Object result = xpath.evaluate(env);
411: if (result != null && result instanceof OMElement) {
412: assertEquals(KOREAN_TEXT, ((OMElement) result)
413: .getText());
414: }
415: }
416: } else {
417: fail("Did not receive the reply mail");
418: }
419: }
420:
421: public void testRoundTripIMAPKoreanCharset() throws Exception {
422:
423: String msgId = UUIDGenerator.getUUID();
424: Options options = new Options();
425: options.setTo(new EndpointReference(
426: "mailto:synapse.test.7@gmail.com"));
427: options.setReplyTo(new EndpointReference(
428: "mailto:synapse.test.0@gmail.com"));
429: options.setAction(Constants.AXIS2_NAMESPACE_URI
430: + "/echoOMElement");
431: options.setMessageId(msgId);
432: options.setProperty(
433: Constants.Configuration.CHARACTER_SET_ENCODING,
434: KOREAN_CHARSET);
435:
436: ServiceClient sender = new ServiceClient(getClientCfgCtx(),
437: null);
438: sender.setOptions(options);
439: sender.fireAndForget(createKoreanPayload());
440:
441: Thread.yield();
442: Thread.sleep(1000 * 10);
443:
444: Object reply = null;
445: boolean replyNotFound = true;
446: int retryCount = 3;
447: while (replyNotFound) {
448: log.debug("Checking for response ... with MessageID : "
449: + msgId);
450: reply = getMessage(msgId);
451: if (reply != null) {
452: replyNotFound = false;
453: } else {
454: if (retryCount-- > 0) {
455: Thread.sleep(10000);
456: } else {
457: break;
458: }
459: }
460: }
461:
462: if (reply != null && reply instanceof String) {
463: log.debug("Result Body : " + reply);
464: XMLStreamReader reader = StAXUtils
465: .createXMLStreamReader(new StringReader(
466: (String) reply));
467: SOAPEnvelope env = new StAXSOAPModelBuilder(reader)
468: .getSOAPEnvelope();
469: if (env != null) {
470: AXIOMXPath xpath = new AXIOMXPath("//my:myValue");
471: xpath
472: .addNamespace("my",
473: "http://localhost/axis2/services/EchoXMLService");
474: Object result = xpath.evaluate(env);
475: if (result != null && result instanceof OMElement) {
476: assertEquals(KOREAN_TEXT, ((OMElement) result)
477: .getText());
478: }
479: }
480: } else {
481: fail("Did not receive the reply mail");
482: }
483: }
484:
485: private Object getMessage(String requestMsgId) {
486: Session session = Session.getInstance(props, null);
487: session.setDebug(log.isTraceEnabled());
488: Store store = null;
489:
490: try {
491: store = session.getStore("pop3");
492: store.connect(username, password);
493: Folder folder = store
494: .getFolder(MailConstants.DEFAULT_FOLDER);
495: folder.open(Folder.READ_WRITE);
496: Message[] msgs = folder.getMessages();
497: log.debug(msgs.length + " replies in reply mailbox");
498: for (Message m : msgs) {
499: String[] inReplyTo = m
500: .getHeader(MailConstants.MAIL_HEADER_IN_REPLY_TO);
501: log.debug("Got reply to : "
502: + Arrays.toString(inReplyTo));
503: if (inReplyTo != null && inReplyTo.length > 0) {
504: for (int j = 0; j < inReplyTo.length; j++) {
505: if (requestMsgId.equals(inReplyTo[j])) {
506: m.setFlag(Flags.Flag.DELETED, true);
507: return m.getContent();
508: }
509: }
510: }
511: m.setFlag(Flags.Flag.DELETED, true);
512: }
513: folder.close(true);
514: } catch (Exception e) {
515: e.printStackTrace();
516: } finally {
517: if (store != null) {
518: try {
519: store.close();
520: } catch (MessagingException ignore) {
521: }
522: store = null;
523: }
524: }
525: return null;
526: }
527:
528: /**
529: * Create a axis2 configuration context that 'knows' about the Mail transport
530: * @return
531: * @throws Exception
532: */
533: public ConfigurationContext getClientCfgCtx() throws Exception {
534:
535: AxisConfiguration axisCfg = new AxisConfiguration();
536: TransportOutDescription trpOutDesc = new TransportOutDescription(
537: "mailto");
538:
539: trpOutDesc.addParameter(new Parameter("mail.smtp.host",
540: "smtp.gmail.com"));
541: trpOutDesc.addParameter(new Parameter("mail.smtp.port", "587"));
542: trpOutDesc.addParameter(new Parameter(
543: "mail.smtp.starttls.enable", "true"));
544: trpOutDesc.addParameter(new Parameter("mail.smtp.user",
545: "synapse.test.1"));
546: trpOutDesc.addParameter(new Parameter("mail.smtp.password",
547: "mailpassword"));
548: trpOutDesc
549: .addParameter(new Parameter("mail.smtp.auth", "true"));
550:
551: MailTransportSender trpSender = new MailTransportSender();
552: trpOutDesc.setSender(trpSender);
553:
554: axisCfg.addTransportOut(trpOutDesc);
555: ConfigurationContext cfgCtx = new ConfigurationContext(axisCfg);
556:
557: trpSender.init(cfgCtx, trpOutDesc);
558: return cfgCtx;
559: }
560:
561: protected OMElement createKoreanPayload() {
562: OMFactory fac = OMAbstractFactory.getOMFactory();
563: OMNamespace omNs = fac.createOMNamespace(
564: "http://localhost/axis2/services/EchoXMLService", "my");
565: OMElement method = fac.createOMElement("echoOMElement", omNs);
566: OMElement value = fac.createOMElement("myValue", omNs);
567: value.addChild(fac.createOMText(value, KOREAN_TEXT));
568: method.addChild(value);
569: return method;
570: }
571: }
|