use of javax.swing.filechooser.FileFilter in project zaproxy by zaproxy.
the class ExtensionCompare method compareSessions.
private void compareSessions() {
JFileChooser chooser = new JFileChooser(Model.getSingleton().getOptionsParam().getUserDirectory());
File file = null;
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else if (file.isFile() && file.getName().endsWith(".session")) {
return true;
}
return false;
}
@Override
public String getDescription() {
return Constant.messages.getString("file.format.zap.session");
}
});
int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
if (rc == JFileChooser.APPROVE_OPTION) {
try {
file = chooser.getSelectedFile();
if (file == null) {
return;
}
Model cmpModel = new Model();
Session session = cmpModel.getSession();
//log.info("opening session file " + file.getAbsolutePath());
//WaitMessageDialog waitMessageDialog = View.getSingleton().getWaitMessageDialog("Loading session file. Please wait ...");
cmpModel.openSession(file, this);
// TODO support other implementations in the future
ParosDatabase db = new ParosDatabase();
db.setDatabaseParam(new DatabaseParam());
db.open(file.getAbsolutePath());
Map<String, String> curMap = new HashMap<>();
Map<String, String> cmpMap = new HashMap<>();
// Load the 2 sessions into 2 maps
this.buildHistoryMap(Model.getSingleton().getDb().getTableHistory(), curMap);
this.buildHistoryMap(db.getTableHistory(), cmpMap);
File outputFile = this.getOutputFile();
if (outputFile != null) {
// Write the result to the specified file
try {
TreeSet<String> sset = new TreeSet<>();
// Combine the keys for both maps
sset.addAll(curMap.keySet());
sset.addAll(cmpMap.keySet());
StringBuilder sb = new StringBuilder(500);
sb.append("<?xml version=\"1.0\"?>");
sb.append(CRLF);
sb.append("<report>");
sb.append(CRLF);
sb.append("<session-names>");
sb.append(CRLF);
sb.append("<session1>");
sb.append(Model.getSingleton().getSession().getSessionName());
sb.append("</session1>");
sb.append(CRLF);
sb.append("<session2>");
sb.append(session.getSessionName());
sb.append("</session2>");
sb.append(CRLF);
sb.append("</session-names>");
sb.append(CRLF);
Iterator<String> iter = sset.iterator();
while (iter.hasNext()) {
sb.append("<urlrow>");
sb.append(CRLF);
String key = iter.next();
String method = key.substring(0, key.indexOf(" "));
String url = key.substring(key.indexOf(" ") + 1);
sb.append("<method>");
sb.append(method);
sb.append("</method>");
sb.append(CRLF);
sb.append("<url>");
sb.append(url);
sb.append("</url>");
sb.append(CRLF);
sb.append("<code1>");
if (curMap.containsKey(key)) {
sb.append(curMap.get(key));
} else {
sb.append("---");
}
sb.append("</code1>");
sb.append(CRLF);
sb.append("<code2>");
if (cmpMap.containsKey(key)) {
sb.append(cmpMap.get(key));
} else {
sb.append("---");
}
sb.append("</code2>");
sb.append(CRLF);
sb.append("</urlrow>");
sb.append(CRLF);
}
sb.append("</report>");
sb.append(CRLF);
ReportGenerator.stringToHtml(sb.toString(), Constant.getZapInstall() + File.separator + "xml" + File.separator + "reportCompare.xsl", outputFile.getAbsolutePath());
try {
DesktopUtils.openUrlInBrowser(outputFile.toURI());
} catch (Exception e) {
log.error(e.getMessage(), e);
View.getSingleton().showMessageDialog(MessageFormat.format(Constant.messages.getString("report.complete.warning"), new Object[] { outputFile.getAbsolutePath() }));
}
} catch (Exception e1) {
log.warn(e1.getMessage(), e1);
}
}
//waitMessageDialog.setVisible(true);
} catch (Exception e) {
log.warn(e.getMessage(), e);
}
}
}
use of javax.swing.filechooser.FileFilter in project zaproxy by zaproxy.
the class PolicyManagerDialog method getImportButton.
private JButton getImportButton() {
if (this.importButton == null) {
this.importButton = new JButton(Constant.messages.getString("ascan.policymgr.button.import"));
this.importButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Default to ZAP home dir - we dont want to import/export to the policy dir
JFileChooser chooser = new JFileChooser(Constant.getZapHome());
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else if (file.isFile() && file.getName().endsWith(".policy")) {
return true;
}
return false;
}
@Override
public String getDescription() {
return Constant.messages.getString("file.format.zap.policy");
}
});
File file = null;
int rc = chooser.showOpenDialog(View.getSingleton().getMainFrame());
if (rc == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
if (file == null) {
return;
}
try {
extension.getPolicyManager().importPolicy(file);
policyNamesChanged();
} catch (ConfigurationException | IOException e1) {
logger.error(e1.getMessage(), e1);
View.getSingleton().showWarningDialog(Constant.messages.getString("ascan.policy.load.error"));
}
}
}
});
}
return this.importButton;
}
use of javax.swing.filechooser.FileFilter in project zaproxy by zaproxy.
the class OptionsCertificatePanel method browseButtonActionPerformed.
//GEN-LAST:event_addPkcs12ButtonActionPerformed
private void browseButtonActionPerformed(java.awt.event.ActionEvent evt) {
//GEN-FIRST:event_browseButtonActionPerformed
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter() {
@Override
public String getDescription() {
return Constant.messages.getString("options.cert.label.client.cert") + " (*.p12, *.pfx)";
}
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".p12") || f.getName().toLowerCase().endsWith(".pfx");
}
});
int state = fc.showOpenDialog(null);
if (state == JFileChooser.APPROVE_OPTION) {
fileTextField.setText(fc.getSelectedFile().toString());
}
}
use of javax.swing.filechooser.FileFilter in project zaproxy by zaproxy.
the class PopupMenuExportMessage method getOutputFile.
private File getOutputFile() {
JFileChooser chooser = new JFileChooser(extension.getModel().getOptionsParam().getUserDirectory());
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else if (file.isFile() && file.getName().endsWith(".txt")) {
return true;
}
return false;
}
@Override
public String getDescription() {
return Constant.messages.getString("file.format.ascii");
}
});
File file = null;
int rc = chooser.showSaveDialog(extension.getView().getMainFrame());
if (rc == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
if (file == null) {
return file;
}
extension.getModel().getOptionsParam().setUserDirectory(chooser.getCurrentDirectory());
String fileName = file.getAbsolutePath();
if (!fileName.endsWith(".txt")) {
fileName += ".txt";
file = new File(fileName);
}
return file;
}
return file;
}
use of javax.swing.filechooser.FileFilter in project zaproxy by zaproxy.
the class ReportLastScan method generateReport.
/**
* Generates a report. Defaults to HTML report if reportType is null.
* @param view
* @param model
* @param reportType
*/
public void generateReport(ViewDelegate view, Model model, ReportType reportType) {
// ZAP: Allow scan report file name to be specified
final ReportType localReportType;
if (reportType == null) {
localReportType = ReportType.HTML;
} else {
localReportType = reportType;
}
try {
JFileChooser chooser = new WritableFileChooser(Model.getSingleton().getOptionsParam().getUserDirectory());
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else if (file.isFile()) {
String lcFileName = file.getName().toLowerCase(Locale.ROOT);
switch(localReportType) {
case XML:
return lcFileName.endsWith(XML_FILE_EXTENSION);
case MD:
return lcFileName.endsWith(MD_FILE_EXTENSION);
case HTML:
default:
return (lcFileName.endsWith(HTM_FILE_EXTENSION) || lcFileName.endsWith(HTML_FILE_EXTENSION));
}
}
return false;
}
@Override
public String getDescription() {
switch(localReportType) {
case XML:
return Constant.messages.getString("file.format.xml");
case MD:
return Constant.messages.getString("file.format.md");
case HTML:
default:
return Constant.messages.getString("file.format.html");
}
}
});
String reportXSL = "";
String fileExtension = "";
switch(localReportType) {
case XML:
fileExtension = XML_FILE_EXTENSION;
// Dont use XSLT
reportXSL = null;
break;
case MD:
fileExtension = MD_FILE_EXTENSION;
reportXSL = (Constant.getZapInstall() + "/xml/report.md.xsl");
break;
case HTML:
default:
fileExtension = HTML_FILE_EXTENSION;
reportXSL = (Constant.getZapInstall() + "/xml/report.html.xsl");
break;
}
//Default the filename to a reasonable extension;
chooser.setSelectedFile(new File(fileExtension));
int rc = chooser.showSaveDialog(View.getSingleton().getMainFrame());
File file = null;
if (rc == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
File report = generate(file.getAbsolutePath(), model, reportXSL);
if (report == null) {
view.showMessageDialog(MessageFormat.format(Constant.messages.getString("report.unknown.error"), new Object[] { file.getAbsolutePath() }));
return;
}
try {
DesktopUtils.openUrlInBrowser(report.toURI());
} catch (Exception e) {
logger.error(e.getMessage(), e);
view.showMessageDialog(MessageFormat.format(Constant.messages.getString("report.complete.warning"), new Object[] { report.getAbsolutePath() }));
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
view.showWarningDialog(Constant.messages.getString("report.unexpected.error"));
}
}
Aggregations