Example usage for java.awt EventQueue isDispatchThread

List of usage examples for java.awt EventQueue isDispatchThread

Introduction

In this page you can find the example usage for java.awt EventQueue isDispatchThread.

Prototype

public static boolean isDispatchThread() 

Source Link

Document

Returns true if the calling thread is Toolkit#getSystemEventQueue the current AWT EventQueue 's dispatch thread.

Usage

From source file:org.parosproxy.paros.extension.history.ProxyListenerLog.java

private void addHistory(HttpMessage msg, int type) {

    HistoryReference historyRef = null;/*from   www.j  a v a 2 s . co  m*/

    try {
        historyRef = new HistoryReference(model.getSession(), type, msg);
    } catch (Exception e) {
        return;
    }

    if (type != HistoryReference.TYPE_MANUAL && type != HistoryReference.TYPE_HIDDEN) {
        return;
    }

    // add history to list (log panel).  Must use event queue because this proxylistener may not be run from event queue.
    synchronized (historyList) {
        if (type == HistoryReference.TYPE_MANUAL) {

            if (pattern == null) {
                addHistoryInEventQueue(historyRef);
            } else {
                StringBuffer sb = new StringBuffer();
                sb.append(msg.getRequestHeader().toString());
                sb.append(msg.getRequestBody().toString());
                if (!msg.getResponseHeader().isEmpty()) {
                    sb.append(msg.getResponseHeader().toString());
                    sb.append(msg.getResponseBody().toString());

                }
                if (pattern.matcher(sb.toString()).find()) {
                    addHistoryInEventQueue(historyRef);
                }
            }
        }
    }

    // add history to site panel.  Must use event queue because this proxylistener may not be run from event queue.
    final HistoryReference ref = historyRef;
    final HttpMessage finalMsg = msg;
    if (EventQueue.isDispatchThread()) {
        model.getSession().getSiteTree().addPath(ref, msg);
        if (isFirstAccess) {
            isFirstAccess = false;
            view.getSiteTreePanel().expandRoot();
        }

    } else {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    model.getSession().getSiteTree().addPath(ref, finalMsg);
                    if (isFirstAccess) {
                        isFirstAccess = false;
                        view.getSiteTreePanel().expandRoot();
                    }
                }
            });
        } catch (Exception e) {
        }

    }
}

From source file:org.parosproxy.paros.extension.history.ProxyListenerLog.java

private void addHistoryInEventQueue(final HistoryReference ref) {
    if (EventQueue.isDispatchThread()) {
        historyList.addElement(ref);/*ww  w  .j  a  va2 s. co  m*/
    } else {
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    historyList.addElement(ref);
                }

            });
        } catch (Exception e) {
        }
    }
}

From source file:org.parosproxy.paros.extension.manualrequest.http.impl.HttpPanelSender.java

private void persistAndShowMessage(HttpMessage httpMessage) {
    if (!EventQueue.isDispatchThread()) {
        EventQueue.invokeLater(() -> persistAndShowMessage(httpMessage));
        return;/*from  w  w w.j  a  va  2  s.co m*/
    }

    try {
        Session session = Model.getSingleton().getSession();
        HistoryReference ref = new HistoryReference(session, HistoryReference.TYPE_ZAP_USER, httpMessage);
        final ExtensionHistory extHistory = getHistoryExtension();
        if (extHistory != null) {
            extHistory.addHistory(ref);
        }
        SessionStructure.addPath(Model.getSingleton().getSession(), ref, httpMessage);
    } catch (HttpMalformedHeaderException | DatabaseException e) {
        logger.warn("Failed to persist message sent:", e);
    }
}

From source file:org.parosproxy.paros.extension.scanner.ExtensionScanner.java

/**
 * This method initializes menuScanner   
 *    //from w  w w.j  a v a 2s  . c o  m
 * @return javax.swing.JMenu   
 */
//   private JMenu getMenuScanner() {
//      if (menuScanner == null) {
//         menuScanner = new JMenu();
//         menuScanner.setText("Scanner");
//         menuScanner.add(getMenuItemScanAll());
//         menuScanner.add(getMenuItemScan());
//         menuScanner.addSeparator();
//         menuScanner.add(getMenuItemPolicy());
//      }
//      return menuScanner;
//   }

public void scannerComplete() {
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }

    final long scanTime = System.currentTimeMillis() - startTime;

    if (getView() != null) {
        getMenuItemScanAll().setEnabled(true);
        getMenuItemScan().setEnabled(true);
        getMenuItemPolicy().setEnabled(true);
        popupMenuScanHistory.setEnabled(true);

    }

    if (getView() != null && progressDialog != null) {
        if (EventQueue.isDispatchThread()) {
            progressDialog.dispose();
            progressDialog = null;
            getView().showMessageDialog("Scanning completed in " + scanTime / 1000
                    + "s.  The result can be obtained from Report>Last Scan Result.");
            return;
        }
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    progressDialog.dispose();
                    progressDialog = null;
                    getView().showMessageDialog("Scanning completed in " + scanTime / 1000
                            + "s.  The result can be obtained from Report>Last Scan Result.");
                }
            });
        } catch (Exception e) {
        }
    }

}

From source file:org.parosproxy.paros.extension.spider.ExtensionSpider.java

