Update of /cvsroot/asterisk-java/asterisk-java/src/java/net/sf/asterisk/fastagi
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31610/src/java/net/sf/asterisk/fastagi
Added Files:
ReplyBuilderImpl.java ReplyBuilder.java
Log Message:
Added ReplyBuilder
--- NEW FILE: ReplyBuilderImpl.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.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.sf.asterisk.fastagi.reply.AGIReply;
/**
* Default implementation of the ReplyBuilder interface.
*
* @author srt
* @version $Id: ReplyBuilderImpl.java,v 1.1 2005/03/09 20:31:35 srt Exp $
*/
public class ReplyBuilderImpl implements ReplyBuilder
{
private static final Pattern STATUS_PATTERN = Pattern
.compile("^(\\d{3})[ -]");
private static final Pattern RESULT_PATTERN = Pattern
.compile("^200 result=(\\d+) ");
private static final Pattern SYNOPSIS_PATTERN = Pattern
.compile("^\\s*Usage:\\s*(.*)\\s*$");
private static final String END_OF_PROPER_USAGE = "520 End of proper usage.";
public AGIReply buildReply(List lines)
{
String firstLine;
AGIReply reply;
Matcher statusMatcher;
Matcher resultMatcher;
if (lines.size() == 0)
{
return null;
}
reply = new AGIReply();
firstLine = (String) lines.get(0);
statusMatcher = STATUS_PATTERN.matcher(firstLine);
if (statusMatcher.find())
{
reply.setStatus(Integer.parseInt(statusMatcher.group(1)));
}
resultMatcher = RESULT_PATTERN.matcher(firstLine);
if (resultMatcher.find())
{
reply.setResult(Integer.parseInt(resultMatcher.group(1)));
}
if (reply.getStatus() == AGIReply.SC_INVALID_COMMAND_SYNTAX)
{
StringBuffer usageSB;
if (lines.size() > 1)
{
String secondLine;
Matcher synopsisMatcher;
secondLine = (String) lines.get(1);
synopsisMatcher = SYNOPSIS_PATTERN.matcher(secondLine);
if (synopsisMatcher.find())
{
reply.setSynopsis(synopsisMatcher.group(1));
}
}
usageSB = new StringBuffer();
for (int i = 2; i < lines.size(); i++)
{
String line;
line = (String) lines.get(i);
if (END_OF_PROPER_USAGE.equals(line))
{
break;
}
usageSB.append(line.trim());
usageSB.append(" ");
}
reply.setUsage(usageSB.toString().trim());
}
return reply;
}
}
--- NEW FILE: ReplyBuilder.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.List;
import net.sf.asterisk.fastagi.reply.AGIReply;
/**
* Transforms a List of lines to instances of AGIReply.
*
* @author srt
* @version $Id: ReplyBuilder.java,v 1.1 2005/03/09 20:31:35 srt Exp $
*/
public interface ReplyBuilder
{
/**
* Transforms a List of lines read from Asterisk to an appropriate AGIReply.
*
* @param lines the lines read from Asterisk
* @return the appropriate AGIReply
*/
AGIReply buildReply(List lines);
}
|