客户端协议处理框架

来源:互联网 发布:薛家 知乎 编辑:程序博客网 时间:2024/05/01 07:19

     作为协议处理框架的实现程序,通常要实现基于特定协议的URLConenction、URLStreamHandler和ContentHandler类的具体子类,此外还要创建URLStreamHandler和ContentHandler类的工厂类,他们负责创建URLStreamHandler和ContentHandler类的具体子类的实例

    

实现协议处理框架

  • EchoURLConnection类:继承自URLConnection类
  • EchoURLStreamHandler类:继承自URLStreamHandler类
  • EchoURLStreamHandlerFactory类:实现了URLStreamHandlerFactory接口
  • EchoContentHandler:继承自ContentHandler类
  • EchoContentHandlerFactory:实现ContentHandlerFactory接口
package 客户端协议处理框架;

创建EchoURLConnection:

public class EchoURLConnection extends URLConnection{
private Socket connection = null;
private static final int DEFAULTPORT = 8089;

public EchoURLConnection(URL url) {
       super(url);
}
   public synchronized InputStream getInputStream() throws IOException
    {
    if(!connected) connect();
    return connection.getInputStream();
    }
   public synchronized OutputStream getOutputStream() throws IOException
   {
  if(!connected) connect();
  return connection.getOutputStream();
   }
   public String getContentType()
   {
  return "text/plain";
   }
@Override
public void connect() throws IOException {
// TODO Auto-generated method stub
if(!connected){
int port=url.getPort();
if(port<0||port>65535) port=DEFAULTPORT;
connection=new Socket(url.getHost(),port);
connected=true;
}
}
public void close() throws IOException{
if(connected){
connection.close();
connected=false;
}
}
}

创建EchoURLStreamHandler 类和工厂

URLStreamHandler 协议处理器,主要负责创建与协议相关的URLConnection对象,EchoURLStreamHandler类的openConnection()方法负责创建一个EchoURLConenction对象。URL类的openConnection()方法会调用URLStreamHandler类的openConnection()方法。

package 客户端协议处理框架;


import java.io.IOException;
import java.net.*;


public class EchoURLStreamHandler extends URLStreamHandler{
    public int getDefaultPort()
    {
    return 8089;
    }
@Override
protected URLConnection openConnection(URL url) throws IOException {
// TODO Auto-generated method stub
return new EchoURLConnection(url);
}
}

EchoURLStreamHandler的工厂类 

package 客户端协议处理框架;
import java.net.*;
public class EchoURLStreamHandlerFactory implements URLStreamHandlerFactory{


@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
// TODO Auto-generated method stub
if(protocol.equals("echo"))
return new EchoURLStreamHandler();
return null;
}


}

 创建EchoContentHandler类和工厂类

对于服务器发送的数据,客户程序可通过URLConnection类的getInputStream()方法获得输入流,然后读取数据,此外URLConnection类还提供了getContent()方法,

它有两种重载形式:

1)public Object getContent()

2) public Object getContent(Class[] classes)

第一个getContent()方法能把服务器发送的数据转换成一个Java对象。第二个getContent()方法的classes参数制定了转换而成的Java对象的类型,getContent()方法把服务器发送的数据优先转换成classes数组中

第一个元素制定的类型,如果转换失败,则尝试转换为classes数组中第二个元素的类型,依次类推。URLConnection类的getContent()方法会调用ContentHandler类的相应的getContent()方法。


package 客户端协议处理框架;
import java.io.*;
import java.net.*;
public class EchoContentHandler extends ContentHandler{


@Override
public Object getContent(URLConnection connection) throws IOException {
// TODO Auto-generated method stub
InputStream inputStream = connection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
return reader.readLine();
}
public Object getContent(URLConnection connection,Class[] classes) throws IOException{
InputStream inputStream = connection.getInputStream();
for(int i=0;i<classes.length;i++)
{
if(classes[i]==InputStream.class) return inputStream;
else if(classes[i]==String.class) return getContent(connection);
}
return null;
}
}

package 客户端协议处理框架;
import java.net.*;
public class EchoContentHandlerFactory implements ContentHandlerFactory{


@Override
public ContentHandler createContentHandler(String mimetype) {
// TODO Auto-generated method stub
if(mimetype.equals("text/plain"))
return new EchoContentHandler();
else
return null;
}


}

测试类:

package 客户端协议处理框架;


import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import javax.swing.plaf.synth.SynthSpinnerUI;


public class EchoClient {


public static void main(String[] args) {
// TODO Auto-generated method stub
URL.setURLStreamHandlerFactory(new EchoURLStreamHandlerFactory());
URLConnection.setContentHandlerFactory(new EchoContentHandlerFactory());
try {
URL url = new URL("echo://localhost:8089");
EchoURLConnection connection = (EchoURLConnection)url.openConnection();
connection.setDoOutput(true);
PrintWriter  writer = new PrintWriter(connection.getOutputStream(),true);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
while ((msg = reader.readLine()) != null) {
writer.println(msg);
Object content = connection.getContent();
if (content instanceof String) {
System.out.println(content);
if (content.equals("echo:bye")) {
connection.close();
break;
}
}
else if(content instanceof InputStream){
InputStream in=(InputStream)content;
BufferedReader read = new BufferedReader(new InputStreamReader(in));
System.out.println(read.readLine());
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}



0 0
原创粉丝点击