package com.resoft.navigate.util;
//Cognos Sdk调用包
//axisCrnpClient.jar
import javax.xml.rpc.ServiceException;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Stub;
import com.cognos.developer.schemas.bibus._3.*;
public class CognosLoginTest{
private ContentManagerService_Port cmService = null;
private ReportService_Port reportService = null;
// connect to Cognos8 services
public void connectToReportServer(String endPoint){
try{
ContentManagerService_ServiceLocator cmServiceLocator = new ContentManagerService_ServiceLocator();
ReportService_ServiceLocator rsServiceLocator = new ReportService_ServiceLocator();
cmService = cmServiceLocator.getcontentManagerService(new java.net.URL(endPoint));
reportService = rsServiceLocator.getreportService(new java.net.URL(endPoint));
}catch (ServiceException ex){
System.out.println("Caught a ServiceException: " + ex.getMessage());
ex.printStackTrace();
}catch (Exception ex){
System.out.println("Cognos 8 server URL was: " + endPoint);
}
}
// logon to Cognos 8
public String quickLogon(String namespace, String uid, String pwd){
StringBuffer credentialXML = new StringBuffer();
credentialXML.append("<credential>");
credentialXML.append("<namespace>").append(namespace).append("</namespace>");
credentialXML.append("<username>").append(uid).append("</username>");
credentialXML.append("<password>").append(pwd).append("</password>");
credentialXML.append("</credential>");
String encodedCredentials = credentialXML.toString();
XmlEncodedXML xmlCredentials = new XmlEncodedXML();
xmlCredentials.setValue(encodedCredentials);
try{
cmService.logon(xmlCredentials, null);
BiBusHeader CMbibus = (BiBusHeader) ((Stub) cmService).getHeaderObject("", "biBusHeader");
((Stub) reportService).setHeader("", "biBusHeader", CMbibus);
System.out.println("login 0k");
}catch (Exception e){
e.printStackTrace();
}
System.out.println("Logon successful as " + uid);
return ("Logon successful as " + uid);
}
public static void main(String[] args){
String namespaceID = "dbAuth";
String userID = "dssadmin";
String password = "123";
String endPoint = "http://10.0.3.199:81/cognos8/cgi-bin/cognos.cgi";
CognosLoginTest gc = new CognosLoginTest();
gc.connectToReportServer(endPoint);
gc.quickLogon(namespaceID, userID, password);
System.out.println("OK");
}
}
上面的代码在tommcat/websphere都没问题,但是在一客户使用weblogic却发现问题,在执行 cmService.logon(xmlCredentials, null);代码时就抛出异常,查阅相关资料发现,weblogic在解释XML格式时有自己的方法,原因如下:
1、weblogic8自带的xml解析器不能解析GBK编码的,如果要让weblogic能够解析GBK编码,那么需要为weblogic8配置额外的xml解析器;
2、 一般使用apache的xml解析器,也就是Xerces.jar和Xalan.jar;
需要在操作系统的当前用户下设定classpath指定上面的xml解析器, 在weblogic的控制台执行如下操作解决:
1、在控制台的导航树中找到Services->XML这一节点,点击该节点创建一个XML Registries,主要是填写一些类的全名,这些类在上面的xml解析器中,即上面的jar包中,配置如下
Document Builder Factory:org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
SAX Parser Factory:org.apache.xerces.jaxp.SAXParserFactoryImpl
Transformer Factory:org.apache.xalan.processor.TransformerFactoryImpl
2、点击apply
3、如果在myeclipse中启动weblogic8,那么还需要做如下配置,启动后的weblogic8才能支持GBK,进入到window->preferences->MyEclipse->Application Server->weblogic8->paths将Xerces.jar和Xalan.jar的路径加入到prepend to classpath中
Cognos 集成jar
以下jar包的api:
axis.jar
axisCrnpClient.jar
axisCognosClient.jar
commons-discovery.jar
commons-logging.jar
jaxrpc.jar
saaj.jar
wsdl4j.jar
xercesImpl.jar
xml-apis.jar
dom4j.jar
xalan.jar