Search in sources :

Example 1 with Address

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

the class Reviewers method parse.

@Override
public ReviewerResource parse(ChangeResource rsrc, IdString id) throws OrmException, ResourceNotFoundException, AuthException {
    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;
        }
    }
    // See if the id exists as a reviewer for this change
    if (accountId != null && fetchAccountIds(rsrc).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) Address(com.google.gerrit.server.mail.Address) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 2 with Address

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

the class ProjectWatchIT method noNotificationForDraftPatchSetsForWatchersInNotifyConfig.

@Test
public void noNotificationForDraftPatchSetsForWatchersInNotifyConfig() throws Exception {
    Address addr = new Address("Watcher", "watcher@example.com");
    NotifyConfig nc = new NotifyConfig();
    nc.addEmail(addr);
    nc.setName("team");
    nc.setHeader(NotifyConfig.Header.TO);
    nc.setTypes(EnumSet.of(NotifyType.NEW_PATCHSETS, NotifyType.ALL_COMMENTS));
    ProjectConfig cfg = projectCache.checkedGet(project).getConfig();
    cfg.putNotifyConfig("team", nc);
    saveProjectConfig(project, cfg);
    PushOneCommit.Result r = pushFactory.create(db, admin.getIdent(), testRepo, "subject", "a", "a1").to("refs/for/master");
    r.assertOkStatus();
    sender.clear();
    r = pushFactory.create(db, admin.getIdent(), testRepo, "subject", "a", "a2", r.getChangeId()).to("refs/for/master%draft");
    r.assertOkStatus();
    assertThat(sender.getMessages()).isEmpty();
    setApiUser(admin);
    ReviewInput in = new ReviewInput();
    in.message = "comment";
    gApi.changes().id(r.getChangeId()).current().review(in);
    assertThat(sender.getMessages()).isEmpty();
}
Also used : ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Address(com.google.gerrit.server.mail.Address) NotifyConfig(com.google.gerrit.server.git.NotifyConfig) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 3 with Address

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

the class RawMailParser method parse.

/**
   * Parses a MailMessage from a string.
   *
   * @param raw String as received over the wire
   * @return Parsed MailMessage
   * @throws MailParsingException
   */
public static MailMessage parse(String raw) throws MailParsingException {
    MailMessage.Builder messageBuilder = MailMessage.builder();
    messageBuilder.rawContentUTF(raw);
    Message mimeMessage;
    try {
        MessageBuilder builder = new DefaultMessageBuilder();
        mimeMessage = builder.parseMessage(new ByteArrayInputStream(raw.getBytes(UTF_8)));
    } catch (IOException | MimeException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    // Add general headers
    if (mimeMessage.getMessageId() != null) {
        messageBuilder.id(mimeMessage.getMessageId());
    }
    if (mimeMessage.getSubject() != null) {
        messageBuilder.subject(mimeMessage.getSubject());
    }
    messageBuilder.dateReceived(new DateTime(mimeMessage.getDate()));
    // Add From, To and Cc
    if (mimeMessage.getFrom() != null && mimeMessage.getFrom().size() > 0) {
        Mailbox from = mimeMessage.getFrom().get(0);
        messageBuilder.from(new Address(from.getName(), from.getAddress()));
    }
    if (mimeMessage.getTo() != null) {
        for (Mailbox m : mimeMessage.getTo().flatten()) {
            messageBuilder.addTo(new Address(m.getName(), m.getAddress()));
        }
    }
    if (mimeMessage.getCc() != null) {
        for (Mailbox m : mimeMessage.getCc().flatten()) {
            messageBuilder.addCc(new Address(m.getName(), m.getAddress()));
        }
    }
    // Add additional headers
    mimeMessage.getHeader().getFields().stream().filter(f -> !MAIN_HEADERS.contains(f.getName().toLowerCase())).forEach(f -> messageBuilder.addAdditionalHeader(f.getName() + ": " + f.getBody()));
    // Add text and html body parts
    StringBuilder textBuilder = new StringBuilder();
    StringBuilder htmlBuilder = new StringBuilder();
    try {
        handleMimePart(mimeMessage, textBuilder, htmlBuilder);
    } catch (IOException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    messageBuilder.textContent(Strings.emptyToNull(textBuilder.toString()));
    messageBuilder.htmlContent(Strings.emptyToNull(htmlBuilder.toString()));
    try {
        // required attributes are missing, so that the caller doesn't fall over.
        return messageBuilder.build();
    } catch (IllegalStateException e) {
        throw new MailParsingException("Missing required attributes after email was parsed", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Message(org.apache.james.mime4j.dom.Message) MimeException(org.apache.james.mime4j.MimeException) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) TextBody(org.apache.james.mime4j.dom.TextBody) Ints(com.google.common.primitives.Ints) InputStreamReader(java.io.InputStreamReader) Multipart(org.apache.james.mime4j.dom.Multipart) Strings(com.google.common.base.Strings) Address(com.google.gerrit.server.mail.Address) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) ImmutableList(com.google.common.collect.ImmutableList) ByteArrayInputStream(java.io.ByteArrayInputStream) CharStreams(com.google.common.io.CharStreams) Entity(org.apache.james.mime4j.dom.Entity) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) Message(org.apache.james.mime4j.dom.Message) Address(com.google.gerrit.server.mail.Address) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.apache.james.mime4j.MimeException)

Example 4 with Address

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

the class RegisterNewEmailSender method init.

@Override
protected void init() throws EmailException {
    super.init();
    setHeader("Subject", "[Gerrit Code Review] Email Verification");
    add(RecipientType.TO, new Address(addr));
}
Also used : Address(com.google.gerrit.server.mail.Address)

Example 5 with Address

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

the class AttachmentMessage method expectedMailMessage.

@Override
public MailMessage expectedMailMessage() {
    System.out.println("😛 test");
    MailMessage.Builder expect = MailMessage.builder();
    expect.id("<CAM7sg=3meaAVUxW3KXeJEVs8sv_ADw1BnvpcHHiYVR2TQQi__w@mail.gmail.com>").from(new Address("Patrick Hiesel", "hiesel@google.com")).addTo(new Address("Patrick Hiesel", "hiesel@google.com")).textContent("Contains unwanted attachment").htmlContent("<div dir=\"ltr\">Contains unwanted attachment</div>").subject("Test Subject").addAdditionalHeader("MIME-Version: 1.0").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)

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