public void spiderComplete() {

    try {// w  w  w .j  a v  a2  s  . c o m
        getModel().getDb().getTableHistory().deleteHistoryType(getModel().getSession().getSessionId(),
                HistoryReference.TYPE_SPIDER_SEED);
        getModel().getDb().getTableHistory().deleteHistoryType(getModel().getSession().getSessionId(),
                HistoryReference.TYPE_SPIDER_VISITED);

    } catch (SQLException e) {
        e.printStackTrace();
    }

    if (getView() != null) {
        getMenuItemSpider().setEnabled(true);
        getPopupMenuSpider().setEnabled(true);

    }

    try {
        Thread.sleep(3000);
    } catch (Exception e) {
    }

    if (getView() != null && dialog != null) {
        if (EventQueue.isDispatchThread()) {
            dialog.dispose();
            return;
        }
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    dialog.dispose();
                }
            });
        } catch (Exception e) {
        }
    }
}

From source file:org.parosproxy.paros.extension.spider.ExtensionSpider.java

public void spiderProgress(final URI uri, final int percentageComplete, final int numberCrawled,
        final int numberToCrawl) {
    String uriString = "";

    if (dialog != null) {
        if (EventQueue.isDispatchThread()) {
            dialog.getTxtNumCrawled().setText(Integer.toString(numberCrawled));
            dialog.getTxtOutstandingCrawl().setText(Integer.toString(numberToCrawl));

            dialog.getProgressBar().setValue(percentageComplete);
            uriString = "";
            if (uri != null) {
                uriString = uri.toString();
            }//from   w  ww.jav  a 2  s .  c  o  m
            dialog.getTxtDisplay().setText(uriString);
            //dialog.getTxtDisplay().setCaretPosition(0);

            return;
        }
        try {
            EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    String uriString = "";
                    dialog.getTxtNumCrawled().setText(Integer.toString(numberCrawled));
                    dialog.getTxtOutstandingCrawl().setText(Integer.toString(numberToCrawl));

                    dialog.getProgressBar().setValue(percentageComplete);
                    if (uri != null) {
                        uriString = uri.toString();
                    }
                    dialog.getTxtDisplay().setText(uriString);
                    //dialog.getTxtDisplay().setCaretPosition(0);

                }
            });
        } catch (Exception e) {
        }

    }

}

From source file:org.parosproxy.paros.model.Session.java

private void refreshScope() {
    // log.debug("refreshScope");
    if (Constant.isLowMemoryOptionSet()) {
        // Nothing to do
        return;//  www .  jav  a 2  s  .c o  m
    }

    if (EventQueue.isDispatchThread()) {
        refreshScope((SiteNode) siteTree.getRoot());
        Control.getSingleton().sessionScopeChanged();
    } else {
        try {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    refreshScope((SiteNode) siteTree.getRoot());
                    Control.getSingleton().sessionScopeChanged();
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.parosproxy.paros.model.SiteMap.java

/**
 * Add the HistoryReference with the corresponding HttpMessage into the SiteMap.
 * This method saves the msg to be read from the reference table.  Use 
 * this method if the HttpMessage is known.
 * Note that this method must only be called on the EventDispatchThread
 * @param msg// ww w  .  j a  v  a2  s.  c  o m
 * @return 
 */
public SiteNode addPath(HistoryReference ref, HttpMessage msg) {
    if (Constant.isLowMemoryOptionSet()) {
        throw new InvalidParameterException("SiteMap should not be accessed when the low memory option is set");
    }

    if (View.isInitialised() && Constant.isDevBuild() && !EventQueue.isDispatchThread()) {
        // In developer mode log an error if we're not on the EDT
        // Adding to the site tree on GUI ('initial') threads causes problems
        log.error("SiteMap.addPath not on EDT " + Thread.currentThread().getName(), new Exception());
    }

    URI uri = msg.getRequestHeader().getURI();
    log.debug("addPath " + uri.toString());

    SiteNode parent = (SiteNode) getRoot();
    SiteNode leaf = null;
    String folder = "";

    try {

        String host = getHostName(uri);

        // add host
        parent = findAndAddChild(parent, host, ref, msg);

        List<String> path = model.getSession().getTreePath(msg);
        for (int i = 0; i < path.size(); i++) {
            folder = path.get(i);
            if (folder != null && !folder.equals("")) {
                if (i == path.size() - 1) {
                    leaf = findAndAddLeaf(parent, folder, ref, msg);
                    ref.setSiteNode(leaf);
                } else {
                    parent = findAndAddChild(parent, folder, ref, msg);
                }
            }
        }
        if (leaf == null) {
            // No leaf found, which means the parent was really the leaf
            // The parent will have been added with a 'blank' href, so replace it with the real one
            parent.setHistoryReference(ref);
            leaf = parent;
        }

    } catch (Exception e) {
        // ZAP: Added error
        log.error("Exception adding " + uri.toString() + " " + e.getMessage(), e);
    }

    if (hrefMap.get(ref.getHistoryId()) == null) {
        hrefMap.put(ref.getHistoryId(), leaf);
    }

    return leaf;
}

From source file:org.parosproxy.paros.model.SiteNode.java

private void nodeChanged() {
    if (this.siteMap == null) {
        return;//from w  ww  . ja  v  a  2s  . co m
    }
    if (EventQueue.isDispatchThread()) {
        nodeChangedEventHandler();
    } else {
        try {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    nodeChangedEventHandler();
                }
            });
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

From source file:org.parosproxy.paros.view.OutputPanel.java

public void append(final String msg) {
    if (EventQueue.isDispatchThread()) {
        doAppend(msg);/*from   www  .j  a  v a  2 s  .  co m*/
        return;
    }
    try {
        EventQueue.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                doAppend(msg);
            }
        });
    } catch (Exception e) {
        // ZAP: Added logging.
        logger.error(e.getMessage(), e);
    }
}