xmla 的配置见http://www.activeinterface.com/b2005_11_21.html,但是,如果是通过java去访问XMLA,则会因为用户权限的问题,得不到正确结果。
配置方法是在xmla 的虚拟目录上,把安全性的基本身份验证方式选上,然后在java 代码中,通过 Authenticator 类为验证提供用户和密码。部分代码如下:
用户密码验证提供者
package com.chinadci;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
import java.net.*;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class AuthImpl extends Authenticator
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
protected PasswordAuthentication getPasswordAuthentication()
{
String user = "IBMUSERAZ3\\IBMUSER";
String password="xxxx";
char pass[] = password.toCharArray();
return new PasswordAuthentication(user, pass);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
package com.chinadci;
import java.io.*;
import java.net.*;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class XMLA
{
private String sXmlaServer;
private HttpURLConnection oHttp;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public XMLA()
{
super();
// TODO Auto-generated constructor stub
sXmlaServer= "http://localhost/olap/msmdpump.dll";
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public String EncloseTag(String sTag, String sValue)
{
return "<" + sTag + ">" + sValue + "</" + sTag + ">";
}
public String Discover(String sRequestType,String sRestrictions,String sProps)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Authenticator.setDefault(new AuthImpl());
//构造soap 头部
String strType = EncloseTag("RequestType",sRequestType);
String strRestrictions = EncloseTag("Restrictions",EncloseTag("RestrictionList",sRestrictions));
String strProps = EncloseTag("Properties",EncloseTag("PropertyList",sProps));
String sNS = " xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi = \"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String sPayload = "<SOAP-ENV:Envelope " + sNS + " >"
+ " <SOAP-ENV:Body>"
+ " <Discover xmlns=\"urn:schemas-microsoft-com:xml-analysis\" >"
+ strType + strRestrictions + strProps
+ " </Discover>"
+ " </SOAP-ENV:Body>"
+ " </SOAP-ENV:Envelope>";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
URL url;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
url = new URL(sXmlaServer);
oHttp = (HttpURLConnection) url.openConnection();
oHttp.setRequestMethod("POST");
oHttp.setDoOutput(true);
oHttp.setDoInput( true);
oHttp.setRequestProperty("Content-Type","text/xml");
oHttp.setRequestProperty("SOAPAction", '"'+ "urn:schemas-microsoft-com:xml-analysis:Discover" + '"' );
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(oHttp.getOutputStream(),"utf-8")),true);//创建输出流
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//send the post request
out.println(sPayload);
out.flush() ;
out.close() ;
//read the response xml
BufferedReader in
= new BufferedReader(new InputStreamReader(oHttp.getInputStream(),"utf-8"));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String responseMessage =oHttp.getResponseMessage();
String responseXml ="";
if (responseMessage.equals("OK" ))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
String line;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while ((line = in.readLine()) != null)
{
responseXml =responseXml+line;
}
}
return responseXml;
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return "";
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
测试入口
1
package com.chinadci;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
import java.io.File;
4
import java.io.FileWriter;
5
import java.io.IOException;
6
import java.net.Authenticator;
7
import java.net.PasswordAuthentication;
8![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class XmlaJavaService
{
10![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public XmlaJavaService()
{
12
super();
13
// TODO Auto-generated constructor stub
14
}
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
17
* @param args
18
*/
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
20
// TODO Auto-generated method stub
21
XMLA oxmla=new XMLA();
22
23
24![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
//String sRequestType = "DBSCHEMA_CATALOGS";
26
//String sRequestType = "DISCOVER_DATASOURCES";
27
String sRequestType = "MDSCHEMA_CUBES";
28
String sRestrictions = "";
29
String sProps = "<DataSourceInfo>IBMUSERAZ3\\SQL2005;</DataSourceInfo><Catalog>Adventure Works DW Standard Edition</Catalog><Format>Tabular</Format>";
30
String responseXml =oxmla.Discover(sRequestType,sRestrictions,sProps );
31
32
FileWriter f;
33![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
34
f = new FileWriter(new File("d:/1.xml"));
35
f.write(responseXml );
36
f.close() ;
37![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException e)
{
39
// TODO Auto-generated catch block
40
e.printStackTrace();
41
}
42![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
44
}
45![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
}
47![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)