jeudi 5 avril 2018

Cognos Mashup Service CORS error

So I am trying to get the Cognos Mashup Service to work from the examples given here: https://www.ibm.com/communities/analytics/cognos-analytics-blog/how-to-use-ibm-10-2-2-cognos-mashup-service-cms-samples-in-ibm-cognos-analytics-11/

The application actually works perfectly when running in Eclipse using the internal browser. However, when I change the default eclipse browser from internal to external like Firefox or Chrome, I get a CORS Access error when connecting to Cognos 11:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://10.168.47.16:12385/ibmcognos/bi/v1/disp/rds/pagedReportData/report/i72BCF4B83AFE430F9A7F4721DF093FAB?fmt=HTMLFragment&selection=List1&rowLimit=2. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)

From what I understand, I need to add the following to my server config file:

<Location />
order allow,deny
allow from all
Header set Access-Control-Allow-Origin "*"
</Location> 

However, this just caused an infinite loop of 404 errors. So what am I doing wrong, and why does the internal eclipse browser work? This is my simple web app html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Generic Authentication Sample</title>
<script>

var myNameSpace=null;
var myUserName=null;
var myPassword=null;

try {
  var objXHR = new XMLHttpRequest();
  } catch (e) {
    try {
        alert('Msxml2.XMLHTTP');
      var objXHR = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
      try {
          alert('Microsoft.XMLHTTP')
        var objXHR = new ActiveXObject('Microsoft.XMLHTTP');
      } catch (e) {
        alert('XMLHttpRequest not supported'); }
      }
  }      

 function doReport()
 {

    myNameSpace=document.getElementById("nameSpace").value;
    myUserName=document.getElementById("userName").value;
    myPassword=document.getElementById("password").value;
    var myReportID=document.getElementById("storeID").value;

    var myFrag = document.getElementById("fragment");

    if(myNameSpace==""||myUserName==""||myPassword=="")
    {
        myFrag.innerHTML="";
        myFrag.innerHTML= "<HTML><BODY><b>Please enter Authentication information ....</b></BODY></HTML>";
        return;
    }

    if(myReportID=="")
    {
        myFrag.innerHTML="";
        myFrag.innerHTML= "<HTML><BODY><b>Please enter the Report ID ....</b></BODY></HTML>";
        return;
    }

    var url = parent.settings.document.getElementById("serverURL").value + "/bi/v1/disp/rds/pagedReportData/report/" + document.getElementById("storeID").value;
    window.status = "Loading Report. Please wait....";
    var frag = document.getElementById("fragment");
    frag.innerHTML= "<HTML><BODY>Loading Report. Please wait....</BODY></HTML>";
    try
    {
        objXHR.open("GET", url, true);
        objXHR.onreadystatechange = callBack;
        objXHR.send(null);
    }
    catch (e)
    {
        alert("Error occurs when doing report.\r\n"+ e);
    }

 }

function doLogon()
{

    var xmlData =
      "xmlData=<auth:credentials xmlns:auth='http://developer.cognos.com/schemas/ccs/auth/types/1'>"
      +"<auth:credentialElements><auth:name>CAMNamespace</auth:name>"
      +"<auth:value><auth:actualValue>"+myNameSpace+"</auth:actualValue></auth:value></auth:credentialElements>"
      +"<auth:credentialElements><auth:name>CAMUsername</auth:name>"
      +"<auth:value><auth:actualValue>"+myUserName+"</auth:actualValue></auth:value></auth:credentialElements>"
      +"<auth:credentialElements><auth:name>CAMPassword</auth:name>"
      +"<auth:value><auth:actualValue>"+myPassword+"</auth:actualValue></auth:value></auth:credentialElements>"
      +"</auth:credentials>";

    try
    {

        var url = parent.settings.document.getElementById("serverURL").value + "/bi/v1/disp/rds/auth/logon";
        objXHR.open("POST", url, false);
        objXHR.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        objXHR.setRequestHeader("Content-length",xmlData.length);
        objXHR.setRequestHeader("Connection","close");
        objXHR.send(xmlData);

        if(objXHR.status == 200)
        {

            // Processes the response from the CMS Authentication service.
            // If we recieve an "accountInfo" element back, we have successfully logged in.
            var accountInfo = objXHR.responseXML.getElementsByTagName("auth:accountInfo");

            if (accountInfo.length > 0)
            {   
                objXHR = new XMLHttpRequest(); //each request can only be opened once
                doReport();
            }
            else
            {
                //Otherwise, prompt error message
                alert("Error occurs when doing logon. Please check input credentials.");
                return;
            }
        }
        else
        {
            var url =  parent.settings.document.getElementById("serverURL").value + "/bi/v1/disp/rds/auth/logon";   
            alert(url);
            showError(objXHR.responseText);
            window.status = "Done";
        }
    }
    catch (e)
    {
        alert("Error occurs when doing logon.\r\n"+e);
    }

}

