我需要在Java上编写一个XMLA 代理类,它与一侧的多维数据集(蒙德里安或 MS SSAS)和另一侧的 Kendo UI PivotGrid 进行通信。它必须做两件事:
- 在 OLAP 服务器上进行自动化
- 将查询从 UI 转换为 OLAP Cube 并返回
Kendo PivotGrid 可以直接连接到 XMLA 服务器,但这不是一个好主意,因为客户端(浏览器)可以读取用户和密码连接所需所以,我决定写代理,把它们隐藏起来。
假设这一点,您能否就如何实施它提供一些指导?
经过一番研究,我在 olap4j 源代码中找到了解决方案。这里有一个 chenged 方法,org.olap4j.driver.xmla.proxy.XmlaOlap4jAbstractHttpProxy
类成员
public byte[] getResponse(String server_url, String request) throws IOException {
URLConnection urlConnection = null;
try {
URL url = new URL(server_url);
urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
// Set headers
urlConnection.setRequestProperty("content-type", "text/xml; charset=".concat("UTF-8"));
urlConnection.setRequestProperty("User-Agent", "Olap4j");
urlConnection.setRequestProperty("Accept", "text/xml;q=1");
urlConnection.setRequestProperty("Accept-Charset", "UTF-8;q=1");
urlConnection.setRequestProperty("Accept-Encoding", "gzip"); // Tell the server that we support gzip encoding
// Some servers expect a SOAPAction header.
if (request.contains("DISCOVER")) {
urlConnection.setRequestProperty("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Discover\"");
} else if (request.contains("EXECUTE")) {
urlConnection.setRequestProperty("SOAPAction", "\"urn:schemas-microsoft-com:xml-analysis:Execute\"");
}
// Send data (i.e. POST). Use same encoding as specified in the
// header.
final String encoding1 = "UTF-8";
urlConnection.getOutputStream().write(request.getBytes(encoding1));
// Get the response, again assuming default encoding.
InputStream is = urlConnection.getInputStream();
// Detect that the server used gzip encoding
String contentEncoding =
urlConnection.getHeaderField("Content-Encoding");
if ("gzip".equals(contentEncoding)) {
is = new GZIPInputStream(is);
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int count;
while ((count = is.read(buf)) > 0) {
baos.write(buf, 0, count);
}
return baos.toByteArray();
} catch (Exception e) {
// In order to prevent the JDK from keeping this connection
// in WAIT mode, we need to empty the error stream cache.
final int espCode =
((HttpURLConnection) urlConnection).getResponseCode();
InputStream errorStream =
((HttpURLConnection) urlConnection).getErrorStream();
final ByteArrayOutputStream baos =
new ByteArrayOutputStream();
final byte[] buf = new byte[1024];
int count;
if (errorStream != null) {
while ((count = errorStream.read(buf)) > 0) {
baos.write(buf, 0, count);
}
errorStream.close();
}
baos.close();
}
return null;
}