Util是个工具类,对一些方法进行封装
package com.synale.cordys.soa;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class Util {
private static final String UserName="David";
private static final String PassWord="cordys";
private static final String ServerName="192.168.100.108";
private static final String CordysOrgName="Learning";
public static final String ServerUrl="http://"+ServerName+"/"+CordysOrgName+"/com.eibus.web.soap.Gateway.wcp";
private static final String soapRequestOfSaml =""
+ "<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+ " <SOAP:Header>"
+ " <wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
+ " <wsse:UsernameToken xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">"
+ " <wsse:Username>"+UserName+"</wsse:Username>"
+ " <wsse:Password>"+PassWord+"</wsse:Password>"
+ " </wsse:UsernameToken>"
+ " </wsse:Security>"
+ " </SOAP:Header>"
+ " <SOAP:Body>"
+ " <samlp:Request xmlns:samlp=\"urn:oasis:names:tc:SAML:1.0:protocol\" MajorVersion=\"1\" MinorVersion=\"1\">"
+ " <samlp:AuthenticationQuery>"
+ " <saml:Subject xmlns:saml=\"urn:oasis:names:tc:SAML:1.0:assertion\">"
+ " <saml:NameIdentifier Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified\">"+UserName+"</saml:NameIdentifier>"
+ " </saml:Subject>"
+ " </samlp:AuthenticationQuery>"
+ " </samlp:Request>"
+ " </SOAP:Body>"
+ " </SOAP:Envelope>";
/**
* 获取Cordys的令牌
* @throws Exception
*/
public static String getAssertionArtifact()
{
String AssertionArtifact="";
try{
PostMethod postmethod = new PostMethod(ServerUrl);
byte[] b = soapRequestOfSaml.getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/xop+xml; charset=UTF-8; type=\"text/xml\"");
postmethod.setRequestEntity(re);
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postmethod);
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document dom = builder.parse(postmethod.getResponseBodyAsStream());
Node samlArtifact = XPathAPI.selectSingleNode(dom, "//*[local-name()='AssertionArtifact']");
AssertionArtifact=samlArtifact.getTextContent();
}catch(Exception e){
e.printStackTrace();
}
return AssertionArtifact;
}
/**
* 通过 URL获取 HttpClient里面数据
* @param url地址, postData 请求数据
* @return 字符串
*/
public static String postHttpClient(String url,String postData)
{
String soapResponseData ="";
try {
PostMethod postmethod = new PostMethod(url);
if(postData!=null)
{
byte[] b = postData.getBytes("UTF-8");
InputStream is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/xop+xml; charset=UTF-8; type=\"text/xml\"");
postmethod.setRequestEntity(re);
}
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postmethod);
System.err.println("statuscode = " + statusCode);
soapResponseData = postmethod.getResponseBodyAsString();
}catch(Exception e){e.printStackTrace();}
System.out.println("soapResponseData:"+soapResponseData);
return soapResponseData ;
}
}
测试方法
package com.synale.cordys.test;
import com.synale.cordys.soa.*;
public class TestSOA {
/**
* @param args
*/
public static void main(String[] args) {
String SoapRequestOfTargetService =""
+"<SOAP:Envelope xmlns:SOAP=\"http://schemas.xmlsoap.org/soap/envelope/\"> "
+" <SOAP:Body> "
+" <GetAllLvdictionary xmlns=\"http://schemas.cordys.com/LVWSAppServerPackage\" preserveSpace=\"no\" qAccess=\"0\" qValues=\"\" /> "
+" </SOAP:Body> "
+"</SOAP:Envelope> ";
String AssertionArtifact=Util.getAssertionArtifact();
String ServerURL=Util.ServerUrl+"?SAMLart="+AssertionArtifact;
String ResponseData=Util.postHttpClient(ServerURL, SoapRequestOfTargetService);
System.out.println(ResponseData);
}
}