Search in sources :

Example 16 with Address

use of com.google.gerrit.server.mail.Address in project gerrit by GerritCodeReview.

the class QuotedPrintableHeaderMessage method expectedMailMessage.

@Override
public MailMessage expectedMailMessage() {
    System.out.println("😛 test");
    MailMessage.Builder expect = MailMessage.builder();
    expect.id("<001a114da7ae26e2eb053fe0c29c@google.com>").from(new Address("Jonathan Nieder (Gerrit)", "noreply-gerritcodereview-CtTy0igsBrnvL7dKoWEIEg@google.com")).addTo(new Address("ekempin", "ekempin@google.com")).textContent(textContent).subject("âme vulgaire").dateReceived(new DateTime(2016, 10, 25, 9, 11, 35, 0, DateTimeZone.UTC));
    return expect.build();
}
Also used : MailMessage(com.google.gerrit.server.mail.receive.MailMessage) Address(com.google.gerrit.server.mail.Address) DateTime(org.joda.time.DateTime)

Example 17 with Address

use of com.google.gerrit.server.mail.Address in project gerrit by GerritCodeReview.

the class RevisionReviewers method parse.

@Override
public ReviewerResource parse(RevisionResource rsrc, IdString id) throws OrmException, ResourceNotFoundException, AuthException, MethodNotAllowedException {
    if (!rsrc.isCurrent()) {
        throw new MethodNotAllowedException("Cannot access on non-current patch set");
    }
    Address address = Address.tryParse(id.get());
    Account.Id accountId = null;
    try {
        accountId = accounts.parse(TopLevelResource.INSTANCE, id).getUser().getAccountId();
    } catch (ResourceNotFoundException e) {
        if (address == null) {
            throw e;
        }
    }
    Collection<Account.Id> reviewers = approvalsUtil.getReviewers(dbProvider.get(), rsrc.getNotes()).all();
    // See if the id exists as a reviewer for this change
    if (reviewers.contains(accountId)) {
        return resourceFactory.create(rsrc, accountId);
    }
    // See if the address exists as a reviewer on the change
    if (address != null && rsrc.getNotes().getReviewersByEmail().all().contains(address)) {
        return new ReviewerResource(rsrc, address);
    }
    throw new ResourceNotFoundException(id);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) Address(com.google.gerrit.server.mail.Address) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 18 with Address

use of com.google.gerrit.server.mail.Address in project gerrit by GerritCodeReview.

the class ListReviewers method apply.

