Skip to content

Commit

Permalink
Add import from exported json button & the import function logic
Browse files Browse the repository at this point in the history
  • Loading branch information
blackphreak authored Oct 18, 2023
1 parent f23e7c5 commit 0497c55
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.nccgroup.loggerplusplus.logview.processor.EntryImportWorker;
import lombok.extern.log4j.Log4j2;
import org.apache.logging.log4j.util.Base64Util;
import com.google.gson.Gson;
import com.google.gson.JsonElement;

import javax.swing.*;
import java.io.BufferedReader;
Expand Down Expand Up @@ -199,6 +201,58 @@ public static ArrayList<HttpRequestResponse> importZAP() {
return requests;
}

public static ArrayList<HttpRequestResponse> importFromExportedJson() {
ArrayList<HttpRequestResponse> requests = new ArrayList<>();

String filename = getLoadFile();
if ( filename.length() == 0 ) { // exit if no file selected
return new ArrayList<>();
}

try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException e) {
log.error("LoggerImport-readFile: Error Opening File " + filename);
return new ArrayList<>();
}

// declare all required variables for re-use in runtime
JsonArray arr = gson.fromJson(reader, JsonElement.class).getAsJsonArray();
Base64Utils b64Decoder = LoggerPlusPlus.montoya.utilities().base64Utils();
JsonObject obj, req, res;
HttpService httpService;
HttpRequest httpRequest;
HttpResponse httpResponse;
HttpRequestResponse requestResponse;
String url;
String[] v = new String[2];

Iterator<JsonElement> i = arr.iterator();
while (i.hasNext()) {
obj = i.next().getAsJsonObject();
req = obj.getAsJsonObject("Request");
res = obj.getAsJsonObject("Response");

url = req.getAsString("URL");
v[0] = req.getAsString("AsBase64");
v[1] = res.getAsString("AsBase64");

try {
httpService = HttpService.httpService(url);
httpRequest = HttpRequest.httpRequest(httpService, b64Decoder.decode(v[0], Base64DecodingOptions.URL));
httpResponse = HttpResponse.httpResponse(b64Decoder.decode(v[1], Base64DecodingOptions.URL));
requestResponse = HttpRequestResponse.httpRequestResponse(httpRequest, httpResponse);

requests.add(requestResponse);
} catch (Exception e) {
log.error("LoggerImport-importWStalker: Error Parsing Content");
return new ArrayList<>();
}
}

return requests;
}

public static boolean loadImported(ArrayList<HttpRequestResponse> requests, Boolean sendToAutoExporters) {
EntryImportWorker importWorker = LoggerPlusPlus.instance.getLogProcessor().createEntryImportBuilder()
.setOriginatingTool(ToolType.EXTENSIONS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ public void actionPerformed(ActionEvent e) {
}
}));

importGroup.add(new JButton(new AbstractAction("Import From Exported JSON") {
@Override
public void actionPerformed(ActionEvent e) {
ArrayList<HttpRequestResponse> requests = LoggerImport.importFromExportedJson();
if (LoggerPlusPlus.instance.getExportController().getEnabledExporters().size() > 0) {
int res = JOptionPane.showConfirmDialog(LoggerPlusPlus.instance.getLoggerFrame(),
"One or more auto-exporters are currently enabled. " +
"Do you want the imported entries to also be sent to the auto-exporters?",
"Auto-exporters Enabled", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
LoggerImport.loadImported(requests, res == JOptionPane.YES_OPTION);
} else {
LoggerImport.loadImported(requests, false);
}
}
}));

ComponentGroup exportGroup = new ComponentGroup(Orientation.HORIZONTAL);
HashMap<Class<? extends LogExporter>, LogExporter> exporters = LoggerPlusPlus.instance
.getExportController().getExporters();
Expand Down

0 comments on commit 0497c55

Please sign in to comment.