コード例 #1
0
ファイル: Service.java プロジェクト: Nekit10/Wheel-Of-Fortune
  /**
   * Constructor.
   *
   * @param session Session object for this service
   * @param urlname URLName object to be used for this service
   */
  protected Service(Session session, URLName urlname) {
    this.session = session;
    debug = session.getDebug();
    url = urlname;

    /*
     * Initialize the URLName with default values.
     * The URLName will be updated when connect is called.
     */
    String protocol = null;
    String host = null;
    int port = -1;
    String user = null;
    String password = null;
    String file = null;

    // get whatever information we can from the URL
    // XXX - url should always be non-null here, Session
    //       passes it into the constructor
    if (url != null) {
      protocol = url.getProtocol();
      host = url.getHost();
      port = url.getPort();
      user = url.getUsername();
      password = url.getPassword();
      file = url.getFile();
    }

    // try to get protocol-specific default properties
    if (protocol != null) {
      if (host == null) host = session.getProperty("mail." + protocol + ".host");
      if (user == null) user = session.getProperty("mail." + protocol + ".user");
    }

    // try to get mail-wide default properties
    if (host == null) host = session.getProperty("mail.host");

    if (user == null) user = session.getProperty("mail.user");

    // try using the system username
    if (user == null) {
      try {
        user = System.getProperty("user.name");
      } catch (SecurityException sex) {
        // XXX - it's not worth creating a MailLogger just for this
        // logger.log(Level.CONFIG, "Can't get user.name property", sex);
      }
    }

    url = new URLName(protocol, host, port, file, user, password);

    // create or choose the appropriate event queue
    String scope = session.getProperties().getProperty("mail.event.scope", "folder");
    Executor executor = (Executor) session.getProperties().get("mail.event.executor");
    if (scope.equalsIgnoreCase("application")) q = EventQueue.getApplicationEventQueue(executor);
    else if (scope.equalsIgnoreCase("session")) q = session.getEventQueue();
    else // if (scope.equalsIgnoreCase("store") ||
      //     scope.equalsIgnoreCase("folder"))
      q = new EventQueue(executor);
  }
コード例 #2
0
  public static void dumpPart(Part p, biz.systempartners.claims.ClaimsViewer claimsViewer)
      throws Exception {
    if (p instanceof Message) dumpEnvelope((Message) p);

    /**
     * Dump input stream ..
     *
     * <p>InputStream is = p.getInputStream(); // If "is" is not already buffered, wrap a
     * BufferedInputStream // around it. if (!(is instanceof BufferedInputStream)) is = new
     * BufferedInputStream(is); int c; while ((c = is.read()) != -1) System.out.write(c);
     */
    String ct = p.getContentType();
    try {
      pr("CONTENT-TYPE: " + (new ContentType(ct)).toString());
    } catch (ParseException pex) {
      pr("BAD CONTENT-TYPE: " + ct);
    }
    String filename = p.getFileName();
    if (filename != null) pr("FILENAME: " + filename);

    /*
     * Using isMimeType to determine the content type avoids
     * fetching the actual content data until we need it.
     */
    if (p.isMimeType("text/plain")) {
      pr("This is plain text");
      pr("---------------------------");
      if (!showStructure && !saveAttachments) System.out.println((String) p.getContent());
    } else if (p.isMimeType("multipart/*")) {
      pr("This is a Multipart");
      pr("---------------------------");
      Multipart mp = (Multipart) p.getContent();
      level++;
      int count = mp.getCount();
      for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i), claimsViewer);
      level--;
    } else if (p.isMimeType("message/rfc822")) {
      pr("This is a Nested Message");
      pr("---------------------------");
      level++;
      dumpPart((Part) p.getContent(), claimsViewer);
      level--;
    } else {
      if (!showStructure && !saveAttachments) {
        /*
         * If we actually want to see the data, and it's not a
         * MIME type we know, fetch it and check its Java type.
         */
        Object o = p.getContent();
        if (o instanceof String) {
          pr("This is a string");
          pr("---------------------------");
          System.out.println((String) o);
        } else if (o instanceof InputStream) {
          pr("This is just an input stream");
          pr("---------------------------");
          InputStream is = (InputStream) o;
          int c;
          while ((c = is.read()) != -1) System.out.write(c);
        } else {
          pr("This is an unknown type");
          pr("---------------------------");
          pr(o.toString());
        }
      } else {
        // just a separator
        pr("---------------------------");
      }
    }

    /*
     * If we're saving attachments, write out anything that
     * looks like an attachment into an appropriately named
     * file.  Don't overwrite existing files to prevent
     * mistakes.
     */
    if (saveAttachments && level != 0 && !p.isMimeType("multipart/*")) {
      String disp = p.getDisposition();
      // many mailers don't include a Content-Disposition
      if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
        if (filename == null) filename = "Attachment" + attnum++;
        pr("Saving attachment to file " + filename);
        try {
          File f = new File(System.getProperty("user.dir"), filename);
          /*  if (f.exists())
          // XXX - could try a series of names
          throw new IOException("file exists");*/
          OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
          InputStream is = p.getInputStream();
          int c;
          while ((c = is.read()) != -1) os.write(c);
          os.close();
          if (p.isMimeType("text/xml") || p.isMimeType("application/octet-stream")) {
            processBrRequisitionFile(
                f, claimsViewer, claimsViewer.getInvoiceVector(), claimsViewer.getFilesVector());
          }
          System.out.println("I have saved file [" + f.getAbsolutePath() + "]");
        } catch (IOException ex) {
          pr("Failed to save attachment: " + ex);
        }
        pr("---------------------------");
      }
    }
  }
