[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/fastagi/impl AGIReaderImpl.java,1.1,
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-04-15 04:47:11
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/impl In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16598/src/java/net/sf/asterisk/fastagi/impl Modified Files: AGIReaderImpl.java AGIRequestImpl.java Removed Files: RequestBuilderImpl.java ReplyBuilderImpl.java Log Message: - Moved funcitonality of ReplyBuilder and RequestBuilder to AGIReplyImpl and AGIRequestImpl (thanks to Steve Drach for providing this patch) - Removed ReplyBuilde and RequestBuilder - Refactored Reply into interface and implementation Index: AGIReaderImpl.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/impl/AGIReaderImpl.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -p -r1.1 -r1.2 --- AGIReaderImpl.java 11 Mar 2005 15:20:50 -0000 1.1 +++ AGIReaderImpl.java 15 Apr 2005 04:47:00 -0000 1.2 @@ -25,9 +25,8 @@ import net.sf.asterisk.fastagi.AGIHangup import net.sf.asterisk.fastagi.AGINetworkException; import net.sf.asterisk.fastagi.AGIReader; import net.sf.asterisk.fastagi.AGIRequest; -import net.sf.asterisk.fastagi.ReplyBuilder; -import net.sf.asterisk.fastagi.RequestBuilder; import net.sf.asterisk.fastagi.reply.AGIReply; +import net.sf.asterisk.fastagi.reply.impl.AGIReplyImpl; import net.sf.asterisk.io.SocketConnectionFacade; /** @@ -39,14 +38,10 @@ import net.sf.asterisk.io.SocketConnecti public class AGIReaderImpl implements AGIReader { private SocketConnectionFacade socket; - private ReplyBuilder replyBuilder; - private RequestBuilder requestBuilder; public AGIReaderImpl(SocketConnectionFacade socket) { this.socket = socket; - this.replyBuilder = new ReplyBuilderImpl(); - this.requestBuilder = new RequestBuilderImpl(); } public AGIRequest readRequest() throws AGIException @@ -76,7 +71,7 @@ public class AGIReaderImpl implements AG e); } - request = requestBuilder.buildRequest(lines); + request = new AGIRequestImpl(lines); return request; } @@ -130,7 +125,7 @@ public class AGIReaderImpl implements AG } } - reply = replyBuilder.buildReply(lines); + reply = new AGIReplyImpl(lines); return reply; } Index: AGIRequestImpl.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/impl/AGIRequestImpl.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -p -r1.3 -r1.4 --- AGIRequestImpl.java 6 Apr 2005 18:58:59 -0000 1.3 +++ AGIRequestImpl.java 15 Apr 2005 04:47:00 -0000 1.4 @@ -17,8 +17,16 @@ package net.sf.asterisk.fastagi.impl; import java.io.Serializable; +import java.net.URLDecoder; +import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.Iterator; +import java.util.List; import java.util.Map; +import java.util.StringTokenizer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import net.sf.asterisk.fastagi.AGIRequest; @@ -30,93 +38,103 @@ import net.sf.asterisk.fastagi.AGIReques */ public class AGIRequestImpl implements Serializable, AGIRequest { - /** - * Serial version identifier. - */ - private static final long serialVersionUID = 3257001047145789496L; + private static final Pattern SCRIPT_PATTERN = Pattern + .compile("^([^\\?]*)\\?(.*)$"); + private static final Pattern PARAMETER_PATTERN = Pattern + .compile("^(.*)=(.*)$"); - /** - * The name of the script to execute. - */ - private String script; + private String rawCallerId; /** - * The full URL of the request in the form agi://host[:port][/script] + * Serial version identifier. */ - private String requestURL; + private static final long serialVersionUID = 3257001047145789496L; - /** - * The name of the channel. - */ - private String channel; + private Map request; /** - * The unique id of the channel. + * A map assigning the values of a parameter (an array of Strings) to the + * name of the parameter. */ - private String uniqueId; + private Map parameterMap; /** - * The type of the channel, for example "SIP". + * Creates a new AGIRequestImpl. */ - private String type; + public AGIRequestImpl() + { + throw new IllegalArgumentException("Environment must not be null."); + } - /** - * The language, for example "en". - */ - private String language; + public AGIRequestImpl(final Collection lines) + { + if (lines == null) + { + throw new IllegalArgumentException("Environment must not be null."); + } + request = buildMap(lines); + } /** - * The Caller*ID, for example "1234". + * Builds a map containing variable names as key (with the "agi_" prefix + * stripped) and the corresponding values.<br> + * Syntactically invalid and empty variables are skipped. + * + * @param lines the environment to transform. + * @return a map with the variables set corresponding to the given + * environment. */ - private String callerId; + private Map buildMap(final Collection lines) + { + Map map; + Iterator lineIterator; - /** - * The Caller*ID Name, for example "John Doe". - */ - private String callerIdName; + map = new HashMap(); + lineIterator = lines.iterator(); - private String dnid; + while (lineIterator.hasNext()) + { + String line; + int colonPosition; + String key; + String value; - private String rdnis; + line = (String) lineIterator.next(); + colonPosition = line.indexOf(':'); - /** - * The context in the dial plan from which the AGI script was called. - */ - private String context; + // no colon on the line? + if (colonPosition < 0) + { + continue; + } - /** - * The extension in the dial plan from which the AGI script was called. - */ - private String extension; + // key doesn't start with agi_? + if (!line.startsWith("agi_")) + { + continue; + } - /** - * The priority in the dial plan from which the AGI script was called. - */ - private Integer priority; + // first colon in line is last character -> no value present? + if (line.length() < colonPosition + 2) + { + continue; + } - /** - * Boolean.TRUE if this agi is passed audio (EAGI - Enhanced AGI).<br> - * Enhanced AGI is currently not supported on FastAGI. - */ - private Boolean enhanced; + key = line.substring(4, colonPosition).toLowerCase(); + value = line.substring(colonPosition + 2); - /** - * The account code set for the call. - */ - private String accountCode; + if (value.length() != 0) + { + map.put(key, value); + } + } - /** - * A map assigning the values of a parameter (an array of Strings) to the - * name of the parameter. - */ - private Map parameterMap; + return map; + } - /** - * Creates a new AGIRequestImpl. - */ - public AGIRequestImpl() + public Map getRequest() { - + return request; } /** @@ -126,20 +144,22 @@ public class AGIRequestImpl implements S */ public String getScript() { + if (script != null) + return script; + script = (String) request.get("network_script"); + if (script != null) + { + Matcher scriptMatcher = SCRIPT_PATTERN.matcher(script); + if (scriptMatcher.matches()) + { + script = scriptMatcher.group(1); + parameters = scriptMatcher.group(2); + } + } return script; } /** - * Sets the name of the script to execute. - * - * @param script the name of the script to execute. - */ - public void setScript(String script) - { - this.script = script; - } - - /** * Returns the full URL of the request in the form * agi://host[:port][/script]. * @@ -148,18 +168,7 @@ public class AGIRequestImpl implements S */ public String getRequestURL() { - return requestURL; - } - - /** - * Sets the full URL of the request in the form agi://host[:port][/script]. - * - * @param requestURL the full URL of the request in the form - * agi://host[:port][/script]. - */ - public void setRequestURL(String requestURL) - { - this.requestURL = requestURL; + return (String) request.get("request"); } /** @@ -169,17 +178,7 @@ public class AGIRequestImpl implements S */ public String getChannel() { - return channel; - } - - /** - * Sets the name of the channel. - * - * @param channel the name of the channel. - */ - public void setChannel(String channel) - { - this.channel = channel; + return (String) request.get("channel"); } /** @@ -189,17 +188,7 @@ public class AGIRequestImpl implements S */ public String getUniqueId() { - return uniqueId; - } - - /** - * Sets the unqiue id of the channel. - * - * @param uniqueId the unqiue id of the channel. - */ - public void setUniqueId(String uniqueId) - { - this.uniqueId = uniqueId; + return (String) request.get("uniqueid"); } /** @@ -209,17 +198,7 @@ public class AGIRequestImpl implements S */ public String getType() { - return type; - } - - /** - * Sets the type of the channel, for example "SIP". - * - * @param type the type of the channel, for example "SIP". - */ - public void setType(String type) - { - this.type = type; + return (String) request.get("type"); } /** @@ -229,77 +208,85 @@ public class AGIRequestImpl implements S */ public String getLanguage() { - return language; + return (String) request.get("language"); } - /** - * Sets the language, for example "en". - * - * @param language the language, for example "en". - */ - public void setLanguage(String language) - { - this.language = language; - } + private boolean callerIdCreated; - /** - * Returns the Caller*ID, for example "1234". - * - * @return the Caller*ID, for example "1234". - */ public String getCallerId() { - return callerId; - } + int lbPosition; + int rbPosition; - /** - * Sets the Caller*ID, for example "1234". - * - * @param callerId the Caller*ID, for example "1234". - */ - public void setCallerId(String callerId) - { - this.callerId = callerId; + if (!callerIdCreated) + { + rawCallerId = (String) request.get("callerid"); + callerIdCreated = true; + } + + if (rawCallerId == null) + { + return null; + } + + lbPosition = rawCallerId.indexOf('<'); + rbPosition = rawCallerId.indexOf('>'); + + if (lbPosition < 0 || rbPosition < 0) + { + return rawCallerId; + } + + return rawCallerId.substring(lbPosition + 1, rbPosition); } - /** - * Returns the the Caller*ID Name, for example "John Doe". - * - * @return the the Caller*ID Name, for example "John Doe". - */ public String getCallerIdName() { - return callerIdName; - } + int lbPosition; + String callerIdName; - /** - * Sets the the Caller*ID Name, for example "John Doe". - * - * @param callerIdName the the Caller*ID Name, for example "John Doe". - */ - public void setCallerIdName(String callerIdName) - { - this.callerIdName = callerIdName; - } + if (!callerIdCreated) + { + rawCallerId = (String) request.get("callerid"); + callerIdCreated = true; + } + + if (rawCallerId == null) + { + return null; + } - public String getDnid() - { - return dnid; - } + lbPosition = rawCallerId.indexOf('<'); - public void setDnid(String dnid) - { - this.dnid = dnid; + if (lbPosition < 0) + { + return null; + } + + callerIdName = rawCallerId.substring(0, lbPosition).trim(); + if (callerIdName.startsWith("\"") && callerIdName.endsWith("\"")) + { + callerIdName = callerIdName.substring(1, callerIdName.length() - 1); + } + + if (callerIdName.length() == 0) + { + return null; + } + else + { + return callerIdName; + } } - public String getRdnis() + public String getDnid() { - return rdnis; + return (String) request.get("dnid"); } - public void setRdnis(String rdnis) + public String getRdnis() { - this.rdnis = rdnis; + return (String) request.get("rdnis"); } /** @@ -311,18 +298,7 @@ public class AGIRequestImpl implements S */ public String getContext() { - return context; - } - - /** - * Sets the context in the dial plan from which the AGI script was called. - * - * @param context the context in the dial plan from which the AGI script was - * called. - */ - public void setContext(String context) - { - this.context = context; + return (String) request.get("context"); } /** @@ -334,18 +310,7 @@ public class AGIRequestImpl implements S */ public String getExtension() { - return extension; - } - - /** - * Sets the extension in the dial plan from which the AGI script was called. - * - * @param extension the extension in the dial plan from which the AGI script - * was called. - */ - public void setExtension(String extension) - { - this.extension = extension; + return (String) request.get("extension"); } /** @@ -357,18 +322,11 @@ public class AGIRequestImpl implements S */ public Integer getPriority() { - return priority; - } - - /** - * Sets the priority in the dial plan from which the AGI script was called. - * - * @param priority the priority in the dial plan from which the AGI script - * was called. - */ - public void setPriority(Integer priority) - { - this.priority = priority; + if (request.get("priority") != null) + { + return new Integer((String) request.get("priority")); + } + return null; } /** @@ -380,18 +338,18 @@ public class AGIRequestImpl implements S */ public Boolean getEnhanced() { - return enhanced; - } - - /** - * Sets wheather this agi is passed audio. - * - * @param enhanced Boolean.TRUE if this agi is passed audio, Boolean.FALSE - * otherwise. - */ - public void setEnhanced(Boolean enhanced) - { - this.enhanced = enhanced; + if (request.get("enhanced") != null) + { + if ("1.0".equals((String) request.get("enhanced"))) + { + return Boolean.TRUE; + } + else + { + return Boolean.FALSE; + } + } + return null; } /** @@ -401,17 +359,7 @@ public class AGIRequestImpl implements S */ public String getAccountCode() { - return accountCode; - } - - /** - * Sets the account code set for the call. - * - * @param accountCode the account code set for the call. - */ - public void setAccountCode(String accountCode) - { - this.accountCode = accountCode; + return (String) request.get("accountCode"); } public String getParameter(String name) @@ -430,7 +378,7 @@ public class AGIRequestImpl implements S public String[] getParameterValues(String name) { - if (parameterMap == null) + if (getParameterMap().isEmpty()) { return null; } @@ -442,15 +390,78 @@ public class AGIRequestImpl implements S { if (parameterMap == null) { - parameterMap = new HashMap(); + parameterMap = parseParameters(parameters); } - return parameterMap; } - - public void setParameterMap(Map parameterMap) + + private String parameters; + private String script; + + private Map parseParameters(String s) { - this.parameterMap = parameterMap; + Map parameterMap; + Map result; + Iterator parameterIterator; + StringTokenizer st; + + parameterMap = new HashMap(); + if (s == null) + { + return parameterMap; + } + + st = new StringTokenizer(s, "&"); + while (st.hasMoreTokens()) + { + String parameter; + Matcher parameterMatcher; + String name; + String value; + List values; + + parameter = st.nextToken(); + parameterMatcher = PARAMETER_PATTERN.matcher(parameter); + if (parameterMatcher.matches()) + { + name = URLDecoder.decode(parameterMatcher.group(1)); + value = URLDecoder.decode(parameterMatcher.group(2)); + } + else + { + name = URLDecoder.decode(parameter); + value = ""; + } + + if (parameterMap.get(name) == null) + { + values = new ArrayList(); + values.add(value); + parameterMap.put(name, values); + } + else + { + values = (List) parameterMap.get(name); + values.add(value); + } + } + + result = new HashMap(); + parameterIterator = parameterMap.keySet().iterator(); + while (parameterIterator.hasNext()) + { + String name; + List values; + String[] valueArray; + + name = (String) parameterIterator.next(); + values = (List) parameterMap.get(name); + + valueArray = new String[values.size()]; + result.put(name, values.toArray(valueArray)); + } + + return result; } public String toString() --- RequestBuilderImpl.java DELETED --- --- ReplyBuilderImpl.java DELETED --- |