Trusted Authentication TABLEAU:
Step 1: Create a TableauAuthServlet Class
package com.tableau;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TableauAuthServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
// Read Property file and config data
Properties config = getConfigProps();
final String wgserver = config.getProperty("TableauServerURL");
final String params = config.getProperty("displayParam");
System.out.println("Tableau Server URL " + wgserver);
System.out.println("Tableau display Param " + params);
System.out.println("LDAP groups" + request.getHeader("groupList"));
// Read Parameters from url
String site = request.getParameter("s");
String workbook = request.getParameter("w");
String view = request.getParameter("v");
String groupName = request.getParameter("g");
System.out.println("site " + site);
System.out.println("workbook " + workbook);
System.out.println("view " + view);
String tabGroupUserID = getTabGroup(groupName, request.getHeader("groupList"));
String user = request.getHeader("uid");
System.out.println("user " + user);
// Get Trusted ticket from Tableau
final String dst1 = "t/" + site + "/views/" + workbook + "/" + view;
System.out.println("dst1 IS : " + dst1);
// String ticket = getTrustedTicket(wgserver, user,
// request.getRemoteAddr(),site);
String ticket = getTrustedTicket(wgserver, tabGroupUserID, request.getRemoteAddr(), site);
System.out.println("remoteAdd " + request.getRemoteAddr());
System.out.println("ticket " + ticket);
System.out.println("tabGroupUserID is: " + tabGroupUserID);
if (!ticket.equals("-1")) //&& (tabGroupUserID.equals("FinanceReport") || tabGroupUserID.equals("SepgReport")) )
{
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
// response.setHeader("Location", "http://" + wgserver +
// "/trusted/" + ticket + "/" + dst + "?" + params);
request.setAttribute("tabserverip", wgserver);
request.setAttribute("url", "trusted/" + ticket + "/" + dst1);
request.setAttribute("userid", request.getHeader("uid"));
request.setAttribute("groupid", getTabGroup(groupName, request.getHeader("groupList")));
// response.sendRedirect("http://" + wgserver + "/trusted/" +
// ticket + "/" + dst1 + "?" + params);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/report.jsp");
dispatcher.forward(request, response);
}
else
{
// handle error
// throw new ServletException("Invalid ticket " + ticket);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/norights.jsp");
dispatcher.forward(request, response);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
// the client_ip parameter isn't necessary to send in the POST unless you
// have
// wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
private String getTrustedTicket(String wgserver, String user, String remoteAddr, String sitename) throws ServletException
{
OutputStreamWriter out = null;
BufferedReader in = null;
try
{
// Encode the parameters
StringBuffer data = new StringBuffer();
data.append(URLEncoder.encode("username", "UTF-8"));
data.append("=");
data.append(URLEncoder.encode(user, "UTF-8"));
data.append("&");
data.append(URLEncoder.encode("client_ip", "UTF-8"));
data.append("=");
data.append(URLEncoder.encode(remoteAddr, "UTF-8"));
data.append("&");
data.append(URLEncoder.encode("target_site", "UTF-8"));
data.append("=");
data.append(URLEncoder.encode(sitename, "UTF-8"));
// Send the request
URL url = new URL("http://" + wgserver + "/trusted");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
out = new OutputStreamWriter(conn.getOutputStream());
out.write(data.toString());
out.flush();
// Read the response
StringBuffer rsp = new StringBuffer();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
rsp.append(line);
}
return rsp.toString();
}
catch (Exception e)
{
throw new ServletException(e);
}
finally
{
try
{
if (in != null)
in.close();
if (out != null)
out.close();
}
catch (IOException e)
{
}
}
}
public Properties getConfigProps()
{
// Reading properties file to set Default values
Properties config = new Properties();
try
{
config.load(new FileInputStream(getServletContext().getRealPath("/WEB-INF/conf/Tview.properties")));
}
catch (IOException e)
{
System.out.println("Exception in getBankConfigProps: " + e);
}
return config;
}
public String getTabGroup(String groupName, String ldapGroups)
{
String grps[] = ldapGroups.split("cn=");
String tmpGroup = null;
for (int i = 0; i < grps.length; i++)
{
if (grps[i] == null || grps[i].equals(""))
continue;
tmpGroup = grps[i].substring(0, grps[i].indexOf(","));
System.out.println(tmpGroup);
if (tmpGroup.equalsIgnoreCase(groupName))
{
System.out.println("Tableau Group of the user tmpGroup IS:" + tmpGroup);
return tmpGroup;
}
}
return "-";
}
}
Create Properties file :
WEB-INF/conf/Tview.properties location:
Properties file contains:
TableauServerURL=domain name or IPAdress
displayParam=:embed=yes&:toolbar=yes
Create Jsp File named as report.jsp:
<html>
<%
String svr=(String)request.getAttribute("tabserverip");
String repUrl=(String)request.getAttribute("url");
String userid=(String)request.getAttribute("userid");
String groupid=(String)request.getAttribute("groupid");
System.out.println("tabserverip is: "+svr);
System.out.println("url is: "+repUrl);
System.out.println("userid is: "+userid);
System.out.println("groupid is: "+groupid);
String jscriptUrl="https://" + svr + "/javascripts/api/viz_v1.js";
System.out.println("jscriptUrl is: "+jscriptUrl);
%>
<script type="text/javascript" src="<%=jscriptUrl%>"></script>
<object class="tableauViz" width="100%" height="100%" style="display:none;">
<param name="path" value="<%=repUrl%>" />
<param name="filter" value="InsightID=<%=userid%>"/>
</object>
</html>
Create JSP file named as norights.jsp:
<html>
<p>
<b><center><font name="ariel" size="3">You are not authorized.</font></center></b>
</p>
</html>
web.xml
<servlet>
<servlet-name>tview</servlet-name>
<servlet-class>
com.tview.TableauAuthServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tview</servlet-name>
<url-pattern>/tview</url-pattern>
</servlet-mapping>
Step 1: Create a TableauAuthServlet Class
package com.tableau;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TableauAuthServlet extends HttpServlet
{
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
// Read Property file and config data
Properties config = getConfigProps();
final String wgserver = config.getProperty("TableauServerURL");
final String params = config.getProperty("displayParam");
System.out.println("Tableau Server URL " + wgserver);
System.out.println("Tableau display Param " + params);
System.out.println("LDAP groups" + request.getHeader("groupList"));
// Read Parameters from url
String site = request.getParameter("s");
String workbook = request.getParameter("w");
String view = request.getParameter("v");
String groupName = request.getParameter("g");
System.out.println("site " + site);
System.out.println("workbook " + workbook);
System.out.println("view " + view);
String tabGroupUserID = getTabGroup(groupName, request.getHeader("groupList"));
String user = request.getHeader("uid");
System.out.println("user " + user);
// Get Trusted ticket from Tableau
final String dst1 = "t/" + site + "/views/" + workbook + "/" + view;
System.out.println("dst1 IS : " + dst1);
// String ticket = getTrustedTicket(wgserver, user,
// request.getRemoteAddr(),site);
String ticket = getTrustedTicket(wgserver, tabGroupUserID, request.getRemoteAddr(), site);
System.out.println("remoteAdd " + request.getRemoteAddr());
System.out.println("ticket " + ticket);
System.out.println("tabGroupUserID is: " + tabGroupUserID);
if (!ticket.equals("-1")) //&& (tabGroupUserID.equals("FinanceReport") || tabGroupUserID.equals("SepgReport")) )
{
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
// response.setHeader("Location", "http://" + wgserver +
// "/trusted/" + ticket + "/" + dst + "?" + params);
request.setAttribute("tabserverip", wgserver);
request.setAttribute("url", "trusted/" + ticket + "/" + dst1);
request.setAttribute("userid", request.getHeader("uid"));
request.setAttribute("groupid", getTabGroup(groupName, request.getHeader("groupList")));
// response.sendRedirect("http://" + wgserver + "/trusted/" +
// ticket + "/" + dst1 + "?" + params);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/report.jsp");
dispatcher.forward(request, response);
}
else
{
// handle error
// throw new ServletException("Invalid ticket " + ticket);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/norights.jsp");
dispatcher.forward(request, response);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
// the client_ip parameter isn't necessary to send in the POST unless you
// have
// wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
private String getTrustedTicket(String wgserver, String user, String remoteAddr, String sitename) throws ServletException
{
OutputStreamWriter out = null;
BufferedReader in = null;
try
{
// Encode the parameters
StringBuffer data = new StringBuffer();
data.append(URLEncoder.encode("username", "UTF-8"));
data.append("=");
data.append(URLEncoder.encode(user, "UTF-8"));
data.append("&");
data.append(URLEncoder.encode("client_ip", "UTF-8"));
data.append("=");
data.append(URLEncoder.encode(remoteAddr, "UTF-8"));
data.append("&");
data.append(URLEncoder.encode("target_site", "UTF-8"));
data.append("=");
data.append(URLEncoder.encode(sitename, "UTF-8"));
// Send the request
URL url = new URL("http://" + wgserver + "/trusted");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
out = new OutputStreamWriter(conn.getOutputStream());
out.write(data.toString());
out.flush();
// Read the response
StringBuffer rsp = new StringBuffer();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
rsp.append(line);
}
return rsp.toString();
}
catch (Exception e)
{
throw new ServletException(e);
}
finally
{
try
{
if (in != null)
in.close();
if (out != null)
out.close();
}
catch (IOException e)
{
}
}
}
public Properties getConfigProps()
{
// Reading properties file to set Default values
Properties config = new Properties();
try
{
config.load(new FileInputStream(getServletContext().getRealPath("/WEB-INF/conf/Tview.properties")));
}
catch (IOException e)
{
System.out.println("Exception in getBankConfigProps: " + e);
}
return config;
}
public String getTabGroup(String groupName, String ldapGroups)
{
String grps[] = ldapGroups.split("cn=");
String tmpGroup = null;
for (int i = 0; i < grps.length; i++)
{
if (grps[i] == null || grps[i].equals(""))
continue;
tmpGroup = grps[i].substring(0, grps[i].indexOf(","));
System.out.println(tmpGroup);
if (tmpGroup.equalsIgnoreCase(groupName))
{
System.out.println("Tableau Group of the user tmpGroup IS:" + tmpGroup);
return tmpGroup;
}
}
return "-";
}
}
Create Properties file :
WEB-INF/conf/Tview.properties location:
Properties file contains:
TableauServerURL=domain name or IPAdress
displayParam=:embed=yes&:toolbar=yes
Create Jsp File named as report.jsp:
<html>
<%
String svr=(String)request.getAttribute("tabserverip");
String repUrl=(String)request.getAttribute("url");
String userid=(String)request.getAttribute("userid");
String groupid=(String)request.getAttribute("groupid");
System.out.println("tabserverip is: "+svr);
System.out.println("url is: "+repUrl);
System.out.println("userid is: "+userid);
System.out.println("groupid is: "+groupid);
String jscriptUrl="https://" + svr + "/javascripts/api/viz_v1.js";
System.out.println("jscriptUrl is: "+jscriptUrl);
%>
<script type="text/javascript" src="<%=jscriptUrl%>"></script>
<object class="tableauViz" width="100%" height="100%" style="display:none;">
<param name="path" value="<%=repUrl%>" />
<param name="filter" value="InsightID=<%=userid%>"/>
</object>
</html>
Create JSP file named as norights.jsp:
<html>
<p>
<b><center><font name="ariel" size="3">You are not authorized.</font></center></b>
</p>
</html>
web.xml
<servlet>
<servlet-name>tview</servlet-name>
<servlet-class>
com.tview.TableauAuthServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>tview</servlet-name>
<url-pattern>/tview</url-pattern>
</servlet-mapping>
Thank you very much for the useful info.Could you please explain how to run Java code in order to get trusted ticket?
ReplyDeleteThanks for sharing this information.
ReplyDeleteTableau Soap Connection
Respect and that i have a keen present: Where To Start House Remodeling house renovations near me
ReplyDelete