Tableau与Web项目的集成

时间:2017-07-25 06:38:55

标签: javascript java tableau tableau-server tableau-online

我正在使用java脚本api与Web项目进行Tableau集成。我已经使用commnad在tabau服务器中配置了我的ip:tabadmin set wgserver.trusted_hosts""和相应的命令。但我无法获得票证,最终以-1结束。我已经遵循了所有配置步骤。

 public class TableauServlet extends javax.servlet.http.HttpServlet {
private static final long serialVersionUID = 1L;

public TableauServlet() {
    super();
}       

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final String user = "raghu";
    final String wgserver = "103.xxx.xxx.xx";
    final String dst = "views/Regional/College?:iid=1";
    final String params = ":embed=yes&:toolbar=yes";

    String ticket = getTrustedTicket(wgserver, user, request.getRemoteAddr());

    if ( !ticket.equals("-1") ) {
        response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
        response.setHeader("Location", "http://" + wgserver + "/trusted/" + ticket + "/" + dst + "?" + params);
    }
    else
        // handle error
        throw new ServletException("Invalid ticket " + ticket);
}   

// 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) 
    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"));

        // 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) {}
    }
}

}

1 个答案:

答案 0 :(得分:0)

我认为您在URL中缺少'target_site'参数,您将获得受信任的票证,如果您的默认站点中没有“views / Regional / College”,则需要该参数。

我对'-1'票也感到非常沮丧! 您可能尝试的一件事是在将Web服务器IP添加到tableau的trusted_hosts后重新启动tableau服务器。

我们最终做的另一件事是将web服务器的内部ip和外部ip添加到tableau上的trusted_hosts。由于您使用103.xxx.xxx.xx作为您的tableau服务器,我假设两台服务器都位于同一内部网络上。如果其他一切都失败了,你可以试试。

我的代码与您的代码几乎完全相同,并且工作正常。因此,如果问题仍然存在,则必须与配置相关。 这是我的代码:

private String getAuthenticationTicket(String tableauServerUserName,String tableauServerUrl, String targetSite) {

        OutputStreamWriter out = null;
        BufferedReader in = null;
        try {
            StringBuffer data = new StringBuffer();
            data.append(URLEncoder.encode("username", Constant.UTF_8));
            data.append("=");
            data.append(URLEncoder.encode(tableauServerUserName, Constant.UTF_8));
            data.append("&");
            data.append(URLEncoder.encode("target_site", Constant.UTF_8));
            data.append("=");
            data.append(URLEncoder.encode(targetSite, Constant.UTF_8));
            URL url = new URL(tableauServerUrl + "/" + "trusted");

            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            out = new OutputStreamWriter(conn.getOutputStream());
            out.write(data.toString());
            out.flush();
            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 ex) {
            //log stuff, handle error
            return null;
        } finally {
            try {
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
            } catch (IOException ex) {
               //log stuff, handle error
            }
        }
    }