[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/fastagi AGIRequestBuilderImpl.java,N
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-05 22:47:12
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24114/src/java/net/sf/asterisk/fastagi Added Files: AGIRequestBuilderImpl.java AGIRequestBuilder.java AGIRequestImpl.java AGIRequest.java Log Message: Added initial stuff for fastagi support --- NEW FILE: AGIRequestBuilderImpl.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.fastagi; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * Default implementation of the AGIRequestBuilder interface. * * @author srt * @version $Id: AGIRequestBuilderImpl.java,v 1.1 2005/03/05 22:46:59 srt Exp $ */ public class AGIRequestBuilderImpl implements AGIRequestBuilder { /** * Creates a new AGIRequestBuilderImpl. */ public AGIRequestBuilderImpl() { } public AGIRequest buildAGIRequest(final Collection lines) { AGIRequestImpl request; Map map; if (lines == null) { throw new IllegalArgumentException("Environment must not be null."); } request = new AGIRequestImpl(); map = buildMap(lines); request.setScript((String) map.get("network_script")); request.setRequestURL((String) map.get("request")); request.setChannel((String) map.get("channel")); request.setUniqueId((String) map.get("uniqueid")); request.setType((String) map.get("type")); request.setLanguage((String) map.get("language")); if (map.get("callerid") != null) { String rawCallerId = (String) map.get("callerid"); request.setCallerId(getCallerId(rawCallerId)); request.setCallerIdName(getCallerIdName(rawCallerId)); } request.setDnid((String) map.get("dnid")); request.setRdnis((String) map.get("rdnis")); request.setContext((String) map.get("context")); request.setExtension((String) map.get("extension")); if (map.get("priority") != null) { request.setPriority(new Integer((String) map.get("priority"))); } if (map.get("enhanced") != null) { if ("1.0".equals((String) map.get("enhanced"))) { request.setEnhanced(Boolean.TRUE); } else { request.setEnhanced(Boolean.FALSE); } } request.setAccountCode((String) map.get("accountcode")); return request; } /** * 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 Map buildMap(final Collection lines) { Map map; Iterator lineIterator; map = new HashMap(); lineIterator = lines.iterator(); while (lineIterator.hasNext()) { String line; int colonPosition; String key; String value; line = (String) lineIterator.next(); colonPosition = line.indexOf(':'); // no colon on the line? if (colonPosition < 0) { continue; } // key doesn't start with agi_? if (!line.startsWith("agi_")) { continue; } // first colon in line is last character -> no value present? if (line.length() < colonPosition + 2) { continue; } key = line.substring(4, colonPosition).toLowerCase(); value = line.substring(colonPosition + 2); if (value.length() != 0) { map.put(key, value); } } return map; } private String getCallerId(final String rawCallerId) { int lbPosition; int rbPosition; lbPosition = rawCallerId.indexOf('<'); rbPosition = rawCallerId.indexOf('>'); if (lbPosition < 0 || rbPosition < 0) { return rawCallerId; } return rawCallerId.substring(lbPosition + 1, rbPosition); } private String getCallerIdName(final String rawCallerId) { int lbPosition; String callerIdName; lbPosition = rawCallerId.indexOf('<'); 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; } } } --- NEW FILE: AGIRequestBuilder.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.fastagi; import java.util.Collection; /** * Parses the environment printed by asterisk's <code>setup_env</code> function to an AGIRequest. * * @author srt * @version $Id: AGIRequestBuilder.java,v 1.1 2005/03/05 22:46:59 srt Exp $ */ public interface AGIRequestBuilder { /** * Parses the environment printed by asterisk's <code>setup_env</code> function to an * AGIRequest. * * @param lines the environment printed by asterisk's <code>setup_env</code> function. * @return an AGIRequest setup according to the environment passed in. */ AGIRequest buildAGIRequest(Collection lines); } --- NEW FILE: AGIRequestImpl.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.fastagi; import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; /** * Default implementation of the AGIRequest interface. * * @author srt * @version $Id: AGIRequestImpl.java,v 1.1 2005/03/05 22:46:59 srt Exp $ */ public class AGIRequestImpl implements Serializable, AGIRequest { /** * Serial version identifier. */ private static final long serialVersionUID = 3257001047145789496L; /** * The name of the script to execute. */ private String script; /** * The full URL of the request in the form agi://host[:port][/script] */ private String requestURL; /** * The name of the channel. */ private String channel; /** * The unique id of the channel. */ private String uniqueId; /** * The type of the channel, for example "SIP". */ private String type; /** * The language, for example "en". */ private String language; /** * The Caller*ID, for example "1234". */ private String callerId; /** * The Caller*ID Name, for example "John Doe". */ private String callerIdName; private String dnid; private String rdnis; /** * The context in the dial plan from which the AGI script was called. */ private String context; /** * The extension in the dial plan from which the AGI script was called. */ private String extension; /** * The priority in the dial plan from which the AGI script was called. */ private Integer priority; /** * Boolean.TRUE if this agi is passed audio (EAGI - Enhanced AGI).<br> * Enhanced AGI is currently not supported on FastAGI. */ private Boolean enhanced; /** * The account code set for the call. */ private String accountCode; /** * Creates a new AGIRequestImpl. */ public AGIRequestImpl() { } /** * Returns the name of the script to execute. * * @return the name of the script to execute. */ public String getScript() { 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]. * * @return the full URL of the request in the form agi://host[:port][/script]. */ 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; } /** * Returns the name of the channel. * * @return the name of the channel. */ 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; } /** * Returns the unqiue id of the channel. * * @return the unqiue id of the channel. */ 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; } /** * Returns the type of the channel, for example "SIP". * * @return the type of the channel, for example "SIP". */ 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; } /** * Returns the language, for example "en". * * @return the language, for example "en". */ public String getLanguage() { return language; } /** * Sets the language, for example "en". * * @param language the language, for example "en". */ public void setLanguage(String language) { this.language = language; } /** * Returns the Caller*ID, for example "1234". * * @return the Caller*ID, for example "1234". */ public String getCallerId() { return callerId; } /** * Sets the Caller*ID, for example "1234". * * @param callerId the Caller*ID, for example "1234". */ public void setCallerId(String callerId) { this.callerId = callerId; } /** * 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; } /** * 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; } public String getDnid() { return dnid; } public void setDnid(String dnid) { this.dnid = dnid; } public String getRdnis() { return rdnis; } public void setRdnis(String rdnis) { this.rdnis = rdnis; } /** * Returns the context in the dial plan from which the AGI script was called. * * @return the context in the dial plan from which the AGI script was called. */ 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; } /** * Returns the extension in the dial plan from which the AGI script was called. * * @return the extension in the dial plan from which the AGI script was called. */ 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; } /** * Returns the priority in the dial plan from which the AGI script was called. * * @return the priority in the dial plan from which the AGI script was called. */ 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; } /** * Returns wheather this agi is passed audio (EAGI - Enhanced AGI).<br> * Enhanced AGI is currently not supported on FastAGI. * * @return Boolean.TRUE if this agi is passed audio, Boolean.FALSE otherwise. */ 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; } /** * Returns the account code set for the call. * * @return the account code set for the call. */ 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; } public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } public boolean equals(Object o) { return EqualsBuilder.reflectionEquals(this, o); } public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } } --- NEW FILE: AGIRequest.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.fastagi; /** * * @author srt * @version $Id: AGIRequest.java,v 1.1 2005/03/05 22:46:59 srt Exp $ */ public interface AGIRequest { /** * Returns the name of the script to execute. * * @return the name of the script to execute. */ String getScript(); /** * Returns the full URL of the requestURL in the form agi://host[:port][/script]. * * @return the full URL of the requestURL in the form agi://host[:port][/script]. */ String getRequestURL(); /** * Returns the name of the channel. * * @return the name of the channel. */ String getChannel(); /** * Returns the unqiue id of the channel. * * @return the unqiue id of the channel. */ String getUniqueId(); /** * Returns the type of the channel, for example "SIP". * * @return the type of the channel, for example "SIP". */ String getType(); /** * Returns the language, for example "en". * * @return the language, for example "en". */ String getLanguage(); /** * Returns the Caller*ID, for example "1234". * * @return the Caller*ID, for example "1234". */ String getCallerId(); /** * Returns the the Caller*ID Name, for example "John Doe". * * @return the the Caller*ID Name, for example "John Doe". */ String getCallerIdName(); String getDnid(); String getRdnis(); /** * Returns the context in the dial plan from which the AGI script was called. * * @return the context in the dial plan from which the AGI script was called. */ String getContext(); /** * Returns the extension in the dial plan from which the AGI script was called. * * @return the extension in the dial plan from which the AGI script was called. */ String getExtension(); /** * Returns the priority in the dial plan from which the AGI script was called. * * @return the priority in the dial plan from which the AGI script was called. */ Integer getPriority(); /** * Returns wheather this agi is passed audio (EAGI - Enhanced AGI).<br> * Enhanced AGI is currently not supported on FastAGI. * * @return Boolean.TRUE if this agi is passed audio, Boolean.FALSE otherwise. */ Boolean getEnhanced(); /** * Returns the account code set for the call. * * @return the account code set for the call. */ String getAccountCode(); } |