1、通过用户名密码登录系统时,发送请求到Cognos服务器。通过请求处理流程,若通过身份验证,则COGNOS服务器返回PASSPORT字符串,该字符串是查询Content Store、打开报表、进行报表分析的身份凭证;若验证失败,则返回错误信息,并提示用户重新登录。用户使用相关交易时,系统能够自动完成COGNOS的身份验证过程,实现单点登录,代码如下:
- package com.cognos.common;
- import java.net.MalformedURLException;
- import java.rmi.RemoteException;
- import org.apache.axis.AxisFault;
- import org.apache.axis.client.Stub;
- import org.apache.axis.message.SOAPHeaderElement;
- import com.cognos.developer.schemas.bibus._3.BiBusHeader;
- import com.cognos.developer.schemas.bibus._3.Configuration;
- import com.cognos.developer.schemas.bibus._3.ContentManagerServiceStub;
- import com.cognos.developer.schemas.bibus._3.ContentManagerService_ServiceLocator;
- import com.cognos.developer.schemas.bibus._3.ReportServiceStub;
- import com.cognos.developer.schemas.bibus._3.ReportService_ServiceLocator;
- import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
- import com.cognos.developer.schemas.bibus._3.XmlEncodedXML;
- public class CognosLog {
- // 创建连接Cognos服务的对象.
- //Content Manager Service 内容库管理服务Locator对象
- private ContentManagerService_ServiceLocator cmServiceLocator = null;
- //Report Service 报表服务Locator对象
- private ReportService_ServiceLocator reportServiceLocator = null;
- // Content Manager Service 内容库服务对象
- private ContentManagerServiceStub cmService = null;
- //Report Service 报表服务对象
- private ReportServiceStub repService = null;
-
- // Cognos 8 Content Manager 服务器地址
- private String CM_URL = "http://localhost:9300/p2pd/servlet/dispatch";
- //用户名
- private String username = "root";
- //密码
- private String pwd = "lgtq1983";
- //名称空间
- private String namespace = "lgtq1983";
-
- //构造函数,通过配置文件得到服务器地址,名称空间,用户名和密码,并连接登陆cognos服务器
- public CognosLog(String CM_URL, String username, String pwd,
- String namesapce) {
- this.CM_URL = CM_URL;
- this.username = username;
- this.pwd = pwd;
- this.namespace = namesapce;
- }
- /**
- * 此方法连接Cognos服务
- *
- * @返回一个连接
- */
- public ContentManagerServiceStub connectToCognosServer() throws RemoteException {
- //BI Bus
- BiBusHeader bibus = null;
- while (bibus == null) {
- // content manger service locator 内容管理服务locator
- cmServiceLocator = new ContentManagerService_ServiceLocator();
- //report service locator 报表服务locator
- reportServiceLocator = new ReportService_ServiceLocator();
- try {
- java.net.URL serverURL = new java.net.URL(CM_URL);
- // 得到Cognos 的服务,通过stub方法来初始化,得到连接内容管理服务对象
- cmService = new ContentManagerServiceStub(serverURL,
- cmServiceLocator);
- //创建报表服务对象
- repService = new ReportServiceStub(serverURL,
- reportServiceLocator);
- //设置超时时间,单位为秒,0为从不超时
- cmService.setTimeout(0);
- repService.setTimeout(0);
- } // ...异常捕捉
- catch (MalformedURLException e) {
- System.out.println("Malformed URL:n" + e.getMessage());
- return null;
- } catch (AxisFault e) {
- e.printStackTrace();
- }
- try {
- //连接cognos报表服务器
- quickLogon();
- } catch (Exception ex) {
- System.out.println("");
- return null;
- }
- SOAPHeaderElement x = ((Stub) cmService).getResponseHeader(
- "http://developer.cognos.com/schemas/bibus/3/",
- "biBusHeader");
- // 得到 biBusHeader SOAP:包含登陆信息的Header
- bibus = BIBusHeaderHelper.getHeaderObject(x);
- if (bibus != null) {
- ((Stub) cmService).setHeader(
- "http://developer.cognos.com/schemas/bibus/3/",
- "biBusHeader", bibus);
- return cmService;
- }
- }
- return null;
- }
- /**
- * 匿名登录
- * @return
- */
- public ContentManagerServiceStub connectToCognosServerWithAnonymous() {
- BiBusHeader bibus = null;
- cmServiceLocator = new ContentManagerService_ServiceLocator();
- reportServiceLocator = new ReportService_ServiceLocator();
- try {
- java.net.URL serverURL = new java.net.URL(CM_URL);
- cmService = new ContentManagerServiceStub(serverURL,
- cmServiceLocator);
- repService = new ReportServiceStub(serverURL, reportServiceLocator);
- cmService.setTimeout(0);
- repService.setTimeout(0);
- } catch (MalformedURLException e) {
- System.out.println("Malformed URL:n" + e.getMessage());
- return null;
- } catch (AxisFault e) {
- e.printStackTrace();
- }
- SOAPHeaderElement x = ((Stub) cmService).getResponseHeader(
- "http://developer.cognos.com/schemas/bibus/3/", "biBusHeader");
- bibus = BIBusHeaderHelper.getHeaderObject(x, true, "");
- ((Stub) cmService).setHeader(
- "http://developer.cognos.com/schemas/bibus/3/", "biBusHeader",
- bibus);
- return cmService;
- }
-
- //得到content manager service的接口
- public ContentManagerServiceStub getCMService() {
- return cmService;
- }
- //得到report service 的接口
- public ReportServiceStub getReportService() {
- BiBusHeader bibus = null;
- //从report service得到bibus
- bibus = BIBusHeaderHelper.getHeaderObject(((Stub) repService)
- .getResponseHeader(
- "http://developer.cognos.com/schemas/bibus/3/",
- "biBusHeader"));
- //如果report service的bibus不可用,就从content manager service得到bibus
- if (bibus == null) {
- BiBusHeader CMbibus = null;
- //得到content manager service的bibus
- CMbibus = BIBusHeaderHelper.getHeaderObject(((Stub) cmService)
- .getResponseHeader(
- "http://developer.cognos.com/schemas/bibus/3/",
- "biBusHeader"));
- (repService).setHeader("", "biBusHeader", CMbibus);
- }
- return repService;
- }
- //登陆Cognos 8
- private void quickLogon() throws RemoteException {
- //认证字符串
- StringBuffer credentialXML = new StringBuffer();
- credentialXML.append("<credential>");
- //名称空间
- credentialXML.append("<namespace>");
- credentialXML.append(namespace == null ? "" : namespace);
- credentialXML.append("</namespace>");
- //用户名
- credentialXML.append("<username>");
- credentialXML.append(username == null ? "" : username);
- credentialXML.append("</username>");
- //密码
- credentialXML.append("<password>");
- credentialXML.append(pwd == null ? "" : pwd);
- credentialXML.append("</password>");
- credentialXML.append("</credential>");
- //转换为XML编码的认证字符串
- String encodedCredentials = credentialXML.toString();
- // 调用content manager service的logon方法,此方法为stub的方法
- getCMService().logon(new XmlEncodedXML(encodedCredentials),
- new SearchPathSingleObject[] {});
- System.out.println("服务器连接成功");
- //使用logoff方法来结束session,并且从CAM对象中把passport移除
- }
- public String getPassPort() {
- BiBusHeader bibus = null;
- String passport = null;
- try {
- SOAPHeaderElement x = ((Stub) cmService).getResponseHeader(
- "http://developer.cognos.com/schemas/bibus/3/",
- "biBusHeader");
- bibus = BIBusHeaderHelper.getHeaderObject(x, true, "");
- passport = bibus.getCAM().getCAMPassport().getId().toString();
- System.out.println("passport:"+passport);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return passport;
- }
- /**
- * 测试服务器是否连接成功
- */
- public static void main(String agrs[]) {
- CognosLog cognos = new CognosLog("http://localhost:9300/p2pd/servlet/dispatch","root","huawei","PS");
- try {
- //创建报表服务器连接
- cognos.connectToCognosServer();
- //获取passport值
- cognos.getPassPort();
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- }
添加新评论0 条评论