function showError(msg)
{
    var frag = document.getElementById("fragment");
    frag.innerHTML= "Complete";
    frag.style.visibility = "hidden";
    frag = document.getElementById("error")
    frag.innerHTML= msg;
    frag.style.visibility = "visible";
}

function doLogoff()
{
    try
    {
        objXHR.open("POST", parent.settings.document.getElementById("serverURL").value + "/bi/v1/disp/rds/auth/logoff", false);
        objXHR.send(null);
        checkLogoffStatus();
    }
    catch (e)
    {
        alert("Error occurs when doing logoff.\r\n"+e);
    }
}

function checkLogoffStatus()
{
    if(objXHR.readyState == 4 && objXHR.status == 200)
    {
        window.status = "Successfully log off IBM Cognos Service.";
        alert("Successfully log off IBM Cognos Service.");
    }
    else
    {
        var frag = document.getElementById("fragment");
        frag.innerHTML = objXHR.responseText;
        alert("HTTP ERROR " + objXHR.status + ": " + objXHR.statusText);
        window.status = "Done";
    }
}   

function callBack()
{
    if (objXHR.readyState == 4)
    {

        if (objXHR.status == 200)
        {
            var frag = document.getElementById("fragment");
            frag.innerHTML = objXHR.responseText;
            window.status = "Done";

        }
        else if (objXHR.status == 403)
        {
            doLogon();
        }
        else
        {
            alert("CALLBACK HTTP ERROR " + objXHR.status);
            window.status = "Done";
        }
    }
}


</script>
</head>
<body style="font-family: Verdana, Arial, Sans-Serif; font-weight: normal"><br />
<table align="center" width="95%"
    style="border-bottom-style: solid; border-top-style: solid; border-left-color: #004080; border-top-color: #004080; border-right-color: #004080; border-left-style: solid; border-right-style: solid; border-bottom-color: #004080; border-bottom-width: thin; border-right-width: thin; border-left-width: thin; border-top-width: thin">
    <thead>
        <tr>
            <td
                style="text-align: center; color: #FFFFFF; font-weight: bold; background-color: #0080C0">CMS
            Authentication Sample</td>
        </tr>
    </thead>
    <tr>
      <td>
        <table width="100%">
          <td align="left" width="60%">
            Name Space:&nbsp;&nbsp;<input type="text" size="40", id="nameSpace" value="PLACEHOLDER"><br><br>
            &nbsp;&nbsp;User Name:&nbsp;&nbsp;<input type="text" size="40", id="userName" value="PLACEHOLDER"><br><br>
            &nbsp;&nbsp;&nbsp;&nbsp;Password:&nbsp;&nbsp;<input type="password" size="40", id="password" value="PLACEHOLDER"><br><br>
            &nbsp;&nbsp;&nbsp;&nbsp;Report ID:&nbsp;&nbsp;<input size="40" id="storeID" value="PLACEHOLDER"><br><br>
          </td>
          <td align="left" width="40%">
            <input type="submit" value="Report Output" onClick="doReport()"><br><br>
            <input type="submit" id="logoff" value="Logoff from IBM Cognos Server" onclick="doLogoff()"><br>
          </td>
        </table>
      </td>
    </tr>
    <tr>
      <td
        style="border-top-style: dashed; border-top-color: #000000; border-top-width: medium; border-bottom-style: dashed; border-bottom-width: medium; border-bottom-color: #000000"
            height="100%">
        <DIV id="fragment" style="height: 100%; width: 100%">Your report will appear here</DIV>
      </td>
    </tr>
</table>
</body>
</html>




Aucun commentaire:

Enregistrer un commentaire