📄 icommrest.java
字号:
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.*;
//interface ICommRest for REST
interface ICommRest {
//set communication between client and searver
public Document setComm();
}
//real subject CommRestRequest
class CommRestRequest implements ICommRest {
private Document doc;
private URL url;
private URLConnection conn;
//constructor
public CommRestRequest(URL url){
this.url=url;
}
public Document setComm(){
try{
//build call
this.conn = url.openConnection();
// 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 CommRestRequest
class CommRestRequestProxy implements ICommRest {
private Document doc;
private URL url;
private CommRestRequest commrestrequest;
//constructor
public CommRestRequestProxy(URL url){
this.url=url;
}
public Document setComm(){
// Use 'lazy initialization'
if (commrestrequest == null)
{
commrestrequest = new CommRestRequest(url);
}
this.doc=commrestrequest.setComm();
return doc;
}
}
//interface ICommSoap for SOAP
interface ICommSoap {
//set communication between client and searver
public Document setComm();
}
//real subject CommSoapRequest
class CommSoapRequest implements ICommSoap {
private SOAPEnvelope requestenv;
private SOAPEnvelope env;
private Document doc;
private URL url;
//constructor
public CommSoapRequest(URL url,SOAPEnvelope env){
this.url=url;
this.requestenv=env;
}
public Document setComm(){
try{
// build the call.
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(url);
this.env = call.invoke(requestenv);
SOAPBody result = (SOAPBody) env.getBody();
this.doc = result.getAsDocument();
}
catch (Exception e) {
e.printStackTrace();
}
return doc;
}
}
//proxy of CommSoapRequest
class CommSoapRequestProxy implements ICommSoap {
private SOAPEnvelope requestenv;
private Document doc;
private URL url;
private CommSoapRequest commsoaprequest;
//constructor
public CommSoapRequestProxy(URL url,SOAPEnvelope env){
this.url=url;
this.requestenv=env;
}
public Document setComm(){
// Use 'lazy initialization'
if (commsoaprequest == null)
{
commsoaprequest = new CommSoapRequest(url,requestenv);
}
this .doc=commsoaprequest.setComm();
return doc;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -