KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > transport > mail > MailTransportSender


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package org.apache.axis2.transport.mail;
18
19 import org.apache.axis2.addressing.EndpointReference;
20 import org.apache.axis2.context.MessageContext;
21 import org.apache.axis2.description.TransportOutDescription;
22 import org.apache.axis2.engine.AxisFault;
23 import org.apache.axis2.transport.AbstractTransportSender;
24 import org.apache.axis2.util.Utils;
25
26 import java.io.ByteArrayOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
30 public class MailTransportSender extends AbstractTransportSender {
31     private String JavaDoc host;
32     private String JavaDoc user;
33     private String JavaDoc password;
34     private String JavaDoc smtpPort = "25";
35
36     private ByteArrayOutputStream JavaDoc byteArrayOutputStream;
37
38     public MailTransportSender() {
39
40     }
41
42     public void finalizeSendWithToAddress(MessageContext msgContext,OutputStream JavaDoc out)
43         throws AxisFault {
44             try {
45                 TransportOutDescription transportOut = msgContext.getTransportOut();
46                 user = Utils.getParameterValue(transportOut.getParameter(MailConstants.SMTP_USER));
47                 host = Utils.getParameterValue(transportOut.getParameter(MailConstants.SMTP_HOST));
48                 password =
49                     Utils.getParameterValue(transportOut.getParameter(MailConstants.SMTP_PASSWORD));
50                 smtpPort = Utils.getParameterValue(transportOut.getParameter(MailConstants.SMTP_PORT));
51                 if (user != null && host != null && password != null && smtpPort != null) {
52                     EMailSender sender = new EMailSender(user, host, smtpPort, password);
53
54                     //TODO this is just a temporary hack, fix this to use input streams
55

56                 
57                     
58                 
59                     String JavaDoc eprAddress = msgContext.getTo().getAddress();
60                     int index = eprAddress.indexOf('/');
61                     String JavaDoc subject = "";
62                     String JavaDoc email = null;
63                     if(index >= 0){
64                         subject = eprAddress.substring(index+1);
65                         email = eprAddress.substring(0,index);
66                     }else{
67                         email = eprAddress;
68                     }
69                 
70                     System.out.println(subject);
71                     System.out.println(email);
72
73                     sender.send(subject, email,new String JavaDoc(byteArrayOutputStream.toByteArray()));
74                 } else {
75                     throw new AxisFault(
76                         "user, port, host or password not set, "
77                             + " [user null = "
78                             + (user == null)
79                             + ", password null= "
80                             + (password == null)
81                             + ", host null "
82                             + (host == null)
83                             + ",port null "
84                             + (smtpPort == null));
85
86                 }
87             } catch (IOException JavaDoc e) {
88                 throw new AxisFault(e);
89             }
90
91
92     }
93
94     public OutputStream JavaDoc startSendWithToAddress(MessageContext msgContext, OutputStream JavaDoc out) throws AxisFault {
95         return out;
96     }
97
98     protected OutputStream JavaDoc openTheConnection(EndpointReference epr,MessageContext msgContext) throws AxisFault {
99         byteArrayOutputStream = new ByteArrayOutputStream JavaDoc();
100             return byteArrayOutputStream;
101     }
102
103     //Output Stream based cases are not supported
104
public OutputStream JavaDoc startSendWithOutputStreamFromIncomingConnection(
105         MessageContext msgContext,
106     OutputStream JavaDoc out)
107         throws AxisFault {
108         throw new UnsupportedOperationException JavaDoc();
109
110     }
111     public void finalizeSendWithOutputStreamFromIncomingConnection(
112         MessageContext msgContext,OutputStream JavaDoc out)
113         throws AxisFault {
114     }
115     /* (non-Javadoc)
116      * @see org.apache.axis2.transport.TransportSender#cleanUp()
117      */

118     public void cleanUp(MessageContext msgContext) throws AxisFault {
119         // TODO Auto-generated method stub
120

121     }
122
123 }
124
Popular Tags