superdebug
作者superdebug·2012-09-14 14:02
信息技术经理·可以

Cognos10实现用户登录并获取passport值

字数 22411阅读 3058评论 0赞 0
1、通过用户名密码登录系统时,发送请求到Cognos服务器。通过请求处理流程,若通过身份验证,则COGNOS服务器返回PASSPORT字符串,该字符串是查询Content Store、打开报表、进行报表分析的身份凭证;若验证失败,则返回错误信息,并提示用户重新登录。用户使用相关交易时,系统能够自动完成COGNOS的身份验证过程,实现单点登录,代码如下:
  1. package com.cognos.common;


  2. import java.net.MalformedURLException;
  3. import java.rmi.RemoteException;

  4. import org.apache.axis.AxisFault;
  5. import org.apache.axis.client.Stub;
  6. import org.apache.axis.message.SOAPHeaderElement;

  7. import com.cognos.developer.schemas.bibus._3.BiBusHeader;
  8. import com.cognos.developer.schemas.bibus._3.Configuration;
  9. import com.cognos.developer.schemas.bibus._3.ContentManagerServiceStub;
  10. import com.cognos.developer.schemas.bibus._3.ContentManagerService_ServiceLocator;
  11. import com.cognos.developer.schemas.bibus._3.ReportServiceStub;
  12. import com.cognos.developer.schemas.bibus._3.ReportService_ServiceLocator;
  13. import com.cognos.developer.schemas.bibus._3.SearchPathSingleObject;
  14. import com.cognos.developer.schemas.bibus._3.XmlEncodedXML;

  15. public class CognosLog {
  16.         // 创建连接Cognos服务的对象.
  17.         //Content Manager Service 内容库管理服务Locator对象
  18.         private ContentManagerService_ServiceLocator cmServiceLocator = null;
  19.         //Report Service 报表服务Locator对象
  20.         private ReportService_ServiceLocator reportServiceLocator = null;
  21.         // Content Manager Service 内容库服务对象
  22.         private ContentManagerServiceStub cmService = null;
  23.         //Report Service 报表服务对象
  24.         private ReportServiceStub repService = null;
  25.          

  26.         // Cognos 8 Content Manager 服务器地址
  27.         private String CM_URL = "http://localhost:9300/p2pd/servlet/dispatch";
  28.         //用户名
  29.         private String username = "root";
  30.         //密码
  31.         private String pwd = "lgtq1983";
  32.         //名称空间
  33.         private String namespace = "lgtq1983";
  34.        
  35.         //构造函数,通过配置文件得到服务器地址,名称空间,用户名和密码,并连接登陆cognos服务器
  36.                 public CognosLog(String CM_URL, String username, String pwd,
  37.                                 String namesapce) {
  38.                         this.CM_URL = CM_URL;
  39.                         this.username = username;
  40.                         this.pwd = pwd;
  41.                         this.namespace = namesapce;
  42.                 }

  43.         /**
  44.          * 此方法连接Cognos服务
  45.          *
  46.          * @返回一个连接
  47.          */
  48.         public ContentManagerServiceStub connectToCognosServer() throws RemoteException {
  49.                 //BI Bus
  50.                                 BiBusHeader bibus = null;
  51.                                 while (bibus == null) {
  52.                                         // content manger service locator 内容管理服务locator
  53.                                         cmServiceLocator = new ContentManagerService_ServiceLocator();
  54.                                         //report service locator 报表服务locator
  55.                                         reportServiceLocator = new ReportService_ServiceLocator();

  56.                                         try {
  57.                                                 java.net.URL serverURL = new java.net.URL(CM_URL);
  58.                                                 // 得到Cognos 的服务,通过stub方法来初始化,得到连接内容管理服务对象
  59.                                                 cmService = new ContentManagerServiceStub(serverURL,
  60.                                                                 cmServiceLocator);
  61.                                                 //创建报表服务对象
  62.                                                 repService = new ReportServiceStub(serverURL,
  63.                                                                 reportServiceLocator);
  64.                                                 //设置超时时间,单位为秒,0为从不超时
  65.                                                 cmService.setTimeout(0);
  66.                                                 repService.setTimeout(0);
  67.                                         } // ...异常捕捉
  68.                                         catch (MalformedURLException e) {
  69.                                                 System.out.println("Malformed URL:n" + e.getMessage());
  70.                                                 return null;
  71.                                         } catch (AxisFault e) {

  72.                                                 e.printStackTrace();
  73.                                         }

  74.                                         try {
  75.                                                 //连接cognos报表服务器
  76.                                                 quickLogon();
  77.                                         } catch (Exception ex) {
  78.                                                 System.out.println("");
  79.                                                 return null;
  80.                                         }

  81.                                         SOAPHeaderElement x = ((Stub) cmService).getResponseHeader(
  82.                                                         "http://developer.cognos.com/schemas/bibus/3/",
  83.                                                         "biBusHeader");
  84.                                         // 得到 biBusHeader SOAP:包含登陆信息的Header
  85.                                         bibus = BIBusHeaderHelper.getHeaderObject(x);

  86.                                         if (bibus != null) {
  87.                                                 ((Stub) cmService).setHeader(
  88.                                                                 "http://developer.cognos.com/schemas/bibus/3/",
  89.                                                                 "biBusHeader", bibus);
  90.                                                 return cmService;
  91.                                         }

  92.                                 }
  93.                                 return null;
  94.         }
  95.         /**
  96.          * 匿名登录
  97.          * @return
  98.          */
  99.         public ContentManagerServiceStub connectToCognosServerWithAnonymous() {
  100.                 BiBusHeader bibus = null;
  101.                 cmServiceLocator = new ContentManagerService_ServiceLocator();
  102.                 reportServiceLocator = new ReportService_ServiceLocator();

  103.                 try {
  104.                         java.net.URL serverURL = new java.net.URL(CM_URL);

  105.                         cmService = new ContentManagerServiceStub(serverURL,
  106.                                         cmServiceLocator);
  107.                         repService = new ReportServiceStub(serverURL, reportServiceLocator);

  108.                         cmService.setTimeout(0);
  109.                         repService.setTimeout(0);

  110.                 } catch (MalformedURLException e) {
  111.                         System.out.println("Malformed URL:n" + e.getMessage());
  112.                         return null;
  113.                 } catch (AxisFault e) {

  114.                         e.printStackTrace();
  115.                 }

  116.                 SOAPHeaderElement x = ((Stub) cmService).getResponseHeader(
  117.                                 "http://developer.cognos.com/schemas/bibus/3/", "biBusHeader");
  118.                 bibus = BIBusHeaderHelper.getHeaderObject(x, true, "");

  119.                 ((Stub) cmService).setHeader(
  120.                                 "http://developer.cognos.com/schemas/bibus/3/", "biBusHeader",
  121.                                 bibus);
  122.                 return cmService;
  123.         }
  124.        
  125.         //得到content manager service的接口
  126.         public ContentManagerServiceStub getCMService() {
  127.                 return cmService;
  128.         }
  129.         //得到report service 的接口
  130.         public ReportServiceStub getReportService() {
  131.                 BiBusHeader bibus = null;
  132.                 //从report service得到bibus
  133.                 bibus = BIBusHeaderHelper.getHeaderObject(((Stub) repService)
  134.                                 .getResponseHeader(
  135.                                                 "http://developer.cognos.com/schemas/bibus/3/",
  136.                                                 "biBusHeader"));
  137.                 //如果report service的bibus不可用,就从content manager service得到bibus
  138.                 if (bibus == null) {
  139.                         BiBusHeader CMbibus = null;
  140.                         //得到content manager service的bibus
  141.                         CMbibus = BIBusHeaderHelper.getHeaderObject(((Stub) cmService)
  142.                                         .getResponseHeader(
  143.                                                         "http://developer.cognos.com/schemas/bibus/3/",
  144.                                                         "biBusHeader"));

  145.                         (repService).setHeader("", "biBusHeader", CMbibus);
  146.                 }
  147.                 return repService;
  148.         }
  149.         //登陆Cognos 8
  150.         private void quickLogon() throws RemoteException {
  151.                 //认证字符串
  152.                 StringBuffer credentialXML = new StringBuffer();

  153.                 credentialXML.append("<credential>");
  154.                 //名称空间
  155.                 credentialXML.append("<namespace>");
  156.                 credentialXML.append(namespace == null ? "" : namespace);
  157.                 credentialXML.append("</namespace>");
  158.                 //用户名
  159.                 credentialXML.append("<username>");
  160.                 credentialXML.append(username == null ? "" : username);
  161.                 credentialXML.append("</username>");
  162.                 //密码
  163.                 credentialXML.append("<password>");
  164.                 credentialXML.append(pwd == null ? "" : pwd);
  165.                 credentialXML.append("</password>");

  166.                 credentialXML.append("</credential>");
  167.                 //转换为XML编码的认证字符串
  168.                 String encodedCredentials = credentialXML.toString();

  169.                 // 调用content manager service的logon方法,此方法为stub的方法
  170.                 getCMService().logon(new XmlEncodedXML(encodedCredentials),
  171.                                 new SearchPathSingleObject[] {});
  172.                 System.out.println("服务器连接成功");
  173.                 //使用logoff方法来结束session,并且从CAM对象中把passport移除

  174.         }
  175.         public String getPassPort() {
  176.                 BiBusHeader bibus = null;
  177.                 String passport = null;
  178.                 try {
  179.                         SOAPHeaderElement x = ((Stub) cmService).getResponseHeader(
  180.                                         "http://developer.cognos.com/schemas/bibus/3/",
  181.                                         "biBusHeader");
  182.                         bibus = BIBusHeaderHelper.getHeaderObject(x, true, "");
  183.                         passport = bibus.getCAM().getCAMPassport().getId().toString();
  184.                         System.out.println("passport:"+passport);
  185.                 } catch (Exception e) {
  186.                         e.printStackTrace();
  187.                 }
  188.                 return passport;
  189.         }
  190.         /**
  191.          * 测试服务器是否连接成功
  192.          */
  193.         public static void main(String agrs[]) {
  194.                 CognosLog cognos = new CognosLog("http://localhost:9300/p2pd/servlet/dispatch","root","huawei","PS");
  195.                 try {
  196.                         //创建报表服务器连接
  197.                         cognos.connectToCognosServer();
  198.                         //获取passport值
  199.                         cognos.getPassPort();
  200.                 } catch (RemoteException e) {
  201.                         e.printStackTrace();
  202.                 }
  203.         }
  204. }

如果觉得我的文章对您有用,请点赞。您的支持将鼓励我继续创作!

0

添加新评论0 条评论

Ctrl+Enter 发表

作者其他文章

相关问题

相关资料

X社区推广