comparison mail/src/luan/modules/mail/SmtpCon.java @ 578:60c549d43988

remove LuanState.exception()
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Jul 2015 17:40:48 -0600
parents 7c3ad6db8ac3
children cdc70de628b5
comparison
equal deleted inserted replaced
577:d7a85fbe15f1 578:60c549d43988
28 Map<Object,Object> params = new HashMap<Object,Object>(paramsTbl.asMap(luan)); 28 Map<Object,Object> params = new HashMap<Object,Object>(paramsTbl.asMap(luan));
29 Properties props = new Properties(System.getProperties()); 29 Properties props = new Properties(System.getProperties());
30 30
31 String host = getString(luan,params,"host"); 31 String host = getString(luan,params,"host");
32 if( host==null ) 32 if( host==null )
33 throw luan.exception( "parameter 'host' is required" ); 33 throw new LuanException(luan, "parameter 'host' is required" );
34 props.setProperty("mail.smtp.host",host); 34 props.setProperty("mail.smtp.host",host);
35 35
36 Object port = params.remove("port"); 36 Object port = params.remove("port");
37 if( port != null ) { 37 if( port != null ) {
38 String s; 38 String s;
39 if( port instanceof String ) { 39 if( port instanceof String ) {
40 s = (String)port; 40 s = (String)port;
41 } else if( port instanceof Number ) { 41 } else if( port instanceof Number ) {
42 Integer i = Luan.asInteger(port); 42 Integer i = Luan.asInteger(port);
43 if( i == null ) 43 if( i == null )
44 throw luan.exception( "parameter 'port' must be an integer" ); 44 throw new LuanException(luan, "parameter 'port' must be an integer" );
45 s = i.toString(); 45 s = i.toString();
46 } else { 46 } else {
47 throw luan.exception( "parameter 'port' must be an integer" ); 47 throw new LuanException(luan, "parameter 'port' must be an integer" );
48 } 48 }
49 props.setProperty("mail.smtp.socketFactory.port", s); 49 props.setProperty("mail.smtp.socketFactory.port", s);
50 props.setProperty("mail.smtp.port", s); 50 props.setProperty("mail.smtp.port", s);
51 } 51 }
52 52
54 if( username == null ) { 54 if( username == null ) {
55 session = Session.getInstance(props); 55 session = Session.getInstance(props);
56 } else { 56 } else {
57 String password = getString(luan,params,"password"); 57 String password = getString(luan,params,"password");
58 if( password==null ) 58 if( password==null )
59 throw luan.exception( "parameter 'password' is required with 'username'" ); 59 throw new LuanException(luan, "parameter 'password' is required with 'username'" );
60 props.setProperty("mail.smtp.auth","true"); 60 props.setProperty("mail.smtp.auth","true");
61 final PasswordAuthentication pa = new PasswordAuthentication(username,password); 61 final PasswordAuthentication pa = new PasswordAuthentication(username,password);
62 Authenticator auth = new Authenticator() { 62 Authenticator auth = new Authenticator() {
63 protected PasswordAuthentication getPasswordAuthentication() { 63 protected PasswordAuthentication getPasswordAuthentication() {
64 return pa; 64 return pa;
66 }; 66 };
67 session = Session.getInstance(props,auth); 67 session = Session.getInstance(props,auth);
68 } 68 }
69 69
70 if( !params.isEmpty() ) 70 if( !params.isEmpty() )
71 throw luan.exception( "unrecognized parameters: "+params ); 71 throw new LuanException(luan, "unrecognized parameters: "+params );
72 } 72 }
73 73
74 private String getString(LuanState luan,Map<Object,Object> params,String key) throws LuanException { 74 private String getString(LuanState luan,Map<Object,Object> params,String key) throws LuanException {
75 Object val = params.remove(key); 75 Object val = params.remove(key);
76 if( val!=null && !(val instanceof String) ) 76 if( val!=null && !(val instanceof String) )
77 throw luan.exception( "parameter '"+key+"' must be a string" ); 77 throw new LuanException(luan, "parameter '"+key+"' must be a string" );
78 return (String)val; 78 return (String)val;
79 } 79 }
80 80
81 81
82 public void send(LuanState luan,LuanTable mailTbl) throws LuanException { 82 public void send(LuanState luan,LuanTable mailTbl) throws LuanException {
122 MimeBodyPart part = new MimeBodyPart(); 122 MimeBodyPart part = new MimeBodyPart();
123 part.setContent(html,"text/html"); 123 part.setContent(html,"text/html");
124 mp.addBodyPart(part); 124 mp.addBodyPart(part);
125 } 125 }
126 if( !map.isEmpty() ) 126 if( !map.isEmpty() )
127 throw luan.exception( "invalid body types: " + map ); 127 throw new LuanException(luan, "invalid body types: " + map );
128 bodyPart.setContent(mp); 128 bodyPart.setContent(mp);
129 } else 129 } else
130 throw luan.exception( "parameter 'body' must be a string or table" ); 130 throw new LuanException(luan, "parameter 'body' must be a string or table" );
131 } 131 }
132 132
133 if( attachments != null ) { 133 if( attachments != null ) {
134 if( !(attachments instanceof LuanTable) ) 134 if( !(attachments instanceof LuanTable) )
135 throw luan.exception( "parameter 'attachments' must be a table" ); 135 throw new LuanException(luan, "parameter 'attachments' must be a table" );
136 LuanTable attachmentsTbl = (LuanTable)attachments; 136 LuanTable attachmentsTbl = (LuanTable)attachments;
137 if( !attachmentsTbl.isList() ) 137 if( !attachmentsTbl.isList() )
138 throw luan.exception( "parameter 'attachments' must be a list" ); 138 throw new LuanException(luan, "parameter 'attachments' must be a list" );
139 MimeMultipart mp = new MimeMultipart("mixed"); 139 MimeMultipart mp = new MimeMultipart("mixed");
140 if( body != null ) 140 if( body != null )
141 mp.addBodyPart((MimeBodyPart)bodyPart); 141 mp.addBodyPart((MimeBodyPart)bodyPart);
142 for( Object attachment : attachmentsTbl.asList() ) { 142 for( Object attachment : attachmentsTbl.asList() ) {
143 if( !(attachment instanceof LuanTable) ) 143 if( !(attachment instanceof LuanTable) )
144 throw luan.exception( "each attachment must be a table" ); 144 throw new LuanException(luan, "each attachment must be a table" );
145 Map<Object,Object> attachmentMap = new HashMap<Object,Object>(((LuanTable)attachment).asMap(luan)); 145 Map<Object,Object> attachmentMap = new HashMap<Object,Object>(((LuanTable)attachment).asMap(luan));
146 Object obj; 146 Object obj;
147 147
148 obj = attachmentMap.remove("filename"); 148 obj = attachmentMap.remove("filename");
149 if( obj==null ) 149 if( obj==null )
150 throw luan.exception( "an attachment is missing 'filename'" ); 150 throw new LuanException(luan, "an attachment is missing 'filename'" );
151 if( !(obj instanceof String) ) 151 if( !(obj instanceof String) )
152 throw luan.exception( "an attachment filename must be a string" ); 152 throw new LuanException(luan, "an attachment filename must be a string" );
153 String filename = (String)obj; 153 String filename = (String)obj;
154 154
155 obj = attachmentMap.remove("content_type"); 155 obj = attachmentMap.remove("content_type");
156 if( obj==null ) 156 if( obj==null )
157 throw luan.exception( "an attachment is missing 'content_type'" ); 157 throw new LuanException(luan, "an attachment is missing 'content_type'" );
158 if( !(obj instanceof String) ) 158 if( !(obj instanceof String) )
159 throw luan.exception( "an attachment content_type must be a string" ); 159 throw new LuanException(luan, "an attachment content_type must be a string" );
160 String content_type = (String)obj; 160 String content_type = (String)obj;
161 161
162 Object content = attachmentMap.remove("content"); 162 Object content = attachmentMap.remove("content");
163 if( content==null ) 163 if( content==null )
164 throw luan.exception( "an attachment is missing 'content'" ); 164 throw new LuanException(luan, "an attachment is missing 'content'" );
165 if( content_type.startsWith("text/") && content instanceof byte[] ) 165 if( content_type.startsWith("text/") && content instanceof byte[] )
166 content = new String((byte[])content); 166 content = new String((byte[])content);
167 167
168 if( !attachmentMap.isEmpty() ) 168 if( !attachmentMap.isEmpty() )
169 throw luan.exception( "unrecognized attachment parameters: "+attachmentMap ); 169 throw new LuanException(luan, "unrecognized attachment parameters: "+attachmentMap );
170 170
171 MimeBodyPart part = new MimeBodyPart(); 171 MimeBodyPart part = new MimeBodyPart();
172 part.setContent(content,content_type); 172 part.setContent(content,content_type);
173 part.setFileName(filename); 173 part.setFileName(filename);
174 mp.addBodyPart(part); 174 mp.addBodyPart(part);
175 } 175 }
176 msg.setContent(mp); 176 msg.setContent(mp);
177 } 177 }
178 178
179 if( !mailParams.isEmpty() ) 179 if( !mailParams.isEmpty() )
180 throw luan.exception( "unrecognized parameters: "+mailParams ); 180 throw new LuanException(luan, "unrecognized parameters: "+mailParams );
181 181
182 Transport.send(msg); 182 Transport.send(msg);
183 } catch(MessagingException e) { 183 } catch(MessagingException e) {
184 throw luan.exception(e); 184 throw new LuanException(luan,e);
185 } 185 }
186 } 186 }
187 187
188 } 188 }