[jboss-cvs] jboss-mail/src/java/org/jboss/mail/maillistener ...

Aron Sogor bigman at gmail.com
Thu Jul 13 23:52:18 EDT 2006


  User: asogor  
  Date: 06/07/13 23:52:18

  Modified:    src/java/org/jboss/mail/maillistener  
                        CalendarMailListener.java
                        CalendarMailListenerMBean.java
  Log:
  Start adding PLAN and SCHEDULE mail alias support
  
  Revision  Changes    Path
  1.5       +331 -129  jboss-mail/src/java/org/jboss/mail/maillistener/CalendarMailListener.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CalendarMailListener.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-mail/src/java/org/jboss/mail/maillistener/CalendarMailListener.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- CalendarMailListener.java	6 Jul 2006 23:11:43 -0000	1.4
  +++ CalendarMailListener.java	14 Jul 2006 03:52:17 -0000	1.5
  @@ -21,6 +21,14 @@
    */
   package org.jboss.mail.maillistener;
   
  +import java.text.DateFormat;
  +import java.text.ParseException;
  +import java.text.SimpleDateFormat;
  +import java.util.Calendar;
  +import java.util.Date;
  +import java.util.HashMap;
  +import java.util.Map;
  +
   import javax.jms.ConnectionFactory;
   import javax.jms.ObjectMessage;
   import javax.jms.Queue;
  @@ -41,31 +49,41 @@
   import org.jboss.mail.MailException;
   import org.jboss.mail.MailListener;
   import org.jboss.mail.calendar.data.InviteStatus;
  -import org.jboss.mail.calendar.eventbus.MailResponse;
  +import org.jboss.mail.calendar.eventbus.mail.MailRequest;
  +import org.jboss.mail.calendar.eventbus.mail.PlanEvent;
  +import org.jboss.mail.calendar.eventbus.mail.ScheduleEvent;
  +import org.jboss.mail.calendar.eventbus.mail.StatusChange;
   import org.jboss.mail.message.Mail;
   import org.jboss.mail.message.MailAddress;
   import org.jboss.mail.message.Message;
   import org.jboss.system.ServiceMBeanSupport;
   
   /**
  - * This is the calendar mail listener.
  - * It accepts incomming mail responses for invitations.
  + * This is the calendar mail listener. It accepts incomming mail responses for
  + * invitations.
    * 
    * @author Aron Sogor
    */
  -public class CalendarMailListener extends ServiceMBeanSupport implements CalendarMailListenerMBean, MailListener{
  +public class CalendarMailListener extends ServiceMBeanSupport implements
  +		CalendarMailListenerMBean, MailListener {
  +
  +	private static final String KEY_END_DATE = "end";
  +
  +	private static final String KEY_INCEREMENT_CNT = "icnt";
  +
  +	private static final String KEY_INCEREMENT_SIZE = "isize";
  +
  +	private static final String KEY_START_DATE = "start";
  +
  +	private static final Logger log = Logger
  +			.getLogger(CalendarMailListener.class);
   	
  -	private static final Logger log = Logger.getLogger(CalendarMailListener.class);
   	private String calendarUser = "calendar";
  -    /**
  -     * queue or topic
  -     */
  -    private String destinationType;
   
       /**
  -     * destination queue/topic name
  +	 * CF instance from JNDI
        */
  -    private String destination;
  +	private ConnectionFactory cf;
   
       /**
        * connection factory name
  @@ -73,59 +91,237 @@
       private String connectionFactoryName;
       
       /**
  -     * CF instance from JNDI 
  +	 * dateformat that used to parsed request datefields 
        */
  -    private ConnectionFactory cf;
  +	private DateFormat dateFormat = new SimpleDateFormat();
  +
  +	/**
  +	 * default End Hour calucluated by start date + default duration * hrs for
  +	 * planing
  +	 */
  +	private int defaultDuration;
   
       /**
  -     * destination queue (null unless destinationType="queue")
  +	 * default End Hour for planing
        */
  -    private Queue destinationQueue;
  +	private int defaultIncrementCnt;
  +
  +	/**
  +	 * default increment size for planing
  +	 */
  +	private int defaultIncrementSize;
  +
  +	/**
  +	 * default Start Hour for planing
  +	 */
  +	private int defaultStartHour;
  +
  +	/**
  +	 * destination queue/topic name
  +	 */
  +	private String destination;
   
       /**
  -     * destination topic (null unless destinationType="topic")
  +	 * queue or topic
        */
  -    private Topic destinationTopic;
  +	private String destinationType;
  +
  +	private String[] createInvites(Mail mail, MailAddress invite) {
  +		MailAddress[] invites = mail.getTo();
  +		String[] results = new String[invites.length - 1];
  +		int cnt = 0;
  +		for (int idx = 0; idx < invites.length; idx++) {
  +			if (!invites[idx].equals(invite)) {
  +				results[cnt] = invites[idx].getRawAddress();
  +				cnt++;
  +			}
  +			mail.removeTo(invites[idx]);
  +		}
  +		return results;
  +	}
  +
  +	public String getCalendarUser() {
  +		return calendarUser;
  +	}
  +
  +	public String getConnectionFactoryName() {
  +		return connectionFactoryName;
  +	}
  +
  +	public String getDateFormat() {
  +		return this.dateFormat.toString();
  +	}
  +
  +	public int getDefaultDuration() {
  +		return defaultDuration;
  +	}
  +
  +	public int getDefaultIncrementCnt() {
  +		return defaultIncrementCnt;
  +	}
  +
  +	public int getDefaultIncrementSize() {
  +		return defaultIncrementSize;
  +	}
  +
  +	public int getDefaultStartHour() {
  +		return defaultStartHour;
  +	}
  +
  +	public String getDestination() {
  +		return destination;
  +	}
  +
  +	public String getDestinationType() {
  +		return destinationType;
  +	}
  +
  +	/**
  +	 * Generate a Calendar meeting plan request. The format of the plan request
  +	 * is:
  +	 * PLAN.startdate=x.enddate=y.incrementSize=1.incermentCount=2.calendar at host
  +	 * startdate,enddate,incrementSize,incrementCount are all optional. If not
  +	 * supplied the following defaults are used: startDate = today0.00 +
  +	 * defaultStartHour endDate = startDate + defaultDurationInHour
  +	 * incrementSize = defaultIncrementSize incrementCount =
  +	 * defaultIncrementCount
  +	 * Invite list is generated based on the To field.
  +	 * 
  +	 * @param mail
  +	 * @param address
  +	 * @param acnt
  +	 * @param params
  +	 */
  +	protected void handlePlanRequest(Mail mail, MailAddress[] address,
  +			int acnt, String[] params) {
  +		Map paramMap = parseParams(params, 1);
  +		Date start, end;
  +		Calendar cal = Calendar.getInstance();
  +		cal.set(Calendar.HOUR_OF_DAY, defaultStartHour);
  +		cal.set(Calendar.MINUTE, 0);
  +		cal.set(Calendar.SECOND, 0);
  +		cal.set(Calendar.MILLISECOND, 0);
  +
  +		int incrementSize, incrementCnt;
  +		try {
  +			start = parseDate(KEY_START_DATE, paramMap, cal.getTime());
  +			end = parseDate(KEY_END_DATE, paramMap, new Date(start.getTime()
  +					+ (defaultDuration * 3600000)));
  +			incrementSize = parseInt(KEY_INCEREMENT_SIZE, paramMap,
  +					defaultIncrementSize);
  +			incrementCnt = parseInt(KEY_INCEREMENT_CNT, paramMap,
  +					defaultIncrementCnt);
  +			PlanEvent request = new PlanEvent(address[acnt].getRawAddress(),
  +					createInvites(mail, address[acnt]), start, end,
  +					incrementSize, incrementCnt);
  +			handleResponse(request);
  +		} catch (ParseException e) {
  +			handleRequestSyntaxError(mail, e);
  +		}
  +		mail.removeTo(address[acnt]);
  +	}
  +
  +	public Message handleRequestSyntaxError(Mail mail, Exception ex) {
  +		// TODO: we need something more elegant here.
  +		// Figure out how to send back the request to sender with error message
  +		log.error("Request syntax error", ex);
  +		return null;
  +	}
  +
  +	public void handleResponse(MailRequest response) {
  +		try {
  +			if (destinationType.toLowerCase().equals("topic"))
  +				this.sendMessageTopic(response);
  +			if (destinationType.toLowerCase().equals("queue"))
  +				this.sendMessageQueue(response);
  +		} catch (Exception ex) {
  +			log.error("Failed to deliver response to Calendar Server", ex);
  +		}
  +	}
  +
  +	/**
  +	 * Create a schedule request. Format:
  +	 * SCHEDULE.startdate=x.enddate=y.calendar at host
  +	 * Invite list is generated based on the To field. 
  +	 * 
  +	 * @param mail
  +	 * @param address
  +	 * @param acnt
  +	 * @param params
  +	 */
  +	protected void handleScheduleRequest(Mail mail, MailAddress[] address,
  +			int acnt, String[] params) {
  +		Map paramMap = parseParams(params, 1);
  +		Date start, end;
  +		try {
  +			start = parseDate(KEY_START_DATE, paramMap, null);
  +			end = parseDate(KEY_END_DATE, paramMap, null);
  +			ScheduleEvent request = new ScheduleEvent(address[acnt]
  +					.getRawAddress(), createInvites(mail, address[acnt]),
  +					start, end);
  +			handleResponse(request);
  +		} catch (ParseException e) {
  +			handleRequestSyntaxError(mail, e);
  +		}
  +
  +		mail.removeTo(address[acnt]);
  +	}
  +
  +	public Date parseDate(String key, Map map, Date dflt) throws ParseException {
  +		String value = (String) map.get(key);
  +		if (value == null)
  +			return dflt;
  +		return dateFormat.parse(value);
  +	}
  +
  +	public int parseInt(String key, Map map, int dflt) {
  +		String value = (String) map.get(key);
  +		if (value == null)
  +			return dflt;
  +		return Integer.parseInt(value);
  +	}
  +
  +	private Map parseParams(String[] params, int startIdx) {
  +		Map<String, String> result = new HashMap<String, String>();
  +		for (int idx = startIdx; idx < params.length; idx++) {
  +			String[] pair = params[idx].split("=");
  +			result.put(pair[0], pair[1]);
  +		}
  +		return result;
  +	}
       	
   	public Message send(Message msg) throws MailException {
  -		if(msg != null)
  -		{
  +		if (msg != null) {
   			Mail mail = (Mail) msg;
   			log.info("Process Calendar mail:" + msg);
   			MailAddress[] address = mail.getTo();
   			for (int acnt = 0; acnt < address.length; acnt++) {
  -				if(address[acnt].getUser().endsWith(calendarUser))
  -				{
  -                    String[] params = address[acnt].getUser().replaceAll("." + calendarUser,"").split("\\.");
  +				if (address[acnt].getUser().endsWith(calendarUser)) {
  +					String[] params = address[acnt].getUser().replaceAll(
  +							"." + calendarUser, "").split("\\.");
                       log.info("Do: " + params[0]);
  -                     if(params[0].toUpperCase().equals("ACCEPT"))
  -                     {
  -                         handleResponse(new MailResponse(mail.getFrom().getRawAddress(),InviteStatus.ACCEPTED,params[1]));
  +					if (params[0].toUpperCase().equals("ACCEPT")) {
  +						handleResponse(new StatusChange(mail.getFrom()
  +								.getRawAddress(), InviteStatus.ACCEPTED,
  +								params[1]));
                            mail.removeTo(address[acnt]);
                        }
  -                     if(params[0].toUpperCase().equals("DECLINE"))
  -                     {
  -                         handleResponse(new MailResponse(mail.getFrom().getRawAddress(),InviteStatus.DECLINED,params[1]));
  +					if (params[0].toUpperCase().equals("DECLINE")) {
  +						handleResponse(new StatusChange(mail.getFrom()
  +								.getRawAddress(), InviteStatus.DECLINED,
  +								params[1]));
                            mail.removeTo(address[acnt]);
                        }           				
  +					if (params[0].toUpperCase().equals("PLAN")) {
  +						handlePlanRequest(mail, address, acnt, params);
   				}
  +					if (params[0].toUpperCase().equals("SCHEDULE")) {
  +						handleScheduleRequest(mail, address, acnt, params);
   			}
   		}
  -		return msg;
  -	}
  -	
  -	public void handleResponse(MailResponse response)
  -	{
  -        try{
  -        if(destinationType.toLowerCase().equals("topic"))
  -            this.sendMessageTopic(response);
  -        if(destinationType.toLowerCase().equals("queue"))
  -            this.sendMessageQueue(response);
           }
  -        catch (Exception ex)
  -        {
  -            log.error("Failed to deliver response to Calendar Server",ex);
           }
  +		return msg;
   	}
   
       /**
  @@ -137,10 +333,11 @@
        *             if there is any problem (JMS related or otherwise)
        */
       @Tx(TxType.REQUIRED)
  -    private void sendMessageQueue(MailResponse msg) throws Exception {
  +	private void sendMessageQueue(MailRequest msg) throws Exception {
           InitialContext ctx = new InitialContext();
  -        QueueConnectionFactory cf = (QueueConnectionFactory)ctx.lookup(connectionFactoryName);
  -        Queue queue = (Queue)ctx.lookup("queue/"+destination);
  +		QueueConnectionFactory cf = (QueueConnectionFactory) ctx
  +				.lookup(connectionFactoryName);
  +		Queue queue = (Queue) ctx.lookup("queue/" + destination);
           QueueConnection qc = cf.createQueueConnection();
           QueueSession qs = qc.createQueueSession(true, 0);
   
  @@ -163,12 +360,13 @@
        *             if there is any problem (JMS related or otherwise)
        */
       @Tx(TxType.REQUIRED)
  -    private void sendMessageTopic(MailResponse msg) throws Exception {
  +	private void sendMessageTopic(MailRequest msg) throws Exception {
           InitialContext ctx = new InitialContext();
  -        TopicConnectionFactory cf = (TopicConnectionFactory)ctx.lookup(connectionFactoryName);
  +		TopicConnectionFactory cf = (TopicConnectionFactory) ctx
  +				.lookup(connectionFactoryName);
           TopicConnection tc = cf.createTopicConnection();
           TopicSession ts = tc.createTopicSession(true, 0);
  -        Topic topic = (Topic)ctx.lookup("topic/"+destination);
  +		Topic topic = (Topic) ctx.lookup("topic/" + destination);
           try {
               TopicPublisher pub = ts.createPublisher(topic);
               ObjectMessage om = ts.createObjectMessage(msg);
  @@ -178,32 +376,36 @@
           }
       }
       
  -	public String getCalendarUser() {
  -		return calendarUser;
  -	}
  -
   	public void setCalendarUser(String calendarUser) {
   		this.calendarUser = calendarUser;
   	}
   
  -	public String getConnectionFactoryName() {
  -		return connectionFactoryName;
  -	}
  -
   	public void setConnectionFactoryName(String connectionFactoryName) {
   		this.connectionFactoryName = connectionFactoryName;
   	}
   
  -	public String getDestination() {
  -		return destination;
  +	public void setDateFormat(String dateFormat) {
  +		this.dateFormat = new SimpleDateFormat(dateFormat);
   	}
   
  -	public void setDestination(String destination) {
  -		this.destination = destination;
  +	public void setDefaultDuration(int defaultDuration) {
  +		this.defaultDuration = defaultDuration;
   	}
   
  -	public String getDestinationType() {
  -		return destinationType;
  +	public void setDefaultIncrementCnt(int defaultIncrementCnt) {
  +		this.defaultIncrementCnt = defaultIncrementCnt;
  +	}
  +
  +	public void setDefaultIncrementSize(int defaultIncrementSize) {
  +		this.defaultIncrementSize = defaultIncrementSize;
  +	}
  +
  +	public void setDefaultStartHour(int defaultStartHour) {
  +		this.defaultStartHour = defaultStartHour;
  +	}
  +
  +	public void setDestination(String destination) {
  +		this.destination = destination;
   	}
   
   	public void setDestinationType(String destType) {
  
  
  
  1.3       +26 -4     jboss-mail/src/java/org/jboss/mail/maillistener/CalendarMailListenerMBean.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: CalendarMailListenerMBean.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-mail/src/java/org/jboss/mail/maillistener/CalendarMailListenerMBean.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -r1.2 -r1.3
  --- CalendarMailListenerMBean.java	4 Jul 2006 19:44:30 -0000	1.2
  +++ CalendarMailListenerMBean.java	14 Jul 2006 03:52:17 -0000	1.3
  @@ -21,6 +21,8 @@
    */
   package org.jboss.mail.maillistener;
   
  +import java.text.DateFormat;
  +
   import org.jboss.mail.MailException;
   import org.jboss.mail.message.Message;
   import org.jboss.system.ServiceMBean;
  @@ -31,7 +33,7 @@
    * @author Aron Sogor
    *
    */
  -public interface CalendarMailListenerMBean extends ServiceMBean{
  +public interface CalendarMailListenerMBean extends ServiceMBean {
   
   	public String getCalendarUser();
   
  @@ -49,6 +51,26 @@
   
   	public void setDestinationType(String destType);
       
  +	public void setDateFormat(String dateFormat);
  +
  +	public void setDefaultDuration(int defaultDuration);
  +
  +	public void setDefaultIncrementCnt(int defaultIncrementCnt);
  +
  +	public void setDefaultIncrementSize(int defaultIncrementSize);
  +
  +	public void setDefaultStartHour(int defaultStartHour);
  +
  +	public String getDateFormat();
  +
  +	public int getDefaultDuration();
  +
  +	public int getDefaultIncrementCnt();
  +
  +	public int getDefaultIncrementSize();
  +
  +	public int getDefaultStartHour();
  +
       Message send(Message msg) throws MailException;
   
   }
  
  
  



More information about the jboss-cvs-commits mailing list