Ich habe jetzt einen etwas umständlichen Weg per BeanShell Skript gefunden, der aber auf meinen Anwendungsfall passt.
Dabei nutze ich den Textkanal der Previewseite der Datenquelle / Tabellenvorlage und lade den aktuellen Inhalt über die die generierte PreviewUrl über einen HttpRequest. Bei meinen ersten Tests hatte das Speichern des CSV Ausgabe soweit ganz gut funktioniert. Falls jemand noch eine einfacherer Möglichkeit kennt oder Verbesserungsvorschläge hat, kann dies gern ergänzen.
#!Beanshell
import ...
private static final int BUFFER_SIZE = 4096;
int amountOfResets = 0;
String message = "";
String printtext = "";
String strLanguage = java.util.Locale.getDefault().getLanguage();
String strError = "There was an exception writing file\n";
String strErrorDE = "Es gab einen Fehler bei der Verarbeitung.\n";
String strSuccess = "File written successfully.";
String strSuccessDE = "Datei wurde erfolgreich geschrieben";
public void downloadFile(String fileURL, String filepath)
throws IOException {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
context.logDebug("Content-Type = " + contentType);
context.logDebug("Content-Disposition = " + disposition);
context.logDebug("Content-Length = " + contentLength);
context.logDebug("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = filepath;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
context.logDebug("File downloaded");
} else {
context.logDebug("No file to download. Server replied HTTP code: " + responseCode);
}
if (strLanguage.equals("de")) print(strSuccessDE);
else print(strSuccess);
httpConn.disconnect();
}
void print(Object o) {
if (o == null)
printtext += "NULL" + " ";
else
printtext += o.toString() + " ";
}
void printType(Object o) {
if (o == null)
printtext += "NULL" + " ";
else
printtext += o.getClass() + " ";
}
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
FileFilter filter = new FileFilter() {
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
return f.getName().toLowerCase().endsWith(".csv");
}
public String getDescription() {
if(strLanguage.equals("de")) return "CSV Datei";
else return "CSV file";
}
};
chooser.setFileFilter(filter);
int returnVal = chooser.showSaveDialog(null);
if(returnVal != JFileChooser.APPROVE_OPTION) {
if(strLanguage.equals("de")) print("Abgebrochen.");
else print("Cancelled.");
} else {
File outputFile = chooser.getSelectedFile();
String absolutePath = outputFile.getAbsolutePath();
if (!outputFile.getName().toLowerCase().endsWith(".csv")) {
absolutePath = outputFile.getAbsolutePath() + ".csv";
}
String DELIMITER = ";";
try {
StoreElement dsElement = context.getElement();
Content2 c2 = (Content2) dsElement;
TableTemplate tblTemplate = c2.getTemplate();
PageRef ref = tblTemplate.getPreviewPageRef();
Connection con = context.getConnection();
String protocol = "http://";
String host = con.getHost();
int port = con.getPort();
String ticket = con.createTicket(false);
Language mLang = context.getProject().getMasterLanguage();
TemplateSet tempSet = null;
for (TemplateSet set : context.getProject().getTemplateSets()) {
if (set.getUid().equals("TEXT")) tempSet = set;
}
context.logInfo("Template Set: " + tempSet);
String url = protocol +
host +
ref.getPreviewUrl(mLang, tempSet, false, Previewable.PREVIEWMODE_FORCE_REFRESH, null) +
"/login.ticket=" +
ticket;
context.logInfo("Preview - URL: " + url);
downloadFile(url, absolutePath);
JOptionPane.showMessageDialog(null, printtext);
} catch (Exception e) {
// TODO Auto-generated catch block
context.logError("Error: " + e);
}
}