Mercurial Hosting > luan
comparison mail/src/luan/modules/mail/SmtpCon.java @ 646:cdc70de628b5
simplify LuanException
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 29 Mar 2016 19:58:39 -0600 |
parents | 60c549d43988 |
children | b21d82ee5756 |
comparison
equal
deleted
inserted
replaced
645:859c0dedc8b6 | 646:cdc70de628b5 |
---|---|
26 | 26 |
27 public SmtpCon(LuanState luan,LuanTable paramsTbl) throws LuanException { | 27 public SmtpCon(LuanState luan,LuanTable paramsTbl) throws LuanException { |
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(params,"host"); |
32 if( host==null ) | 32 if( host==null ) |
33 throw new LuanException(luan, "parameter 'host' is required" ); | 33 throw new LuanException( "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 new LuanException(luan, "parameter 'port' must be an integer" ); | 44 throw new LuanException( "parameter 'port' must be an integer" ); |
45 s = i.toString(); | 45 s = i.toString(); |
46 } else { | 46 } else { |
47 throw new LuanException(luan, "parameter 'port' must be an integer" ); | 47 throw new LuanException( "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 |
53 String username = getString(luan,params,"username"); | 53 String username = getString(params,"username"); |
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(params,"password"); |
58 if( password==null ) | 58 if( password==null ) |
59 throw new LuanException(luan, "parameter 'password' is required with 'username'" ); | 59 throw new LuanException( "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 new LuanException(luan, "unrecognized parameters: "+params ); | 71 throw new LuanException( "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(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 new LuanException(luan, "parameter '"+key+"' must be a string" ); | 77 throw new LuanException( "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 { |
83 try { | 83 try { |
84 Map<Object,Object> mailParams = new HashMap<Object,Object>(mailTbl.asMap(luan)); | 84 Map<Object,Object> mailParams = new HashMap<Object,Object>(mailTbl.asMap(luan)); |
85 MimeMessage msg = new MimeMessage(session); | 85 MimeMessage msg = new MimeMessage(session); |
86 | 86 |
87 String from = getString(luan,mailParams,"from"); | 87 String from = getString(mailParams,"from"); |
88 if( from != null ) | 88 if( from != null ) |
89 msg.setFrom(from); | 89 msg.setFrom(from); |
90 | 90 |
91 String to = getString(luan,mailParams,"to"); | 91 String to = getString(mailParams,"to"); |
92 if( to != null ) | 92 if( to != null ) |
93 msg.setRecipients(Message.RecipientType.TO,to); | 93 msg.setRecipients(Message.RecipientType.TO,to); |
94 | 94 |
95 String cc = getString(luan,mailParams,"cc"); | 95 String cc = getString(mailParams,"cc"); |
96 if( cc != null ) | 96 if( cc != null ) |
97 msg.setRecipients(Message.RecipientType.CC,cc); | 97 msg.setRecipients(Message.RecipientType.CC,cc); |
98 | 98 |
99 String subject = getString(luan,mailParams,"subject"); | 99 String subject = getString(mailParams,"subject"); |
100 if( subject != null ) | 100 if( subject != null ) |
101 msg.setSubject(subject); | 101 msg.setSubject(subject); |
102 | 102 |
103 Object body = mailParams.remove("body"); | 103 Object body = mailParams.remove("body"); |
104 Object attachments = mailParams.remove("attachments"); | 104 Object attachments = mailParams.remove("attachments"); |
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 new LuanException(luan, "invalid body types: " + map ); | 127 throw new LuanException( "invalid body types: " + map ); |
128 bodyPart.setContent(mp); | 128 bodyPart.setContent(mp); |
129 } else | 129 } else |
130 throw new LuanException(luan, "parameter 'body' must be a string or table" ); | 130 throw new LuanException( "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 new LuanException(luan, "parameter 'attachments' must be a table" ); | 135 throw new LuanException( "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 new LuanException(luan, "parameter 'attachments' must be a list" ); | 138 throw new LuanException( "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 new LuanException(luan, "each attachment must be a table" ); | 144 throw new LuanException( "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 new LuanException(luan, "an attachment is missing 'filename'" ); | 150 throw new LuanException( "an attachment is missing 'filename'" ); |
151 if( !(obj instanceof String) ) | 151 if( !(obj instanceof String) ) |
152 throw new LuanException(luan, "an attachment filename must be a string" ); | 152 throw new LuanException( "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 new LuanException(luan, "an attachment is missing 'content_type'" ); | 157 throw new LuanException( "an attachment is missing 'content_type'" ); |
158 if( !(obj instanceof String) ) | 158 if( !(obj instanceof String) ) |
159 throw new LuanException(luan, "an attachment content_type must be a string" ); | 159 throw new LuanException( "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 new LuanException(luan, "an attachment is missing 'content'" ); | 164 throw new LuanException( "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 new LuanException(luan, "unrecognized attachment parameters: "+attachmentMap ); | 169 throw new LuanException( "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 new LuanException(luan, "unrecognized parameters: "+mailParams ); | 180 throw new LuanException( "unrecognized parameters: "+mailParams ); |
181 | 181 |
182 Transport.send(msg); | 182 Transport.send(msg); |
183 } catch(MessagingException e) { | 183 } catch(MessagingException e) { |
184 throw new LuanException(luan,e); | 184 throw new LuanException(e); |
185 } | 185 } |
186 } | 186 } |
187 | 187 |
188 } | 188 } |