2017-07-25 195 views
0

我正在使用Java腳本API來與Web項目進行Tableau集成。我已經使用commnad:tabadmin set wgserver.trusted_hosts「」和相應的命令在Tableau Server中配置了我的IP。但是我無法獲得票證,最終以-1結尾。我遵循了所有配置步驟。Tableau與Web項目的集成

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

}

回答

0

我想在您的網址你缺少「target_site」參數,你去信任票,其需要的,如果你沒有在您的默認網站的意見/區域/學院「 。

我也經歷了很多'-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 
      } 
     } 
    } 
+0

感謝您的回覆@merawalaid。我已經設置了目標參數,即時獲取HTML頁面作爲像{<!DOCTYPE html>票證的票證Tableau Server登錄}現在的問題是org.apache.coyote.http11.HeadersTooLargeException:試圖將更多的數據寫入響應頭中,而不是緩衝區中有可用空間。增加連接器上的maxHttpHeaderSize或將更少的數據寫入響應頭。 – veera

+0

Humm ...所以它看起來像Tableau服務器沒有給您一個可信的令牌,並且正試圖將您重定向到它的登錄頁面。如果我在你的地方,我現在只是擺脫'client_ip',直到我有基本的令牌機制工作。另外,我會檢查用戶'raghu'是否具有在tableau服務器上配置到您指定的'target_site'的訪問權限。從代碼的角度來看,我無法找到你在做什麼錯,我有非常類似的代碼工作。所以這個問題一定是因爲一些配置問題。 – merawalaid

+0

我忘了提到的一件事是tableau服務器是在其他一些網絡上,而web服務器是在我的local.Im試圖從我的本地訪問tableau服務器。是否有任何重定向設施? – veera