diff 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
line wrap: on
line diff
--- a/mail/src/luan/modules/mail/SmtpCon.java	Tue Mar 29 18:09:51 2016 -0600
+++ b/mail/src/luan/modules/mail/SmtpCon.java	Tue Mar 29 19:58:39 2016 -0600
@@ -28,9 +28,9 @@
 		Map<Object,Object> params = new HashMap<Object,Object>(paramsTbl.asMap(luan));
 		Properties props = new Properties(System.getProperties());
 
-		String host = getString(luan,params,"host");
+		String host = getString(params,"host");
 		if( host==null )
-			throw new LuanException(luan, "parameter 'host' is required" );
+			throw new LuanException( "parameter 'host' is required" );
 		props.setProperty("mail.smtp.host",host);
 
 		Object port = params.remove("port");
@@ -41,22 +41,22 @@
 			} else if( port instanceof Number ) {
 				Integer i = Luan.asInteger(port);
 				if( i == null )
-					throw new LuanException(luan, "parameter 'port' must be an integer" );
+					throw new LuanException( "parameter 'port' must be an integer" );
 				s = i.toString();
 			} else {
-				throw new LuanException(luan, "parameter 'port' must be an integer" );
+				throw new LuanException( "parameter 'port' must be an integer" );
 			}
 			props.setProperty("mail.smtp.socketFactory.port", s);
 			props.setProperty("mail.smtp.port", s);
 		}
 
-		String username = getString(luan,params,"username");
+		String username = getString(params,"username");
 		if( username == null ) {
 			session = Session.getInstance(props);
 		} else {
-			String password = getString(luan,params,"password");
+			String password = getString(params,"password");
 			if( password==null )
-				throw new LuanException(luan, "parameter 'password' is required with 'username'" );
+				throw new LuanException( "parameter 'password' is required with 'username'" );
 			props.setProperty("mail.smtp.auth","true");
 			final PasswordAuthentication pa = new PasswordAuthentication(username,password);
 			Authenticator auth = new Authenticator() {
@@ -68,13 +68,13 @@
 		}
 
 		if( !params.isEmpty() )
-			throw new LuanException(luan, "unrecognized parameters: "+params );
+			throw new LuanException( "unrecognized parameters: "+params );
 	}
 
-	private String getString(LuanState luan,Map<Object,Object> params,String key) throws LuanException {
+	private String getString(Map<Object,Object> params,String key) throws LuanException {
 		Object val = params.remove(key);
 		if( val!=null && !(val instanceof String) )
-			throw new LuanException(luan, "parameter '"+key+"' must be a string" );
+			throw new LuanException( "parameter '"+key+"' must be a string" );
 		return (String)val;
 	}
 
@@ -84,19 +84,19 @@
 			Map<Object,Object> mailParams = new HashMap<Object,Object>(mailTbl.asMap(luan));
 			MimeMessage msg = new MimeMessage(session);
 
-			String from = getString(luan,mailParams,"from");
+			String from = getString(mailParams,"from");
 			if( from != null )
 				msg.setFrom(from);
 
-			String to = getString(luan,mailParams,"to");
+			String to = getString(mailParams,"to");
 			if( to != null )
 				msg.setRecipients(Message.RecipientType.TO,to);
 
-			String cc = getString(luan,mailParams,"cc");
+			String cc = getString(mailParams,"cc");
 			if( cc != null )
 				msg.setRecipients(Message.RecipientType.CC,cc);
 
-			String subject = getString(luan,mailParams,"subject");
+			String subject = getString(mailParams,"subject");
 			if( subject != null )
 				msg.setSubject(subject);
 
@@ -124,49 +124,49 @@
 						mp.addBodyPart(part);
 					}
 					if( !map.isEmpty() )
-						throw new LuanException(luan, "invalid body types: " + map );
+						throw new LuanException( "invalid body types: " + map );
 					bodyPart.setContent(mp);
 				} else
-					throw new LuanException(luan, "parameter 'body' must be a string or table" );
+					throw new LuanException( "parameter 'body' must be a string or table" );
 			}
 
 			if( attachments != null ) {
 				if( !(attachments instanceof LuanTable) )
-					throw new LuanException(luan, "parameter 'attachments' must be a table" );
+					throw new LuanException( "parameter 'attachments' must be a table" );
 				LuanTable attachmentsTbl = (LuanTable)attachments;
 				if( !attachmentsTbl.isList() )
-					throw new LuanException(luan, "parameter 'attachments' must be a list" );
+					throw new LuanException( "parameter 'attachments' must be a list" );
 				MimeMultipart mp = new MimeMultipart("mixed");
 				if( body != null )
 					mp.addBodyPart((MimeBodyPart)bodyPart);
 				for( Object attachment : attachmentsTbl.asList() ) {
 					if( !(attachment instanceof LuanTable) )
-						throw new LuanException(luan, "each attachment must be a table" );
+						throw new LuanException( "each attachment must be a table" );
 					Map<Object,Object> attachmentMap = new HashMap<Object,Object>(((LuanTable)attachment).asMap(luan));
 					Object obj;
 
 					obj = attachmentMap.remove("filename");
 					if( obj==null )
-						throw new LuanException(luan, "an attachment is missing 'filename'" );
+						throw new LuanException( "an attachment is missing 'filename'" );
 					if( !(obj instanceof String) )
-						throw new LuanException(luan, "an attachment filename must be a string" );
+						throw new LuanException( "an attachment filename must be a string" );
 					String filename = (String)obj;
 
 					obj = attachmentMap.remove("content_type");
 					if( obj==null )
-						throw new LuanException(luan, "an attachment is missing 'content_type'" );
+						throw new LuanException( "an attachment is missing 'content_type'" );
 					if( !(obj instanceof String) )
-						throw new LuanException(luan, "an attachment content_type must be a string" );
+						throw new LuanException( "an attachment content_type must be a string" );
 					String content_type = (String)obj;
 
 					Object content = attachmentMap.remove("content");
 					if( content==null )
-						throw new LuanException(luan, "an attachment is missing 'content'" );
+						throw new LuanException( "an attachment is missing 'content'" );
 					if( content_type.startsWith("text/") && content instanceof byte[] )
 						content = new String((byte[])content);
 
 					if( !attachmentMap.isEmpty() )
-						throw new LuanException(luan, "unrecognized attachment parameters: "+attachmentMap );
+						throw new LuanException( "unrecognized attachment parameters: "+attachmentMap );
 
 					MimeBodyPart part = new MimeBodyPart();
 					part.setContent(content,content_type);
@@ -177,11 +177,11 @@
 			}
 
 			if( !mailParams.isEmpty() )
-				throw new LuanException(luan, "unrecognized parameters: "+mailParams );
+				throw new LuanException( "unrecognized parameters: "+mailParams );
 
 			Transport.send(msg);
 		} catch(MessagingException e) {
-			throw new LuanException(luan,e);
+			throw new LuanException(e);
 		}
 	}