class AsteriskServerImpl {
public AsteriskChannel originate(OriginateAction originateAction) throws ManagerCommunicationException,
NoSuchChannelException
{
final ResponseEvents responseEvents;
final Iterator<ResponseEvent> responseEventIterator;
String uniqueId;
AsteriskChannel channel = null;
// must set async to true to receive OriginateEvents.
originateAction.setAsync(Boolean.TRUE);
initializeIfNeeded();
// 2000 ms extra for the OriginateFailureEvent should be fine
responseEvents = sendEventGeneratingAction(originateAction, originateAction.getTimeout() + 2000);
responseEventIterator = responseEvents.getEvents().iterator();
if (responseEventIterator.hasNext())
{
ResponseEvent responseEvent;
responseEvent = responseEventIterator.next();
if (responseEvent instanceof OriginateResponseEvent)
{
uniqueId = ((OriginateResponseEvent) responseEvent).getUniqueId();
logger.debug(responseEvent.getClass().getName() + " received with uniqueId " + uniqueId);
channel = getChannelById(uniqueId);
}
}
if (channel == null)
{
throw new NoSuchChannelException("Channel '" + originateAction.getChannel() + "' is not available");
}
return channel;
}
}
class AsteriskServerImpl {
public Collection<Voicemailbox> getVoicemailboxes() throws ManagerCommunicationException
{
final Collection<Voicemailbox> voicemailboxes;
ManagerResponse response;
final List<String> result;
initializeIfNeeded();
voicemailboxes = new ArrayList<Voicemailbox>();
if (eventConnection.getVersion().isAtLeast(AsteriskVersion.ASTERISK_1_6))
{
response = sendAction(new CommandAction(SHOW_VOICEMAIL_USERS_1_6_COMMAND));
}
else
{
response = sendAction(new CommandAction(SHOW_VOICEMAIL_USERS_COMMAND));
}
if (!(response instanceof CommandResponse))
{
logger.error("Response to CommandAction(\"" + SHOW_VOICEMAIL_USERS_COMMAND
+ "\") was not a CommandResponse but " + response);
return voicemailboxes;
}
result = ((CommandResponse) response).getResult();
if (result == null || result.size() < 1)
{
return voicemailboxes;
}
// remove headline
result.remove(0);
for (String line : result)
{
final Matcher matcher;
final Voicemailbox voicemailbox;
final String context;
final String mailbox;
final String user;
matcher = SHOW_VOICEMAIL_USERS_PATTERN.matcher(line);
if (!matcher.find())
{
continue;
}
context = matcher.group(1);
mailbox = matcher.group(2);
user = matcher.group(3).trim();
voicemailbox = new Voicemailbox(mailbox, context, user);
voicemailboxes.add(voicemailbox);
}
// get message count for each mailbox
for (Voicemailbox voicemailbox : voicemailboxes)
{
final String fullname;
fullname = voicemailbox.getMailbox() + "@" + voicemailbox.getContext();
response = sendAction(new MailboxCountAction(fullname));
if (response instanceof MailboxCountResponse)
{
MailboxCountResponse mailboxCountResponse;
mailboxCountResponse = (MailboxCountResponse) response;
voicemailbox.setNewMessages(mailboxCountResponse.getNewMessages());
voicemailbox.setOldMessages(mailboxCountResponse.getOldMessages());
}
else
{
logger.error("Response to MailboxCountAction was not a MailboxCountResponse but " + response);
}
}
return voicemailboxes;
}
}
|