Java Code Examples for javax.mail.Flags#add()

The following examples show how to use javax.mail.Flags#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlfrescoImapFolder.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 *  Creates the EML message in the specified folder.
 *  
 *  @param folderFileInfo The folder to create message in.
 *  @param message The original MimeMessage.
 *  @return ID of the new message created 
 * @throws FileNotFoundException 
 * @throws FileExistsException 
 * @throws MessagingException 
 * @throws IOException 
 */
private long createMimeMessageInFolder(
        FileInfo folderFileInfo,
        MimeMessage message,
        Flags flags)
        throws FileExistsException, FileNotFoundException, IOException, MessagingException 
{
    String name = AlfrescoImapConst.MESSAGE_PREFIX + GUID.generate();
    FileFolderService fileFolderService = serviceRegistry.getFileFolderService();
    FileInfo messageFile = fileFolderService.create(folderFileInfo.getNodeRef(), name, ContentModel.TYPE_CONTENT);
    final long newMessageUid = (Long) messageFile.getProperties().get(ContentModel.PROP_NODE_DBID);
    name = AlfrescoImapConst.MESSAGE_PREFIX  + newMessageUid + AlfrescoImapConst.EML_EXTENSION;
    fileFolderService.rename(messageFile.getNodeRef(), name);
    Flags newFlags = new Flags(flags);
    newFlags.add(Flag.RECENT);
    imapService.setFlags(messageFile, newFlags, true);
    
    if (extractAttachmentsEnabled)
    {
        imapService.extractAttachments(messageFile.getNodeRef(), message);
    }
    // Force persistence of the message to the repository
    new IncomingImapMessage(messageFile, serviceRegistry, message);
    return newMessageUid;        
}
 
Example 2
Source File: MaildirMessageName.java    From james-project with Apache License 2.0 6 votes vote down vote up
/**
 * Takes a String which is "Maildir encoded" and translates it
 * into a {@link Flags} object.
 * @param flagsString The String with the flags
 * @return A Flags object containing the flags read form the String
 */
public Flags decodeFlags(String flagsString) {
    Flags localFlags = new Flags();
    if (flagsString.contains(FLAG_DRAFT)) {
        localFlags.add(Flags.Flag.DRAFT);
    }
    if (flagsString.contains(FLAG_FLAGGED)) {
        localFlags.add(Flags.Flag.FLAGGED);
    }
    if (flagsString.contains(FLAG_ANSWERD)) {
        localFlags.add(Flags.Flag.ANSWERED);
    }
    if (flagsString.contains(FLAG_SEEN)) {
        localFlags.add(Flags.Flag.SEEN);
    }
    if (flagsString.contains(FLAG_DELETED)) {
        localFlags.add(Flags.Flag.DELETED);
    }
    return localFlags;
}
 
Example 3
Source File: SetCustomFlagOnBigMessagesTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void shouldNotRemoveOtherFlagsWhenBigMessages() throws Exception {
    Flags appendMessageFlag = new Flags();
    appendMessageFlag.add(Flags.Flag.SEEN);
    appendMessageFlag.add(Flags.Flag.DRAFT);

    ComposedMessageId composedId = inboxMessageManager.appendMessage(
        MessageManager.AppendCommand.builder()
            .withFlags(appendMessageFlag)
            .build(bigMessage()),
        mailboxSession).getId();

    assertThat(getMessageFlags(composedId.getUid()))
        .allSatisfy(flags -> {
            assertThat(flags.contains(Flags.Flag.SEEN)).isTrue();
            assertThat(flags.contains(Flags.Flag.DRAFT)).isTrue();
            assertThat(flags.contains(BIG_MESSAGE)).isTrue();
        });
}
 
