Search in sources :

Example 1 with ParosDatabase

use of org.parosproxy.paros.db.paros.ParosDatabase in project zaproxy by zaproxy.

the class Model method init.

public void init(ControlOverrides overrides) throws SAXException, IOException, Exception {
    getOptionsParam().load(Constant.getInstance().FILE_CONFIG, overrides);
    if (overrides.isExperimentalDb()) {
        logger.info("Using experimental database :/");
        db = DbSQL.getSingleton().initDatabase();
    } else {
        ParosDatabase parosDb = new ParosDatabase();
        parosDb.setDatabaseParam(getOptionsParam().getDatabaseParam());
        db = parosDb;
    }
    createAndOpenUntitledDb();
    HistoryReference.setTableHistory(getDb().getTableHistory());
    HistoryReference.setTableTag(getDb().getTableTag());
    HistoryReference.setTableAlert(getDb().getTableAlert());
}
Also used : ParosDatabase(org.parosproxy.paros.db.paros.ParosDatabase)

Example 2 with ParosDatabase

use of org.parosproxy.paros.db.paros.ParosDatabase 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(getView().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 =
            // getView().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);
                    String fileName = "reportCompare.xsl";
                    Path xslFile = Paths.get(Constant.getZapInstall(), "xml", fileName);
                    if (Files.exists(xslFile)) {
                        stringToHtml(sb.toString(), xslFile.toString(), outputFile.getAbsolutePath());
                    } else {
                        String path = "/org/zaproxy/zap/resources/xml/" + fileName;
                        try (InputStream is = ExtensionCompare.class.getResourceAsStream(path)) {
                            if (is == null) {
                                log.error("Bundled file not found: " + path);
                                return;
                            }
                            stringToHtml(sb.toString(), new StreamSource(is), outputFile.getAbsolutePath());
                        }
                    }
                    if (Files.notExists(outputFile.toPath())) {
                        log.info("Not opening report, does not exist: " + outputFile);
                        return;
                    }
                    try {
                        DesktopUtils.openUrlInBrowser(outputFile.toURI());
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                        getView().showMessageDialog(Constant.messages.getString("report.complete.warning", outputFile.getAbsolutePath()));
                    }
                } catch (Exception e1) {
                    log.warn(e1.getMessage(), e1);
                }
            }
        } catch (Exception e) {
            log.warn(e.getMessage(), e);
        }
    }
}
Also used : Path(java.nio.file.Path) DatabaseParam(org.parosproxy.paros.extension.option.DatabaseParam) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) HttpMalformedHeaderException(org.parosproxy.paros.network.HttpMalformedHeaderException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) DatabaseException(org.parosproxy.paros.db.DatabaseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ParosDatabase(org.parosproxy.paros.db.paros.ParosDatabase) JFileChooser(javax.swing.JFileChooser) TreeSet(java.util.TreeSet) Model(org.parosproxy.paros.model.Model) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) Session(org.parosproxy.paros.model.Session)

Aggregations

ParosDatabase (org.parosproxy.paros.db.paros.ParosDatabase)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 TreeSet (java.util.TreeSet)1 JFileChooser (javax.swing.JFileChooser)1 FileFilter (javax.swing.filechooser.FileFilter)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 StreamSource (javax.xml.transform.stream.StreamSource)1 DatabaseException (org.parosproxy.paros.db.DatabaseException)1 DatabaseParam (org.parosproxy.paros.extension.option.DatabaseParam)1 Model (org.parosproxy.paros.model.Model)1 Session (org.parosproxy.paros.model.Session)1 HttpMalformedHeaderException (org.parosproxy.paros.network.HttpMalformedHeaderException)1 SAXException (org.xml.sax.SAXException)1