import java.io.InputStream;
import java.io.IOException;
public class Request
{
public static final int HELO=0;
public static final int MAIL=1;
public static final int RCPT=2;
public static final int DATA=3;
public static final int NOOP=4;
public static final int RESET=5;
public static final int QUIT=6;
public static final int UNKNOWENCMD=7;
public static final int ERROR=-1;
public InputStream input;
public String request;
public String cmd;
public int requestType;
public String string;
public String domain;
public String user;
public String revrsePath;
public String forwordPath;
public int getRequestType(){return requestType;}
public String getUser(){ return user;}
public String getRevrsePath(){return revrsePath;}
public String getForwordPath(){return forwordPath;}
public String getRequestCmd(){return cmd;}
public String getRequest(){ return request;}
public String Domain(){return domain;}
public String getString(){ return string;}
public void clear()
{
user="";
revrsePath="";
forwordPath="";
domain="";
string="";
}//清空用户信息
public Request()
{
requestType=ERROR;
}
public Request(InputStream input)
{
this.input=input;
}//构造函数,初始化input对象
public String getMsg()
{
StringBuffer requestBuf=new StringBuffer(1024);//申请字符空间
int i;
byte[] buffer=new byte[1024];//字节数组
try
{
i=input.read(buffer);//从输入流中读用户信息并且保存在字节数组里
}
catch(IOException www)
{
www.printStackTrace();//出现IO错误,打印错误信息
i=-1;//将读取量设置为0
}
for(int j=0;j {
requestBuf.append((char) buffer[j]);//将字节数组转化为字符串
}
System.out.println(requestBuf.toString());
request=requestBuf.toString().trim();
jiexi();
return request;
}
public void jiexi()
{
int index=request.indexOf(' ');
if(index else cmd=request.substring(0,index).toUpperCase();
//提取出用户名令
if(cmd.equals("HELO"))
{
requestType=HELO;
domain=request.substring(index+1,request.length());
return;
}
//判断是否为HELO命令
if(cmd.equals("MAIL"))
{
requestType=MAIL;
int x=request.indexOf(' int y=request.indexOf('>');
revrsePath=request.substring(x+1,y);
return;
}
//判断是否为mail命令,提取出revrsePath变量
if(cmd.equals("RCPT"))
{
requestType=RCPT;
int x1=request.indexOf(' int y1=request.indexOf('>');
forwordPath=request.substring(x1+1,y1);
int z=forwordPath.indexOf('@');
if(z>0)
{
user=forwordPath.substring(0,z);
}
else user="";
return;
}
//判断是否为RCPT命令,提取出forwordPath,user变量
if(cmd.equals("DATA"))
{
requestType=DATA;
return;
}
//判断是否为DATA名林
if(cmd.equals("RESET"))
{
requestType=RESET;
return;
}
//判断是否为reset命令
if(cmd.equals("NOOP"))
{
requestType=NOOP;
return;
}
//判断是否为noop命令
if(cmd.equals("QUIT"))
{
requestType=QUIT;
return;
}
//判断是否为quit命令
requestType=UNKNOWENCMD;//如果不是七命令就为未知命令
}
}