Example 4
Source File: DecoderUtils.java    From james-project with Apache License 2.0 6 votes vote down vote up
public static void setFlag(String flagString, Flags flags) throws DecodingException {
    if (flagString.equalsIgnoreCase(MessageFlags.ANSWERED_ALL_CAPS)) {
        flags.add(Flags.Flag.ANSWERED);
    } else if (flagString.equalsIgnoreCase(MessageFlags.DELETED_ALL_CAPS)) {
        flags.add(Flags.Flag.DELETED);
    } else if (flagString.equalsIgnoreCase(MessageFlags.DRAFT_ALL_CAPS)) {
        flags.add(Flags.Flag.DRAFT);
    } else if (flagString.equalsIgnoreCase(MessageFlags.FLAGGED_ALL_CAPS)) {
        flags.add(Flags.Flag.FLAGGED);
    } else if (flagString.equalsIgnoreCase(MessageFlags.SEEN_ALL_CAPS)) {
        flags.add(Flags.Flag.SEEN);
    } else {
        if (flagString.equalsIgnoreCase(MessageFlags.RECENT_ALL_CAPS)) {
            // RFC3501 specifically excludes \Recent
            // The \Recent flag should be set automatically by the server so throw Exception
            //
            // See IMAP-316
            throw new DecodingException(HumanReadableText.INVALID_SYSTEM_FLAG, "\\Recent flag is now allowed to set.");
        } else {
            // RFC3501 allows novel flags
            flags.add(flagString);
        }
    }
}
 
Example 5
Source File: CommandParser.java    From greenmail with Apache License 2.0 6 votes vote down vote up
public void setFlag(String flagString, Flags flags) {
    if (flagString.equalsIgnoreCase(MessageFlags.ANSWERED)) {
        flags.add(Flags.Flag.ANSWERED);
    } else if (flagString.equalsIgnoreCase(MessageFlags.DELETED)) {
        flags.add(Flags.Flag.DELETED);
    } else if (flagString.equalsIgnoreCase(MessageFlags.DRAFT)) {
        flags.add(Flags.Flag.DRAFT);
    } else if (flagString.equalsIgnoreCase(MessageFlags.FLAGGED)) {
        flags.add(Flags.Flag.FLAGGED);
    } else if (flagString.equalsIgnoreCase(MessageFlags.SEEN)) {
        flags.add(Flags.Flag.SEEN);
    } else if (flagString.equalsIgnoreCase(MessageFlags.RECENT)) {
        flags.add(Flags.Flag.RECENT);
    } else {
        // User flag
        flags.add(flagString);
    }
}
 
Example 6
Source File: UpdatedFlags.java    From james-project with Apache License 2.0 6 votes vote down vote up
private static void addModifiedSystemFlags(Flags oldFlags, Flags newFlags, Flags modifiedFlags) {
    if (isChanged(oldFlags, newFlags, Flags.Flag.ANSWERED)) {
        modifiedFlags.add(Flags.Flag.ANSWERED);
    }
    if (isChanged(oldFlags, newFlags, Flags.Flag.DELETED)) {
        modifiedFlags.add(Flags.Flag.DELETED);
    }
    if (isChanged(oldFlags, newFlags, Flags.Flag.DRAFT)) {
        modifiedFlags.add(Flags.Flag.DRAFT);
    }
    if (isChanged(oldFlags, newFlags, Flags.Flag.FLAGGED)) {
        modifiedFlags.add(Flags.Flag.FLAGGED);
    }
    if (isChanged(oldFlags, newFlags, Flags.Flag.RECENT)) {
        modifiedFlags.add(Flags.Flag.RECENT);
    }
    if (isChanged(oldFlags, newFlags, Flags.Flag.SEEN)) {
        modifiedFlags.add(Flags.Flag.SEEN);
    }
}
 
Example 7
Source File: MessageMapperTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void getApplicableFlagShouldUnionAllMessageFlagsExceptRecentAndUser() throws Exception {
    message1.setFlags(new Flags(Flag.ANSWERED));
    message2.setFlags(new Flags(Flag.DELETED));

    Flags recentAndUser = new Flags(Flag.RECENT);
    recentAndUser.add(Flag.USER);
    recentAndUser.add(CUSTOMS_USER_FLAGS_VALUE);
    message3.setFlags(recentAndUser);

    saveMessages();

    assertThat(messageMapper.getApplicableFlag(benwaInboxMailbox))
        .isEqualTo(FlagsBuilder.builder()
            .add(Flag.DELETED, Flag.ANSWERED, Flag.DRAFT, Flag.FLAGGED, Flag.SEEN)
            .add(CUSTOMS_USER_FLAGS_VALUE)
            .build());
}
 
