`import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
public class TableauWorkbookDownloader {
private static final String SERVER_URL = "https://your-tableau-server.com";
private static final String API_VERSION = "3.9";
private static final String USERNAME = "your_username";
private static final String PASSWORD = "your_password";
private static final String SITE_ID = ""; // Leave empty for the default site
public static void main(String[] args) {
String tag = "your_tag";
String token = authenticate();
if (token != null) {
Map<String, String> workbookMap = getWorkbooksByTag(token, tag);
if (!workbookMap.isEmpty()) {
workbookMap.forEach((workbookId, folderPath) -> downloadWorkbook(token, workbookId, folderPath));
} else {
System.out.println("No workbooks with the specified tag found.");
}
}
}
private static String authenticate() {
try (CloseableHttpClient client = HttpClients.createDefault()) {
URI uri = new URI(SERVER_URL + "/api/" + API_VERSION + "/auth/signin");
HttpPost post = new HttpPost(uri);
String json = new JSONObject()
.put("credentials", new JSONObject()
.put("name", USERNAME)
.put("password", PASSWORD)
.put("site", new JSONObject().put("contentUrl", SITE_ID)))
.toString();
post.setEntity(new StringEntity(json));
post.setHeader("Content-Type", "application/json");
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONObject jsonResponse = new JSONObject(responseString);
return jsonResponse.getJSONObject("credentials").getString("token");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private static Map<String, String> getWorkbooksByTag(String token, String tag) {
Map<String, String> workbookMap = new HashMap<>();
try (CloseableHttpClient client = HttpClients.createDefault()) {
URI uri = new URI(SERVER_URL + "/api/" + API_VERSION + "/sites/" + SITE_ID + "/workbooks?filter=tags:eq:" + tag);
HttpGet get = new HttpGet(uri);
get.setHeader("X-Tableau-Auth", token);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONObject jsonResponse = new JSONObject(responseString);
JSONArray workbooks = jsonResponse.getJSONObject("workbooks").getJSONArray("workbook");
for (int i = 0; i < workbooks.length(); i++) {
JSONObject workbook = workbooks.getJSONObject(i);
String workbookId = workbook.getString("id");
String projectId = workbook.getJSONObject("project").getString("id");
String folderPath = getProjectPath(token, projectId);
workbookMap.put(workbookId, folderPath);
}
} catch (Exception e) {
e.printStackTrace();
}
return workbookMap;
}
private static String getProjectPath(String token, String projectId) {
StringBuilder folderPath = new StringBuilder();
try (CloseableHttpClient client = HttpClients.createDefault()) {
while (projectId != null && !projectId.isEmpty()) {
URI uri = new URI(SERVER_URL + "/api/" + API_VERSION + "/sites/" + SITE_ID + "/projects/" + projectId);
HttpGet get = new HttpGet(uri);
get.setHeader("X-Tableau-Auth", token);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String responseString = EntityUtils.toString(entity);
JSONObject jsonResponse = new JSONObject(responseString);
JSONObject project = jsonResponse.getJSONObject("project");
folderPath.insert(0, File.separator + project.getString("name"));
projectId = project.optString("parentProjectId", null);
}
} catch (Exception e) {
e.printStackTrace();
}
return folderPath.toString();
}
private static void downloadWorkbook(String token, String workbookId, String folderPath) {
try (CloseableHttpClient client = HttpClients.createDefault()) {
URI uri = new URI(SERVER_URL + "/api/" + API_VERSION + "/sites/" + SITE_ID + "/workbooks/" + workbookId + "/content");
HttpGet get = new HttpGet(uri);
get.setHeader("X-Tableau-Auth", token);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
Files.createDirectories(Paths.get("." + folderPath));
String filePath = "." + folderPath + File.separator + workbookId + ".twb";
try (InputStream in = entity.getContent();
OutputStream out = new FileOutputStream(filePath)) {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
System.out.println("Workbook downloaded successfully to " + filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}`
Authentication: The authenticate method sends a POST request to Tableau’s sign-in endpoint with the username and password, and it returns the authentication token.
Finding Workbooks by Tag: The getWorkbooksByTag method sends a GET request to fetch workbooks filtered by the specified tag. It then calls getProjectPath to get the folder path for each workbook and stores the workbook IDs and their corresponding folder paths in a map.
Getting Project Path: The getProjectPath method recursively traverses the project hierarchy to build the full folder path for each project.
Downloading the Workbook: The downloadWorkbook method sends a GET request to download the workbook content. It creates the necessary directories and saves the workbook to the appropriate folder.
To download a workbook from Tableau using Java, you’ll need to interact with the Tableau Server REST API. The process involves authenticating with the server, finding the workbook by its tag, and then downloading it.
Here is an example Java code to achieve this:
Add necessary dependencies: You’ll need a JSON library like org.json and an HTTP client library like Apache HttpClient.
Code to authenticate, search by tag, and download the workbook: