/**
  * Writes the mail attributes from the <code>jamesattr:*</code> property.
  *
  * @param node mail node
  * @param mail mail message
  * @throws RepositoryException if a repository error occurs
  * @throws IOException if an IO error occurs
  */
 private void getAttributes(Node node, Mail mail) throws RepositoryException, IOException {
   PropertyIterator iterator = node.getProperties("jamesattr:*");
   while (iterator.hasNext()) {
     Property property = iterator.nextProperty();
     String name =
         Text.unescapeIllegalJcrChars(property.getName().substring("jamesattr:".length()));
     if (property.getType() == PropertyType.BINARY) {
       @SuppressWarnings("deprecation")
       InputStream input = property.getStream();
       try {
         ObjectInputStream stream = new ObjectInputStream(input);
         mail.setAttribute(name, (Serializable) stream.readObject());
       } catch (ClassNotFoundException e) {
         throw new IOException(e.getMessage());
       } finally {
         input.close();
       }
     } else {
       mail.setAttribute(name, property.getString());
     }
   }
 }
  /**
   * @see org.apache.james.queue.jms.JMSMailQueue#populateMailMimeMessage(javax.jms.Message,
   *     org.apache.mailet.Mail)
   */
  protected void populateMailMimeMessage(Message message, Mail mail)
      throws MessagingException, JMSException {
    if (message instanceof BlobMessage) {
      try {
        BlobMessage blobMessage = (BlobMessage) message;
        try {
          // store URL and queueName for later usage
          mail.setAttribute(JAMES_BLOB_URL, blobMessage.getURL());
          mail.setAttribute(JAMES_QUEUE_NAME, queueName);
        } catch (MalformedURLException e) {
          // Ignore on error
          logger.debug("Unable to get url from blobmessage for mail " + mail.getName());
        }
        MimeMessageSource source = new MimeMessageBlobMessageSource(blobMessage);
        mail.setMessage(new MimeMessageCopyOnWriteProxy(source));

      } catch (JMSException e) {
        throw new MailQueueException(
            "Unable to populate MimeMessage for mail " + mail.getName(), e);
      }
    } else {
      super.populateMailMimeMessage(message, mail);
    }
  }
Example #3
0
  /**
   * @see
   *     org.apache.james.smtpserver.JamesMessageHook#onMessage(org.apache.james.protocols.smtp.SMTPSession,
   *     org.apache.mailet.Mail)
   */
  public HookResult onMessage(SMTPSession session, Mail mail) {

    try {
      MimeMessage message = mail.getMessage();
      SpamAssassinInvoker sa = new SpamAssassinInvoker(spamdHost, spamdPort);
      sa.scanMail(message);

      // Add the headers
      for (String key : sa.getHeadersAsAttribute().keySet()) {
        mail.setAttribute(key, sa.getHeadersAsAttribute().get(key));
      }

      // Check if rejectionHits was configured
      if (spamdRejectionHits > 0) {
        try {
          double hits = Double.parseDouble(sa.getHits());

          // if the hits are bigger the rejectionHits reject the
          // message
          if (spamdRejectionHits <= hits) {
            String buffer =
                "Rejected message from "
                    + session.getAttachment(SMTPSession.SENDER, State.Transaction).toString()
                    + " from host "
                    + session.getRemoteAddress().getHostName()
                    + " ("
                    + session.getRemoteAddress().getAddress().getHostAddress()
                    + ") This message reach the spam hits treshold. Required rejection hits: "
                    + spamdRejectionHits
                    + " hits: "
                    + hits;
            session.getLogger().info(buffer);

            // Message reject .. abort it!
            return new HookResult(
                HookReturnCode.DENY,
                DSNStatus.getStatus(DSNStatus.PERMANENT, DSNStatus.SECURITY_OTHER)
                    + " This message reach the spam hits treshold. Please contact the Postmaster if the email is not SPAM. Message rejected");
          }
        } catch (NumberFormatException e) {
          // hits unknown
        }
      }
    } catch (MessagingException e) {
      session.getLogger().error(e.getMessage());
    }
    return new HookResult(HookReturnCode.DECLINED);
  }
  @Test
  public void shouldOverwriteAttributeWhenAttributeAlreadyPresent() throws MessagingException {
    FakeMailetConfig mailetConfig = new FakeMailetConfig("Test", FakeMailContext.defaultContext());
    mailetConfig.setProperty("org.apache.james.junit1", "bar");

    mailet.init(mailetConfig);

    Mail mail =
        MailUtil.createMockMail2Recipients(
            new MimeMessage(Session.getDefaultInstance(new Properties())));
    mail.setAttribute("org.apache.james.junit1", "foo");

    mailet.service(mail);

    assertThat(mail.getAttribute("org.apache.james.junit1")).isEqualTo("bar");
  }