Example 8
Source File: FlagExtraFieldTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void getCentralDirectoryLengthShouldReturnIntegerSizeWhenSystemFlagSet() {
    Flags flags = new Flags();
    flags.add(Flags.Flag.SEEN);
    FlagsExtraField testee = new FlagsExtraField(flags);

    assertThat(testee.getCentralDirectoryLength().getValue())
        .isEqualTo(5);
}
 
Example 9
Source File: CassandraMessageManager.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Support user flags
 */
@Override
protected Flags getPermanentFlags(MailboxSession session) {
    Flags flags = super.getPermanentFlags(session);
    flags.add(Flags.Flag.USER);
    return flags;
}
 
Example 10
Source File: FlagExtraFieldTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void getLocalFileDataLengthShouldReturnIntegerSizeWhenSystemAndUserFlagSet() {
    Flags flags = new Flags("myFlags");
    flags.add(Flags.Flag.ANSWERED);
    FlagsExtraField testee = new FlagsExtraField(flags);

    assertThat(testee.getLocalFileDataLength().getValue())
        .isEqualTo(17);
}
 
Example 11
Source File: FlagsUpdateCalculator.java    From james-project with Apache License 2.0 5 votes vote down vote up
public Flags buildNewFlags(Flags oldFlags) {
    Flags updatedFlags = new Flags(oldFlags);
    switch (mode) {
    case REPLACE:
        return new Flags(providedFlags);
    case ADD:
        updatedFlags.add(providedFlags);
        break;
    case REMOVE:
        updatedFlags.remove(providedFlags);
        break;
    }
    return updatedFlags;
}
 
Example 12
Source File: OpenJPAMessageManager.java    From james-project with Apache License 2.0 5 votes vote down vote up
/**
 * Support user flags
 */
@Override
protected Flags getPermanentFlags(MailboxSession session) {
    Flags flags =  super.getPermanentFlags(session);
    flags.add(Flags.Flag.USER);
    return flags;
}
 
Example 13
Source File: FlagExtraFieldTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void parseFromLocalFileDataShouldParseByteDataWhenOffsetSet() {
    String bufferContent = "\\ANSWERED%\\SEEN%myFlags";
    Flags flags = new Flags("myFlags");
    flags.add(Flags.Flag.SEEN);

    FlagsExtraField testee = new FlagsExtraField(new Flags());
    testee.parseFromLocalFileData(bufferContent
        .getBytes(StandardCharsets.UTF_8), 10, 13);
    assertThat(testee.getValue()).contains("\\SEEN%myFlags");
}
 
Example 14
Source File: StoreCommandParserTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldParseSilentDraftFlagged() throws Exception {
    IdRange[] ranges = { new IdRange(1) };
    Flags flags = new Flags();
    flags.add(Flags.Flag.DRAFT);
    flags.add(Flags.Flag.FLAGGED);
    check("1 FLAGS.SILENT (\\Draft \\Flagged)\r\n", ranges, true, null,
            flags, false, TAG);
}
 
Example 15
Source File: OldKeyword.java    From james-project with Apache License 2.0 5 votes vote down vote up
public Flags applyToState(Flags currentFlags) {
    Flags newStateFlags = new Flags();

    if (isFlagged().orElse(currentFlags.contains(Flags.Flag.FLAGGED))) {
        newStateFlags.add(Flags.Flag.FLAGGED);
    }
    if (isAnswered().orElse(currentFlags.contains(Flags.Flag.ANSWERED))) {
        newStateFlags.add(Flags.Flag.ANSWERED);
    }
    if (isDraft().orElse(currentFlags.contains(Flags.Flag.DRAFT))) {
        newStateFlags.add(Flags.Flag.DRAFT);
    }
    if (isForwarded().orElse(currentFlags.contains(new Flags("$Forwarded")))) {
        newStateFlags.add(new Flags("$Forwarded"));
    }
    boolean shouldMessageBeMarkSeen = isUnread().map(b -> !b).orElse(currentFlags.contains(Flags.Flag.SEEN));
    if (shouldMessageBeMarkSeen) {
        newStateFlags.add(Flags.Flag.SEEN);
    }
    Arrays.stream(currentFlags.getUserFlags())
        .forEach(newStateFlags::add);
    if (currentFlags.contains(Flags.Flag.RECENT)) {
        newStateFlags.add(Flags.Flag.RECENT);
    }
    if (currentFlags.contains(Flags.Flag.DELETED)) {
        newStateFlags.add(Flags.Flag.DELETED);
    }
    return newStateFlags;
}
 
