Example #1
0
  private static void saveCookies() {
    if (cookie_jar != null
        && (!cookie_jar.exists() || cookie_jar.isFile() && cookie_jar.canWrite())) {
      Hashtable cookie_list = new Hashtable();
      Enumeration en =
          Util.getList(cookie_cntxt_list, HTTPConnection.getDefaultContext()).elements();

      // discard cookies which are not to be kept across sessions

      while (en.hasMoreElements()) {
        Cookie cookie = (Cookie) en.nextElement();
        if (!cookie.discard()) cookie_list.put(cookie, cookie);
      }

      // save any remaining cookies in jar
      if (cookie_list.size() > 0) {
        try {
          ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(cookie_jar));
          oos.writeObject(cookie_list);
          oos.close();
        } catch (IOException t) {
          if (LOG.isTraceEnabled()) {
            LOG.trace("An exception occurred: " + t.getMessage());
          }
        }
      }
    }
  }
Example #2
0
  /**
   * the method called by the DefaultCookiePolicyHandler.
   *
   * @return true if the cookie should be accepted
   */
  public synchronized boolean accept(Cookie cookie, DefaultCookiePolicyHandler h, String server) {
    // set the new values

    name_value_label.setText(cookie.getName() + "=" + cookie.getValue());
    domain_value.setText(cookie.getDomain());
    path_value.setText(cookie.getPath());
    if (cookie.expires() == null) expires_value.setText("never");
    else expires_value.setText(cookie.expires().toString());
    int pos = 2;
    if (cookie.isSecure()) add(secure_note, constr, pos++);
    if (cookie.discard()) add(discard_note, constr, pos++);

    if (cookie instanceof Cookie2) {
      Cookie2 cookie2 = (Cookie2) cookie;

      // set ports list
      if (cookie2.getPorts() != null) {
        ((GridLayout) left_panel.getLayout()).setRows(5);
        left_panel.add(ports_label, 2);
        ((GridLayout) right_panel.getLayout()).setRows(5);
        int[] ports = cookie2.getPorts();
        StringBuffer plist = new StringBuffer();
        plist.append(ports[0]);
        for (int idx = 1; idx < ports.length; idx++) {
          plist.append(", ");
          plist.append(ports[idx]);
        }
        ports_value.setText(plist.toString());
        right_panel.add(ports_value, 2);
      }

      // set comment url
      if (cookie2.getCommentURL() != null) {
        c_url_note.setText("For more info on this cookie see: " + cookie2.getCommentURL());
        add(c_url_note, constr, pos++);
      }

      // set comment
      if (cookie2.getComment() != null) {
        comment_value.setText(cookie2.getComment());
        add(comment_label, constr, pos++);
        add(comment_value, constr, pos++);
      }
    }

    // invalidate all labels, so that new values are displayed correctly

    name_value_label.invalidate();
    domain_value.invalidate();
    ports_value.invalidate();
    path_value.invalidate();
    expires_value.invalidate();
    left_panel.invalidate();
    right_panel.invalidate();
    secure_note.invalidate();
    discard_note.invalidate();
    c_url_note.invalidate();
    comment_value.invalidate();
    invalidate();

    // set default domain test

    domain.setText(cookie.getDomain());

    // display

    setResizable(true);
    pack();
    setResizable(false);
    setLocation(
        (screen.width - getPreferredSize().width) / 2,
        (int) ((screen.height - getPreferredSize().height) / 2 * .7));
    setVisible(true);
    default_focus.requestFocus();

    // wait for user input

    try {
      wait();
    } catch (InterruptedException e) {
    }

    setVisible(false);

    // reset popup

    remove(secure_note);
    remove(discard_note);
    left_panel.remove(ports_label);
    ((GridLayout) left_panel.getLayout()).setRows(4);
    right_panel.remove(ports_value);
    ((GridLayout) right_panel.getLayout()).setRows(4);
    remove(c_url_note);
    remove(comment_label);
    remove(comment_value);

    // handle accept/reject domain buttons

    if (accept_domain) {
      String dom = domain.getText().trim().toLowerCase();

      if (accept) h.addAcceptDomain(dom);
      else h.addRejectDomain(dom);
    }

    return accept;
  }