コード例 #3
0
ファイル: Service.java プロジェクト: Nekit10/Wheel-Of-Fortune
  /**
   * Similar to connect(host, user, password) except a specific port can be specified.
   *
   * @param host the host to connect to
   * @param port the port to connect to (-1 means the default port)
   * @param user the user name
   * @param password this user's password
   * @exception AuthenticationFailedException for authentication failures
   * @exception MessagingException for other failures
   * @exception IllegalStateException if the service is already connected
   * @see #connect(java.lang.String, java.lang.String, java.lang.String)
   * @see javax.mail.event.ConnectionEvent
   */
  public synchronized void connect(String host, int port, String user, String password)
      throws MessagingException {

    // see if the service is already connected
    if (isConnected()) throw new IllegalStateException("already connected");

    PasswordAuthentication pw;
    boolean connected = false;
    boolean save = false;
    String protocol = null;
    String file = null;

    // get whatever information we can from the URL
    // XXX - url should always be non-null here, Session
    //       passes it into the constructor
    if (url != null) {
      protocol = url.getProtocol();
      if (host == null) host = url.getHost();
      if (port == -1) port = url.getPort();

      if (user == null) {
        user = url.getUsername();
        if (password == null) // get password too if we need it
        password = url.getPassword();
      } else {
        if (password == null && user.equals(url.getUsername()))
          // only get the password if it matches the username
          password = url.getPassword();
      }

      file = url.getFile();
    }

    // try to get protocol-specific default properties
    if (protocol != null) {
      if (host == null) host = session.getProperty("mail." + protocol + ".host");
      if (user == null) user = session.getProperty("mail." + protocol + ".user");
    }

    // try to get mail-wide default properties
    if (host == null) host = session.getProperty("mail.host");

    if (user == null) user = session.getProperty("mail.user");

    // try using the system username
    if (user == null) {
      try {
        user = System.getProperty("user.name");
      } catch (SecurityException sex) {
        // XXX - it's not worth creating a MailLogger just for this
        // logger.log(Level.CONFIG, "Can't get user.name property", sex);
      }
    }

    // if we don't have a password, look for saved authentication info
    if (password == null && url != null) {
      // canonicalize the URLName
      setURLName(new URLName(protocol, host, port, file, user, null));
      pw = session.getPasswordAuthentication(getURLName());
      if (pw != null) {
        if (user == null) {
          user = pw.getUserName();
          password = pw.getPassword();
        } else if (user.equals(pw.getUserName())) {
          password = pw.getPassword();
        }
      } else save = true;
    }

    // try connecting, if the protocol needs some missing
    // information (user, password) it will not connect.
    // if it tries to connect and fails, remember why for later.
    AuthenticationFailedException authEx = null;
    try {
      connected = protocolConnect(host, port, user, password);
    } catch (AuthenticationFailedException ex) {
      authEx = ex;
    }

    // if not connected, ask the user and try again
    if (!connected) {
      InetAddress addr;
      try {
        addr = InetAddress.getByName(host);
      } catch (UnknownHostException e) {
        addr = null;
      }
      pw = session.requestPasswordAuthentication(addr, port, protocol, null, user);
      if (pw != null) {
        user = pw.getUserName();
        password = pw.getPassword();

        // have the service connect again
        connected = protocolConnect(host, port, user, password);
      }
    }

    // if we're not connected by now, we give up
    if (!connected) {
      if (authEx != null) throw authEx;
      else if (user == null)
        throw new AuthenticationFailedException("failed to connect, no user name specified?");
      else if (password == null)
        throw new AuthenticationFailedException("failed to connect, no password specified?");
      else throw new AuthenticationFailedException("failed to connect");
    }

    setURLName(new URLName(protocol, host, port, file, user, password));

    if (save)
      session.setPasswordAuthentication(getURLName(), new PasswordAuthentication(user, password));

    // set our connected state
    setConnected(true);

    // finally, deliver the connection event
    notifyConnectionListeners(ConnectionEvent.OPENED);
  }