Example 16
Source File: SetCustomFlagOnBigMessagesTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void shouldKeepBigMessageFlagWhenAlreadySet() throws Exception {
    Flags appendMessageFlag = new Flags();
    appendMessageFlag.add(BIG_MESSAGE);

    ComposedMessageId composedId = inboxMessageManager.appendMessage(
        MessageManager.AppendCommand.builder()
            .withFlags(appendMessageFlag)
            .build(bigMessage()),
        mailboxSession).getId();

    assertThat(getMessageFlags(composedId.getUid()))
        .allSatisfy(flags -> assertThat(flags.contains(BIG_MESSAGE)).isTrue());
}
 
Example 17
Source File: SearchTermBuilder.java    From greenmail with Apache License 2.0 5 votes vote down vote up
private static SearchTerm createFlagSearchTerm(String pFlagName, boolean pValue) {
    Flags.Flag flag = toFlag(pFlagName);
    Flags flags = new javax.mail.Flags();
    if (null == flag) { // user flags
        flags.add(pFlagName);
    } else {
        flags.add(flag);
    }
    return new FlagTerm(flags, pValue);
}
 
Example 18
Source File: GetMailboxesMethodTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
public void getMailboxesShouldReturnCorrectUnreadMessagesCount() throws Exception {
    MailboxPath mailboxPath = MailboxPath.forUser(USERNAME, "name");
    MailboxSession mailboxSession = mailboxManager.createSystemSession(USERNAME);
    mailboxManager.createMailbox(mailboxPath, mailboxSession);
    MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, mailboxSession);
    Flags defaultUnseenFlag = new Flags();
    Flags readMessageFlag = new Flags();
    readMessageFlag.add(Flags.Flag.SEEN);
    messageManager.appendMessage(MessageManager.AppendCommand.builder()
        .withFlags(defaultUnseenFlag)
        .build(Message.Builder.of()
            .setSubject("test")
            .setBody("testmail", StandardCharsets.UTF_8)), mailboxSession);
    messageManager.appendMessage(MessageManager.AppendCommand.builder()
        .withFlags(defaultUnseenFlag)
        .build(Message.Builder.of()
            .setSubject("test2")
            .setBody("testmail", StandardCharsets.UTF_8)), mailboxSession);
    messageManager.appendMessage(MessageManager.AppendCommand.builder()
        .withFlags(readMessageFlag)
        .build(Message.Builder.of()
            .setSubject("test3")
            .setBody("testmail", StandardCharsets.UTF_8)), mailboxSession);
    GetMailboxesRequest getMailboxesRequest = GetMailboxesRequest.builder()
            .build();

    List<JmapResponse> getMailboxesResponse = getMailboxesMethod.processToStream(getMailboxesRequest, methodCallId, mailboxSession).collect(Collectors.toList());

    assertThat(getMailboxesResponse)
            .hasSize(1)
            .extracting(JmapResponse::getResponse)
            .hasOnlyElementsOfType(GetMailboxesResponse.class)
            .extracting(GetMailboxesResponse.class::cast)
            .flatExtracting(GetMailboxesResponse::getList)
            .extracting(Mailbox::getUnreadMessages)
            .containsExactly(Number.fromLong(2L));
}
 
Example 19
Source File: FlagExtraFieldTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void parseFromCentralDirectoryDataShouldParseByteDataWhenOffsetSet() {
    String bufferContent = "\\ANSWERED%\\SEEN%myFlags";
    Flags flags = new Flags("myFlags");
    flags.add(Flags.Flag.SEEN);

    FlagsExtraField testee = new FlagsExtraField(new Flags());
    testee.parseFromCentralDirectoryData(bufferContent
        .getBytes(StandardCharsets.UTF_8), 10, 13);
    assertThat(testee.getValue()).contains("\\SEEN%myFlags");
}
 
Example 20
Source File: InMemoryMessageManager.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
protected Flags getPermanentFlags(MailboxSession session) {
    Flags permanentFlags = new Flags(super.getPermanentFlags(session));
    permanentFlags.add(Flags.Flag.USER);
    return permanentFlags;
}