Skip to content

Commit

Permalink
Agent Debugger Export Responses (CSV) (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-mcgoldrick authored Dec 3, 2024
1 parent 7fcb48f commit 301ebae
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,22 @@ public String getLogMsg() {
}
this.responseLogMsg = sb.toString();
} catch (Exception ex) {
LOG.error("Error processing response: " + ex.getMessage(), ex);
LOG.error("Error processing response: {}", ex.getMessage(), ex);
}
return responseLogMsg;
}

public String convertToCSV() {
StringBuilder sb = new StringBuilder();
sb.append(this.httpCode).append(",");
sb.append(this.rspMessage).append(",");
sb.append(responseTime).append(",");
sb.append(getResponseSize()).append(",");
headers.forEach((key, value) -> sb.append(key).append(" = ").append(value.replace(",", "")).append(","));
cookies.forEach((key, value) -> sb.append(key).append(" = ").append(value).append(","));
return sb.toString();
}

/**
* Common codes are 200 OK, 202 accepted, 204 no content, 400 bad request,
* 404 not found, 500 internal server error, 503 Service Unavailable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ private void createToolBar(JComboBox testPlanChooser, JComboBox<TankClientChoice
toolBar.add(new JLabel(" "));
toolBar.add(actions.getPauseAction());
toolBar.addSeparator();
toolBar.add(actions.getCSVAction());
toolBar.addSeparator();

toolBar.add(actions.getClearBookmarksAction());
toolBar.add(new JLabel(" "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class ActionProducer {
public static final String ACTION_STOP = "Stop";
public static final String ACTION_RUN_TO = "Run to Breakpoint...";
private static final String ACTION_PAUSE = "Pause";
private static final String ACTION_EXPORT = "Export Responses";
private static final String ACTION_FIND = "Find...";
private static final String ACTION_SKIP = "Skip";
private static final String ACTION_SKIP_STEP = "Toggle Skip Step";
Expand Down Expand Up @@ -106,20 +107,6 @@ public ActionProducer(AgentDebuggerFrame debuggerFrame, String serviceUrl, Strin
this.projectClient = new ProjectClient(serviceUrl, token);
this.agentClient = new AgentClient(serviceUrl, token);
this.dataFileClient = new DataFileClient(serviceUrl, token);

jFileChooser = new JFileChooser();
jFileChooser.setFileFilter(new FileFilter() {

@Override
public String getDescription() {
return "Agent XML Files";
}

@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith("_h.xml");
}
});
}

/**
Expand Down Expand Up @@ -264,6 +251,19 @@ public void actionPerformed(ActionEvent event) {
public Action getOpenAction() {
Action ret = actionMap.get(ACTION_OPEN);
if (ret == null) {
jFileChooser = new JFileChooser();
jFileChooser.setFileFilter(new FileFilter() {

@Override
public String getDescription() {
return "Agent XML Files";
}

@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith("_h.xml");
}
});
ret = new AbstractAction(ACTION_OPEN, getIcon("script_go.png", IconSize.SMALL)) {
private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -828,6 +828,32 @@ public void actionPerformed(ActionEvent e) {
return ret;
}

/**
*
* @return
*/
public Action getCSVAction() {
Action ret = actionMap.get(ACTION_EXPORT);
if (ret == null) {
jFileChooser = new JFileChooser();
ret = new AbstractAction(ACTION_EXPORT, getIcon("database_tabs.png", IconSize.SMALL)) {
private static final long serialVersionUID = 1L;

@Override
public void actionPerformed(ActionEvent e) {
int option = jFileChooser.showSaveDialog(debuggerFrame);
if (option != JFileChooser.CANCEL_OPTION) {
File selectedFile = jFileChooser.getSelectedFile();
debuggerFrame.exportCSV(selectedFile);
}
}
};
ret.putValue(Action.SHORT_DESCRIPTION, ACTION_EXPORT);
actionMap.put(ACTION_EXPORT, ret);
}
return ret;
}

/**
*
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -1015,4 +1012,16 @@ public RequestResponsePanel getRequestResponsePanel() {
return requestResponsePanel;
}

public void exportCSV(File csvOutputFile) {
try (PrintWriter pw = new PrintWriter(csvOutputFile)) {
pw.println("URL,HTTP Code,HTTP Msg,Response Time,Response Size,Headers...,Cookies...");
steps.stream()
.filter(step -> step.getResponse() != null)
.map(step -> step.getRequest().getRequestUrl() + "," + step.getResponse().convertToCSV())
.forEach(pw::println);
} catch ( FileNotFoundException e) {
LOG.error("Error exporting CSV: {}", String.valueOf(e));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static Component createContentPanel(final AgentDebuggerFrame frame) {
public void mousePressed(MouseEvent e) {
scriptEditorTA.grabFocus();
try {
int offs = scriptEditorTA.viewToModel(e.getPoint());
int offs = scriptEditorTA.viewToModel2D(e.getPoint());
if (offs > -1) {
int line = scriptEditorTA.getLineOfOffset(offs);
if (frame.getSteps().size() > line) {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 301ebae

Please sign in to comment.