icomm.java

来自「OO 图书馆系统」· Java 代码 · 共 225 行

JAVA
225
字号

import java.net.*;
import javax.xml.parsers.*;
import org.apache.axis.message.SOAPBody;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.w3c.dom.*;
//subject of Comm
//create an interface IComm for both REST and SOAP format
interface IComm {
   //sending request to flickr api
   public void sendRequest();
   //receiving response from flickr api
   public Document getResponse();
}


//real subject of CommSoap for SOAP format
class CommSoap implements IComm {
  //create a request SOAPEvelope object 
  private SOAPEnvelope requestenv;
  //create a response SOAPEvelope object
  private SOAPEnvelope env;
  //create a document object
  private Document doc;
  //create a URL object
  private URL url;
  
  //constructor initialization
  public CommSoap(URL url,SOAPEnvelope env){
  	
  	this.url=url;
  	this.requestenv=env;
  	
  	
  }
  //actual running function of sendRequest() for SOAP 
  public void sendRequest(){
  	
  	 try{
  	 
  	   // build the call.
       Service service = new Service();
       Call call = (Call) service.createCall();
       call.setTargetEndpointAddress(url);
       this.env = call.invoke(requestenv);
     }
     catch (Exception e) {
       e.printStackTrace();
     }
  	
  }
  //actual running function of getResponse() for SOAP
  public Document getResponse(){
  	 
  	  
  	
  	 try{
  	
  	   SOAPBody result = (SOAPBody) env.getBody();

       this.doc = result.getAsDocument();
       
     }
     catch (Exception e) {
       e.printStackTrace();
      
     }
         
  	 return doc;
  }
  
}

//proxy of CommSoap
class CommSoapProxy implements IComm {

  //create a request SOAPEvelope object 
  private SOAPEnvelope requestenv;
  //create a document object
  private Document doc;
  //create a URL object
  private URL url;
  //create a CommSoap object
  private CommSoap commsoap;
  
  //constructor initialization
  public CommSoapProxy(URL url,SOAPEnvelope env){
  	
  	this.url=url;
  	this.requestenv=env;
  	
  	
  }
  
  //proxy sendRequest() of SOAP
  public void sendRequest(){
  	// Use 'lazy initialization' 
    if (commsoap == null)
    {
      commsoap = new CommSoap(url,requestenv);
    }
    
    //using real sendRequest() of class CommSoap
    commsoap.sendRequest();
  	
  }
  
  //proxy getResponse() of SOAP
  public Document getResponse(){
  	
  	//using real getResponse() of class CommSoap 
  	this .doc=commsoap.getResponse();
  	return doc;
  	
  	
  }
  
}


//real subject of CommREST for REST format
class CommREST implements IComm {
  //create a document object
  private Document doc;
  //create a URL object
  private URL url;
  //create a URLConnection object;
  private URLConnection conn;
  
  //constructor initialization
  public CommREST(URL url){
  	
  	this.url=url;
  	
  	
  }
  //actual running function of sendRequest() for REST
  public void sendRequest(){
  	
  	 try{
  	 
  	   // build the call.
         this.conn = url.openConnection();
     }
     catch (Exception e) {
       e.printStackTrace();
     }
  	
  }
  //actual running function of getResponse() for REST
  public Document getResponse(){
  	 
  	  
  	
  	 try{
  	
  	    // Get the response
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        DocumentBuilder dber;
        dber = factory.newDocumentBuilder();
        this.doc = dber.parse(conn.getInputStream());
     
     }
     catch (Exception e) {
       e.printStackTrace();
      
     }
         
  	 return doc;
  }
  
}

//proxy of CommREST
class CommRESTProxy implements IComm {

  //create a document object
  private Document doc;
  //create a URL object
  private URL url;
  //create a CommREST object
  private CommREST commrest;
  
  //constructor initialization
  public CommRESTProxy(URL url){
  	
  	this.url=url;
  	
  	
  }
  
  //proxy sendRequest() of REST
  public void sendRequest(){
  	// Use 'lazy initialization' 
    if (commrest == null)
    {
      commrest = new CommREST(url);
    }
    
    //using real sendRequest() of class CommREST
    commrest.sendRequest();
  	
  }
  
  //proxy getResponse() of REST
  public Document getResponse(){
  	
  	//using real getResponse() of class CommREST 
  	this.doc=commrest.getResponse();
  	return doc;
  	
  	
  }
  
}







⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?