@Override
public List<ReviewerInfo> apply(ChangeResource rsrc) throws OrmException, PermissionBackendException {
    Map<String, ReviewerResource> reviewers = new LinkedHashMap<>();
    ReviewDb db = dbProvider.get();
    for (Account.Id accountId : approvalsUtil.getReviewers(db, rsrc.getNotes()).all()) {
        if (!reviewers.containsKey(accountId.toString())) {
            reviewers.put(accountId.toString(), resourceFactory.create(rsrc, accountId));
        }
    }
    for (Address adr : rsrc.getNotes().getReviewersByEmail().all()) {
        if (!reviewers.containsKey(adr.toString())) {
            reviewers.put(adr.toString(), new ReviewerResource(rsrc, adr));
        }
    }
    return json.format(reviewers.values());
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Address(com.google.gerrit.server.mail.Address) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with Address

use of com.google.gerrit.server.mail.Address in project gerrit by GerritCodeReview.

the class OutgoingEmail method send.

/**
   * Format and enqueue the message for delivery.
   *
   * @throws EmailException
   */
public void send() throws EmailException {
    if (NotifyHandling.NONE.equals(notify) && accountsToNotify.isEmpty()) {
        return;
    }
    if (!args.emailSender.isEnabled()) {
        //
        return;
    }
    init();
    if (useHtml()) {
        appendHtml(soyHtmlTemplate("HeaderHtml"));
    }
    format();
    appendText(textTemplate("Footer"));
    if (useHtml()) {
        appendHtml(soyHtmlTemplate("FooterHtml"));
    }
    Set<Address> smtpRcptToPlaintextOnly = new HashSet<>();
    if (shouldSendMessage()) {
        if (fromId != null) {
            final Account fromUser = args.accountCache.get(fromId).getAccount();
            GeneralPreferencesInfo senderPrefs = fromUser.getGeneralPreferencesInfo();
            if (senderPrefs != null && senderPrefs.getEmailStrategy() == CC_ON_OWN_COMMENTS) {
                // If we are impersonating a user, make sure they receive a CC of
                // this message so they can always review and audit what we sent
                // on their behalf to others.
                //
                add(RecipientType.CC, fromId);
            } else if (!accountsToNotify.containsValue(fromId) && rcptTo.remove(fromId)) {
                // If they don't want a copy, but we queued one up anyway,
                // drop them from the recipient lists.
                //
                removeUser(fromUser);
            }
        }
        // In addition, check if users only want to receive plaintext email.
        for (Account.Id id : rcptTo) {
            Account thisUser = args.accountCache.get(id).getAccount();
            GeneralPreferencesInfo prefs = thisUser.getGeneralPreferencesInfo();
            if (prefs == null || prefs.getEmailStrategy() == DISABLED) {
                removeUser(thisUser);
            } else if (useHtml() && prefs.getEmailFormat() == EmailFormat.PLAINTEXT) {
                removeUser(thisUser);
                smtpRcptToPlaintextOnly.add(new Address(thisUser.getFullName(), thisUser.getPreferredEmail()));
            }
            if (smtpRcptTo.isEmpty() && smtpRcptToPlaintextOnly.isEmpty()) {
                return;
            }
        }
        // inbound email replies.
        if (!headers.containsKey("Reply-To")) {
            StringJoiner j = new StringJoiner(", ");
            if (fromId != null) {
                Address address = toAddress(fromId);
                if (address != null) {
                    j.add(address.getEmail());
                }
            }
            smtpRcptTo.stream().forEach(a -> j.add(a.getEmail()));
            smtpRcptToPlaintextOnly.stream().forEach(a -> j.add(a.getEmail()));
            setHeader("Reply-To", j.toString());
        }
        String textPart = textBody.toString();
        OutgoingEmailValidationListener.Args va = new OutgoingEmailValidationListener.Args();
        va.messageClass = messageClass;
        va.smtpFromAddress = smtpFromAddress;
        va.smtpRcptTo = smtpRcptTo;
        va.headers = headers;
        va.body = textPart;
        if (useHtml()) {
            va.htmlBody = htmlBody.toString();
        } else {
            va.htmlBody = null;
        }
        for (OutgoingEmailValidationListener validator : args.outgoingEmailValidationListeners) {
            try {
                validator.validateOutgoingEmail(va);
            } catch (ValidationException e) {
                return;
            }
        }
        if (!smtpRcptTo.isEmpty()) {
            // Send multipart message
            args.emailSender.send(va.smtpFromAddress, va.smtpRcptTo, va.headers, va.body, va.htmlBody);
        }
        if (!smtpRcptToPlaintextOnly.isEmpty()) {
            // Send plaintext message
            Map<String, EmailHeader> shallowCopy = new HashMap<>();
            shallowCopy.putAll(headers);
            // Remove To and Cc
            shallowCopy.remove(HDR_TO);
            shallowCopy.remove(HDR_CC);
            for (Address a : smtpRcptToPlaintextOnly) {
                // Add new To
                EmailHeader.AddressList to = new EmailHeader.AddressList();
                to.add(a);
                shallowCopy.put(HDR_TO, to);
            }
            args.emailSender.send(va.smtpFromAddress, smtpRcptToPlaintextOnly, shallowCopy, va.body);
        }
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ValidationException(com.google.gerrit.server.validators.ValidationException) Address(com.google.gerrit.server.mail.Address) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) OutgoingEmailValidationListener(com.google.gerrit.server.validators.OutgoingEmailValidationListener) AddressList(com.google.gerrit.server.mail.send.EmailHeader.AddressList) GeneralPreferencesInfo(com.google.gerrit.extensions.client.GeneralPreferencesInfo) AddressList(com.google.gerrit.server.mail.send.EmailHeader.AddressList) StringJoiner(java.util.StringJoiner) HashSet(java.util.HashSet)

Example 20 with Address

use of com.google.gerrit.server.mail.Address in project gerrit by GerritCodeReview.

the class AddKeySender method init.

@Override
protected void init() throws EmailException {
    super.init();
    setHeader("Subject", String.format("[Gerrit Code Review] New %s Keys Added", getKeyType()));
    add(RecipientType.TO, new Address(getEmail()));
}
Also used : Address(com.google.gerrit.server.mail.Address)

Aggregations

Address (com.google.gerrit.server.mail.Address)23 DateTime (org.joda.time.DateTime)11 MailMessage (com.google.gerrit.server.mail.receive.MailMessage)6 Account (com.google.gerrit.reviewdb.client.Account)5 Test (org.junit.Test)5 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)3 LinkedHashMap (java.util.LinkedHashMap)3 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)2 ReviewInput (com.google.gerrit.extensions.api.changes.ReviewInput)2 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 NotifyConfig (com.google.gerrit.server.git.NotifyConfig)2 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableTable (com.google.common.collect.ImmutableTable)1