[Asterisk-java-cvs] CVS: asterisk-java/src/java/net/sf/asterisk/fastagi RequestBuilderImpl.java,NONE
Brought to you by:
srt
From: Stefan R. <sr...@us...> - 2005-03-10 13:45:38
|
Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16225/src/java/net/sf/asterisk/fastagi Modified Files: AGIReader.java AGIReaderImpl.java Added Files: RequestBuilderImpl.java AGIConnectionHandler.java RequestBuilder.java Removed Files: AGIRequestBuilderImpl.java AGIRequestBuilder.java Log Message: --- NEW FILE: RequestBuilderImpl.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: RequestBuilderImpl.java,v 1.1 2005/03/10 13:45:23 srt Exp $ */ public class RequestBuilderImpl implements RequestBuilder { /** * Creates a new AGIRequestBuilderImpl. */ public RequestBuilderImpl() { } public AGIRequest buildRequest(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: AGIConnectionHandler.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.IOException; import net.sf.asterisk.io.SocketConnectionFacade; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * An AGIConnectionHandler is created and run by the AGIServer whenever a new * socket connection from an Asterisk Server is received.<br> * It reads the request using an AGIReader and runs the AGIScript configured to * handle this type of request. Finally it closes the socket connection. * * @author srt * @version $Id: AGIConnectionHandler.java,v 1.1 2005/03/10 13:45:23 srt Exp $ */ public class AGIConnectionHandler implements Runnable { private final Log logger = LogFactory.getLog(getClass()); private SocketConnectionFacade socket; private AGIReader reader; private AGIWriter writer; /** * Creates a new AGIConnectionHandler to handle the given socket connection. * @param socket the socket connection to handle. */ public AGIConnectionHandler(SocketConnectionFacade socket) { this.socket = socket; this.reader = new AGIReaderImpl(socket); this.writer = new AGIWriterImpl(socket); } public void run() { AGIRequest request; AGIResponse response; AGIScript script; try { request = reader.readRequest(); response = new AGIResponseImpl(writer, reader); //TODO find appropriate AGIScript //TODO run AGIScript //script.service(request, response); } catch (IOException e) { logger.error("IOException while handling request", e); } finally { try { socket.close(); } catch(IOException e) { } } } } --- NEW FILE: RequestBuilder.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: RequestBuilder.java,v 1.1 2005/03/10 13:45:23 srt Exp $ */ public interface RequestBuilder { /** * 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 buildRequest(Collection lines); } Index: AGIReader.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/AGIReader.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -p -r1.2 -r1.3 --- AGIReader.java 9 Mar 2005 23:45:06 -0000 1.2 +++ AGIReader.java 10 Mar 2005 13:45:23 -0000 1.3 @@ -30,7 +30,15 @@ import net.sf.asterisk.fastagi.reply.AGI public interface AGIReader { /** - * Reads one reply from the network. + * Reads the initial request data from Asterisk. + * + * @return the request read. + * @throws IOException if the request can't be read. + */ + AGIRequest readRequest() throws IOException; + + /** + * Reads one reply to an AGICommand from Asterisk. * * @return the reply read. * @throws IOException if the reply can't be read. Index: AGIReaderImpl.java =================================================================== RCS file: /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi/AGIReaderImpl.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -p -r1.1 -r1.2 --- AGIReaderImpl.java 9 Mar 2005 23:33:03 -0000 1.1 +++ AGIReaderImpl.java 10 Mar 2005 13:45:23 -0000 1.2 @@ -33,13 +33,38 @@ public class AGIReaderImpl implements AG { 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 IOException + { + AGIRequest request; + String line; + List lines; + + lines = new ArrayList(); + + while ((line = socket.readLine()) != null) + { + if (line.length() == 0) + { + break; + } + + lines.add(line); + } + + request = requestBuilder.buildRequest(lines); + + return request; + } + public AGIReply readReply() throws IOException { AGIReply reply; --- AGIRequestBuilderImpl.java DELETED --- --- AGIRequestBuilder.java DELETED --- |