[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/manager ResponseBuilder.java,NONE,1.
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-01 19:25:59
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26211/src/java/net/sf/asterisk/manager Modified Files: ManagerReader.java Added Files: ResponseBuilder.java Log Message: Extracted response building into a separte class --- NEW FILE: ResponseBuilder.java --- /* * Copyright 2004-2005 Stefan Reuter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package net.sf.asterisk.manager; import java.util.Map; import net.sf.asterisk.manager.response.ChallengeResponse; import net.sf.asterisk.manager.response.ExtensionStateResponse; import net.sf.asterisk.manager.response.MailboxCountResponse; import net.sf.asterisk.manager.response.MailboxStatusResponse; import net.sf.asterisk.manager.response.ManagerError; import net.sf.asterisk.manager.response.ManagerResponse; /** * Transforms maps of attributes to instances of ManagerResponse. * * @see net.sf.asterisk.manager.response.ManagerResponse * @author srt * @version $Id: ResponseBuilder.java,v 1.1 2005/03/01 19:25:35 srt Exp $ */ public class ResponseBuilder { /** * Constructs an instance of ManagerResponse based on a map of attributes. * * @param attributes the attributes and their values. The keys of this map must be all lower * case. * @return the response with the given attributes. */ public ManagerResponse buildResponse(final Map attributes) { ManagerResponse response; String responseType; responseType = (String) attributes.get("response"); // determine type if ("error".equalsIgnoreCase(responseType)) { response = new ManagerError(); } else if (attributes.containsKey("challenge")) { ChallengeResponse challengeResponse = new ChallengeResponse(); challengeResponse.setChallenge((String) attributes.get("challenge")); response = challengeResponse; } else if (attributes.containsKey("mailbox") && attributes.containsKey("waiting")) { MailboxStatusResponse mailboxStatusResponse = new MailboxStatusResponse(); mailboxStatusResponse.setMailbox((String) attributes.get("mailbox")); if ("1".equals((String) attributes.get("waiting"))) { mailboxStatusResponse.setWaiting(Boolean.TRUE); } else { mailboxStatusResponse.setWaiting(Boolean.FALSE); } response = mailboxStatusResponse; } else if (attributes.containsKey("mailbox") && attributes.containsKey("newmessages") && attributes.containsKey("oldmessages")) { MailboxCountResponse mailboxCountResponse = new MailboxCountResponse(); mailboxCountResponse.setMailbox((String) attributes.get("mailbox")); mailboxCountResponse.setNewMessages(new Integer((String) attributes.get("newmessages"))); mailboxCountResponse.setOldMessages(new Integer((String) attributes.get("oldmessages"))); response = mailboxCountResponse; } else if (attributes.containsKey("exten") && attributes.containsKey("context") && attributes.containsKey("hint") && attributes.containsKey("status")) { ExtensionStateResponse extensionStateResponse = new ExtensionStateResponse(); extensionStateResponse.setExten((String) attributes.get("exten")); extensionStateResponse.setContext((String) attributes.get("context")); extensionStateResponse.setHint((String) attributes.get("hint")); extensionStateResponse.setStatus(new Integer((String) attributes.get("status"))); response = extensionStateResponse; } else { response = new ManagerResponse(); } // fill known attributes response.setResponse(responseType); if (attributes.containsKey("actionid")) { response.setActionId((String) attributes.get("actionid")); } if (attributes.containsKey("message")) { response.setMessage((String) attributes.get("message")); } if (attributes.containsKey("uniqueid")) { response.setUniqueId((String) attributes.get("uniqueid")); } return response; } } Index: ManagerReader.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/manager/ManagerReader.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -p -r1.3 -r1.4 --- ManagerReader.java 28 Feb 2005 16:45:44 -0000 1.3 +++ ManagerReader.java 1 Mar 2005 19:25:35 -0000 1.4 @@ -25,12 +25,7 @@ import java.util.List; import java.util.Map; import net.sf.asterisk.manager.event.ManagerEvent; -import net.sf.asterisk.manager.response.ChallengeResponse; import net.sf.asterisk.manager.response.CommandResponse; -import net.sf.asterisk.manager.response.ExtensionStateResponse; -import net.sf.asterisk.manager.response.MailboxCountResponse; -import net.sf.asterisk.manager.response.MailboxStatusResponse; -import net.sf.asterisk.manager.response.ManagerError; import net.sf.asterisk.manager.response.ManagerResponse; import org.apache.commons.logging.Log; @@ -54,13 +49,19 @@ class ManagerReader extends Thread private Log log = LogFactory.getLog(getClass()); private static int threadCount = 0; - + /** - * The event builder utility to convert a map of attributes reveived from asterisk - * to instances of registered event classes. + * The event builder utility to convert a map of attributes reveived from asterisk to instances + * of registered event classes. */ private EventBuilder eventBuilder; - + + /** + * The response builder utility to convert a map of attributes reveived from asterisk to instances + * of well known response classes. + */ + private ResponseBuilder responseBuilder; + /** * If set to <code>true</code>, terminates and closes the reader. */ @@ -77,6 +78,7 @@ class ManagerReader extends Thread this.reader = reader; this.eventBuilder = new EventBuilder(); + this.responseBuilder = new ResponseBuilder(); } /** @@ -120,7 +122,6 @@ class ManagerReader extends Thread commandResponse.setDateReceived(new Date()); commandResponse.setResult(commandResult); connection.dispatchResponse(commandResponse); - commandResult = new ArrayList(); processingCommandResult = false; } else @@ -129,9 +130,14 @@ class ManagerReader extends Thread } continue; } + + // Reponse: Follows indicates that the output starting on the next line until + // --END COMMAND-- must be treated as raw output of a command executed by a + // CommandAction. if ("Response: Follows".equalsIgnoreCase(line)) { processingCommandResult = true; + commandResult.clear(); continue; } @@ -183,6 +189,7 @@ class ManagerReader extends Thread } } } + connection.handleDisconnection(); } catch (IOException e) @@ -211,68 +218,14 @@ class ManagerReader extends Thread private ManagerResponse buildResponse(Map buffer) { ManagerResponse response; - String responseType = (String) buffer.get("response"); - // determine type - if ("error".equals(responseType)) - { - response = new ManagerError(); - } - else if (buffer.containsKey("challenge")) - { - ChallengeResponse challengeResponse = new ChallengeResponse(); - challengeResponse.setChallenge((String) buffer.get("challenge")); - response = challengeResponse; - } - else if (buffer.containsKey("mailbox") && buffer.containsKey("waiting")) - { - MailboxStatusResponse mailboxStatusResponse = new MailboxStatusResponse(); - mailboxStatusResponse.setMailbox((String) buffer.get("mailbox")); - mailboxStatusResponse.setWaiting(new Integer((String) buffer.get("mailbox"))); - response = mailboxStatusResponse; - } - else if (buffer.containsKey("mailbox") && buffer.containsKey("newmessages") - && buffer.containsKey("oldmessages")) - { - MailboxCountResponse mailboxCountResponse = new MailboxCountResponse(); - mailboxCountResponse.setMailbox((String) buffer.get("mailbox")); - mailboxCountResponse.setNewMessages(new Integer((String) buffer.get("newmessages"))); - mailboxCountResponse.setOldMessages(new Integer((String) buffer.get("oldmessages"))); - response = mailboxCountResponse; - } - else if (buffer.containsKey("exten") && buffer.containsKey("context") && buffer.containsKey("hint") - && buffer.containsKey("status")) - { - ExtensionStateResponse extensionStateResponse = new ExtensionStateResponse(); - extensionStateResponse.setExten((String) buffer.get("exten")); - extensionStateResponse.setContext((String) buffer.get("context")); - extensionStateResponse.setHint((String) buffer.get("hint")); - extensionStateResponse.setStatus(new Integer((String) buffer.get("status"))); - response = extensionStateResponse; - } - else - { - response = new ManagerResponse(); - } - - // fill known attributes - response.setResponse(responseType); + response = responseBuilder.buildResponse(buffer); - if (buffer.containsKey("actionid")) - { - response.setActionId((String) buffer.get("actionid")); - } - if (buffer.containsKey("message")) - { - response.setMessage((String) buffer.get("message")); - } - if (buffer.containsKey("uniqueid")) + if (response != null) { - response.setUniqueId((String) buffer.get("uniqueid")); + response.setDateReceived(new Date()); } - response.setDateReceived(new Date()); - return response; } @@ -280,7 +233,7 @@ class ManagerReader extends Thread { ManagerEvent event; - event = this.eventBuilder.buildEvent(source, buffer); + event = eventBuilder.buildEvent(source, buffer); if (event != null) { |