/*
* This file is part of the Java SIP library, an open source SIP library. Please see
* http://collaboration.mitre.org/sip for more information
*
* Copyright (c) 2001 The MITRE Corporation
*
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
* License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA.
*
*/
/**
* Defines the action when a response is received for a call
*/
package org.mitre.sipchat.model.presence;
import java.awt.AWTEvent;
import java.util.Date;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import org.mitre.jsip.*;
import org.mitre.sipchat.model.Info;
public class PresenceEvent extends AWTEvent {
private SipMessage message;
public static final int NEXT_ID = AWTEvent.RESERVED_ID_MAX + 4;
public static final int SUBSCRIBE_ID = NEXT_ID;
public static final int NOTIFY_ID = NEXT_ID + 1;
public PresenceEvent( Object source, SipMessage message, int id ) {
super( message, id );
this.message = message;
}
/**
* Get the message that triggered this event
*
* @return the current SIP message
*/
public SipMessage getMessage() {
return message;
}
public String toString() {
return getClass().getName() + "[message=\n" + message.message() + "]";
}
public long getExpiration() {
String expires = message.getHeaderData( SipHeader.Expires );
if ( expires == null ) return 0;
Date exDate = null;
try {
exDate = DateFormat.getDateTimeInstance().parse( expires );
} catch ( ParseException pe ) {
return 0;
}
return exDate.getTime();
}
/**
* return an Info record about this message
*/
public Info getInfo() {
Presentity fromID = new Presentity( message.getHeaderData( SipHeader.From ) );
PresenceDocument doc = null;
try {
doc = new PresenceDocument( getMessage().messageBody() );
} catch ( IOException ioe ) {
// hmm, error in the message
System.err.println( "Error getting presence document: " + ioe );
return null;
}
int code = doc.getUserStatus();
// Hmm, not really the best place for this, but don't know of another
// option right now. We need to add aliases for the user that sent
// this message for things to happen correctly (depending on the name
// used when generating the request that in turn generated this event)
// We'll use the Contact: list as the aliases, and add them to the
// static table in StringPresentity
StringPresentity sp = StringPresentity.fromPresentity( fromID );
String[] contacts = getContacts();
for ( int i = 0; i < contacts.length; i++ ) {
StringPresentity.addAlias( fromID, new Presentity( contacts[i] ) );
}
// TODO: get the dates right
Info userInfo = new Info( fromID, PresenceAgent.statusMessage( code ),
new Date(), new Date(), 0 );
return userInfo;
}
/**
* Get the contact list for this message
* also referred to as aliases
*/
public String[] getContacts() {
return message.getContactList().getUriListArray();
}
}