http://git-wip-us.apache.org/repos/asf/james-project/blob/876987c8/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java ---------------------------------------------------------------------- diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java deleted file mode 100644 index f3771a2..0000000 --- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRMessageMapper.java +++ /dev/null @@ -1,688 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailbox.jcr.mail; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import javax.jcr.ItemNotFoundException; -import javax.jcr.Node; -import javax.jcr.NodeIterator; -import javax.jcr.RepositoryException; -import javax.jcr.Session; -import javax.jcr.query.Query; -import javax.jcr.query.QueryManager; -import javax.jcr.query.QueryResult; -import javax.mail.Flags; - -import org.apache.jackrabbit.commons.JcrUtils; -import org.apache.jackrabbit.util.ISO9075; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.jcr.JCRImapConstants; -import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository; -import org.apache.james.mailbox.jcr.mail.model.JCRMailboxMessage; -import org.apache.james.mailbox.model.MessageMetaData; -import org.apache.james.mailbox.model.MessageRange; -import org.apache.james.mailbox.model.MessageRange.Type; -import org.apache.james.mailbox.store.SimpleMessageMetaData; -import org.apache.james.mailbox.store.mail.AbstractMessageMapper; -import org.apache.james.mailbox.store.mail.MessageMapper; -import org.apache.james.mailbox.store.mail.ModSeqProvider; -import org.apache.james.mailbox.store.mail.UidProvider; -import org.apache.james.mailbox.store.mail.model.Mailbox; -import org.apache.james.mailbox.store.mail.model.MailboxMessage; -import org.apache.james.mailbox.store.mail.utils.ApplicableFlagCalculator; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * JCR implementation of a {@link MessageMapper}. The implementation store each - * message as a seperate child node under the mailbox - * - */ -public class JCRMessageMapper extends AbstractMessageMapper implements JCRImapConstants { - - private static final Logger LOGGER = LoggerFactory.getLogger(JCRMessageMapper.class); - - @SuppressWarnings("deprecation") - private static final String XPATH_LANGUAGE = Query.XPATH; - - /** - * Store the messages directly in the mailbox: .../mailbox/ - */ - public static final int MESSAGE_SCALE_NONE = 0; - - /** - * Store the messages under a year directory in the mailbox: - * .../mailbox/2010/ - */ - public static final int MESSAGE_SCALE_YEAR = 1; - - /** - * Store the messages under a year/month directory in the mailbox: - * .../mailbox/2010/05/ - */ - public static final int MESSAGE_SCALE_MONTH = 2; - - /** - * Store the messages under a year/month/day directory in the mailbox: - * .../mailbox/2010/05/01/ - */ - public static final int MESSAGE_SCALE_DAY = 3; - - /** - * Store the messages under a year/month/day/hour directory in the mailbox: - * .../mailbox/2010/05/02/11 - */ - public static final int MESSAGE_SCALE_HOUR = 4; - - /** - * Store the messages under a year/month/day/hour/min directory in the - * mailbox: .../mailbox/2010/05/02/11/59 - */ - public static final int MESSAGE_SCALE_MINUTE = 5; - - private final int scaleType; - - private final MailboxSessionJCRRepository repository; - - /** - * Construct a new {@link JCRMessageMapper} instance - * - * @param repository - * {@link MailboxSessionJCRRepository} to use - * @param mSession - * {@link MailboxSession} to which the mapper is bound - * @param uidProvider - * {@link UidProvider} to use - * @param modSeqProvider - * {@link ModSeqProvider} to use - * @param scaleType - * message scale type either {@link #MESSAGE_SCALE_DAY}, - * {@link #MESSAGE_SCALE_HOUR}, {@link #MESSAGE_SCALE_MINUTE}, - * {@link #MESSAGE_SCALE_MONTH}, {@link #MESSAGE_SCALE_NONE} or - * {@link #MESSAGE_SCALE_YEAR} - */ - public JCRMessageMapper(MailboxSessionJCRRepository repository, MailboxSession mSession, - UidProvider uidProvider, ModSeqProvider modSeqProvider, int scaleType) { - super(mSession, uidProvider, modSeqProvider); - this.repository = repository; - this.scaleType = scaleType; - } - - /** - * Construct a new {@link JCRMessageMapper} instance using - * {@link #MESSAGE_SCALE_DAY} as default - * - * @param repos - * {@link MailboxSessionJCRRepository} to use - * @param session - * {@link MailboxSession} to which the mapper is bound - * @param uidProvider - * {@link UidProvider} to use - * @param modSeqProvider - * {@link ModSeqProvider} to use - */ - public JCRMessageMapper(MailboxSessionJCRRepository repos, MailboxSession session, - UidProvider uidProvider, ModSeqProvider modSeqProvider) { - this(repos, session, uidProvider, modSeqProvider, MESSAGE_SCALE_DAY); - } - - /** - * Return the JCR Session - * - * @return session - */ - protected Session getSession() throws RepositoryException { - return repository.login(mailboxSession); - } - - /** - * Begin is not supported by level 1 JCR implementations, however we refresh - * the session - */ - @Override - protected void begin() throws MailboxException { - try { - getSession().refresh(true); - } catch (RepositoryException e) { - // do nothin on refresh - } - // Do nothing - } - - /** - * Just call save on the underlying JCR Session, because level 1 JCR - * implementation does not offer Transactions - */ - @Override - protected void commit() throws MailboxException { - try { - if (getSession().hasPendingChanges()) { - getSession().save(); - } - } catch (RepositoryException e) { - throw new MailboxException("Unable to commit", e); - } - } - - /** - * Rollback is not supported by level 1 JCR implementations, so just do - * nothing - */ - @Override - protected void rollback() throws MailboxException { - try { - // just refresh session and discard all pending changes - getSession().refresh(false); - } catch (RepositoryException e) { - // just catch on rollback by now - } - } - - /** - * Logout from open JCR Session - */ - @Override - public void endRequest() { - repository.logout(mailboxSession); - } - - @Override - public long countMessagesInMailbox(Mailbox mailbox) throws MailboxException { - try { - // we use order by because without it count will always be 0 in - // jackrabbit - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message) order by @" - + JCRMailboxMessage.UID_PROPERTY; - QueryManager manager = getSession().getWorkspace().getQueryManager(); - QueryResult result = manager.createQuery(queryString, XPATH_LANGUAGE).execute(); - NodeIterator nodes = result.getNodes(); - long count = nodes.getSize(); - if (count == -1) { - count = 0; - while (nodes.hasNext()) { - nodes.nextNode(); - count++; - } - } - return count; - } catch (RepositoryException e) { - throw new MailboxException("Unable to count messages in mailbox " + mailbox, e); - } - - } - - @Override - public long countUnseenMessagesInMailbox(Mailbox mailbox) throws MailboxException { - - try { - // we use order by because without it count will always be 0 in - // jackrabbit - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.SEEN_PROPERTY + "='false'] order by @" + JCRMailboxMessage.UID_PROPERTY; - QueryManager manager = getSession().getWorkspace().getQueryManager(); - QueryResult result = manager.createQuery(queryString, XPATH_LANGUAGE).execute(); - NodeIterator nodes = result.getNodes(); - long count = nodes.getSize(); - - if (count == -1) { - count = 0; - while (nodes.hasNext()) { - nodes.nextNode(); - - count++; - } - } - return count; - } catch (RepositoryException e) { - throw new MailboxException("Unable to count unseen messages in mailbox " + mailbox, e); - } - } - - @Override - public void delete(Mailbox mailbox, MailboxMessage message) throws MailboxException { - JCRMailboxMessage membership = (JCRMailboxMessage) message; - if (membership.isPersistent()) { - try { - - getSession().getNodeByIdentifier(membership.getId()).remove(); - } catch (RepositoryException e) { - throw new MailboxException("Unable to delete message " + message + " in mailbox " + mailbox, e); - } - } - } - - @Override - public Iterator<MailboxMessage> findInMailbox(Mailbox mailbox, MessageRange set, FetchType fType, int max) - throws MailboxException { - try { - List<MailboxMessage> results; - MessageUid from = set.getUidFrom(); - final MessageUid to = set.getUidTo(); - final Type type = set.getType(); - - switch (type) { - default: - case ALL: - results = findMessagesInMailbox(mailbox, max); - break; - case FROM: - results = findMessagesInMailboxAfterUID(mailbox, from, max); - break; - case ONE: - results = findMessageInMailboxWithUID(mailbox, from); - break; - case RANGE: - results = findMessagesInMailboxBetweenUIDs(mailbox, from, to, max); - break; - } - return results.iterator(); - } catch (RepositoryException e) { - throw new MailboxException("Unable to search MessageRange " + set + " in mailbox " + mailbox, e); - } - } - - /* - * - * TODO: Maybe we should better use an ItemVisitor and just traverse through - * the child nodes. This could be a way faster - */ - @Override - public List<MessageUid> findRecentMessageUidsInMailbox(Mailbox mailbox) throws MailboxException { - - try { - - List<MessageUid> list = new ArrayList<>(); - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.RECENT_PROPERTY + "='true'] order by @" + JCRMailboxMessage.UID_PROPERTY; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - Query query = manager.createQuery(queryString, XPATH_LANGUAGE); - QueryResult result = query.execute(); - - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - list.add(new JCRMailboxMessage(iterator.nextNode(), LOGGER).getUid()); - } - return list; - - } catch (RepositoryException e) { - throw new MailboxException("Unable to search recent messages in mailbox " + mailbox, e); - } - } - - @Override - public MessageUid findFirstUnseenMessageUid(Mailbox mailbox) throws MailboxException { - try { - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.SEEN_PROPERTY + "='false'] order by @" + JCRMailboxMessage.UID_PROPERTY; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - - Query query = manager.createQuery(queryString, XPATH_LANGUAGE); - query.setLimit(1); - QueryResult result = query.execute(); - - NodeIterator iterator = result.getNodes(); - if (iterator.hasNext()) { - return new JCRMailboxMessage(iterator.nextNode(), LOGGER).getUid(); - } else { - return null; - } - } catch (RepositoryException e) { - throw new MailboxException("Unable to find first unseen message in mailbox " + mailbox, e); - } - } - - @Override - public Map<MessageUid, MessageMetaData> expungeMarkedForDeletionInMailbox(Mailbox mailbox, MessageRange set) - throws MailboxException { - try { - final List<MailboxMessage> results; - final MessageUid from = set.getUidFrom(); - final MessageUid to = set.getUidTo(); - final Type type = set.getType(); - switch (type) { - default: - case ALL: - results = findDeletedMessagesInMailbox(mailbox); - break; - case FROM: - results = findDeletedMessagesInMailboxAfterUID(mailbox, from); - break; - case ONE: - results = findDeletedMessageInMailboxWithUID(mailbox, from); - break; - case RANGE: - results = findDeletedMessagesInMailboxBetweenUIDs(mailbox, from, to); - break; - } - Map<MessageUid, MessageMetaData> uids = new HashMap<>(); - for (MailboxMessage m : results) { - MessageUid uid = m.getUid(); - uids.put(uid, new SimpleMessageMetaData(m)); - delete(mailbox, m); - } - return uids; - } catch (RepositoryException e) { - throw new MailboxException("Unable to search MessageRange " + set + " in mailbox " + mailbox, e); - } - } - - @Override - public MessageMetaData move(Mailbox mailbox, MailboxMessage original) throws MailboxException { - throw new UnsupportedOperationException("Not implemented - see https://issues.apache.org/jira/browse/IMAP-370"); - } - - @Override - protected MessageMetaData copy(Mailbox mailbox, MessageUid uid, long modSeq, MailboxMessage original) - throws MailboxException { - try { - String newMessagePath = getSession().getNodeByIdentifier(mailbox.getMailboxId().serialize()).getPath() + NODE_DELIMITER - + String.valueOf(uid.asLong()); - getSession().getWorkspace().copy( - ((JCRMailboxMessage) original).getNode().getPath(), - getSession().getNodeByIdentifier(mailbox.getMailboxId().serialize()).getPath() + NODE_DELIMITER - + String.valueOf(uid.asLong())); - Node node = getSession().getNode(newMessagePath); - node.setProperty(JCRMailboxMessage.MAILBOX_UUID_PROPERTY, mailbox.getMailboxId().serialize()); - node.setProperty(JCRMailboxMessage.UID_PROPERTY, uid.asLong()); - node.setProperty(JCRMailboxMessage.MODSEQ_PROPERTY, modSeq); - // A copy of a message is recent - // See MAILBOX-85 - node.setProperty(JCRMailboxMessage.RECENT_PROPERTY, true); - return new SimpleMessageMetaData(new JCRMailboxMessage(node, LOGGER)); - } catch (RepositoryException e) { - throw new MailboxException("Unable to copy message " + original + " in mailbox " + mailbox, e); - } - } - - @Override - protected MessageMetaData save(Mailbox mailbox, MailboxMessage message) throws MailboxException { - final JCRMailboxMessage membership = (JCRMailboxMessage) message; - try { - - Node messageNode = null; - - if (membership.isPersistent()) { - messageNode = getSession().getNodeByIdentifier(membership.getId()); - } - - if (messageNode == null) { - - Date date = message.getInternalDate(); - if (date == null) { - date = new Date(); - } - - // extracte the date from the message to create node structure - // later - Calendar cal = Calendar.getInstance(); - cal.setTime(date); - final String year = convertIntToString(cal.get(Calendar.YEAR)); - final String month = convertIntToString(cal.get(Calendar.MONTH) + 1); - final String day = convertIntToString(cal.get(Calendar.DAY_OF_MONTH)); - final String hour = convertIntToString(cal.get(Calendar.HOUR_OF_DAY)); - final String min = convertIntToString(cal.get(Calendar.MINUTE)); - - Node mailboxNode = getSession().getNodeByIdentifier(mailbox.getMailboxId().serialize()); - Node node = mailboxNode; - - if (scaleType > MESSAGE_SCALE_NONE) { - // we lock the whole mailbox with all its childs while - // adding the folder structure for the date - - if (scaleType >= MESSAGE_SCALE_YEAR) { - node = JcrUtils.getOrAddFolder(node, year); - - if (scaleType >= MESSAGE_SCALE_MONTH) { - node = JcrUtils.getOrAddFolder(node, month); - - if (scaleType >= MESSAGE_SCALE_DAY) { - node = JcrUtils.getOrAddFolder(node, day); - - if (scaleType >= MESSAGE_SCALE_HOUR) { - node = JcrUtils.getOrAddFolder(node, hour); - - if (scaleType >= MESSAGE_SCALE_MINUTE) { - node = JcrUtils.getOrAddFolder(node, min); - } - } - } - } - } - - } - - MessageUid uid = membership.getUid(); - messageNode = mailboxNode.addNode(String.valueOf(uid.asLong()), "nt:file"); - messageNode.addMixin("jamesMailbox:message"); - try { - membership.merge(messageNode); - - } catch (IOException e) { - throw new RepositoryException("Unable to merge message in to tree", e); - } - } else { - membership.merge(messageNode); - } - return new SimpleMessageMetaData(membership); - } catch (RepositoryException | IOException e) { - throw new MailboxException("Unable to save message " + message + " in mailbox " + mailbox, e); - } - } - - @Override - public Flags getApplicableFlag(Mailbox mailbox) throws MailboxException { - int maxBatchSize = -1; - try { - return new ApplicableFlagCalculator(findMessagesInMailbox(mailbox, maxBatchSize)) - .computeApplicableFlags(); - } catch (RepositoryException e) { - throw new MailboxException("Unable to get message from in mailbox " + mailbox, e); - } - } - - /** - * Return the path to the mailbox. This path is escaped to be able to use it - * in xpath queries - * - * See http://wiki.apache.org/jackrabbit/EncodingAndEscaping - * - * @param mailbox - * @return - * @throws ItemNotFoundException - * @throws RepositoryException - */ - private String getMailboxPath(Mailbox mailbox) throws ItemNotFoundException, RepositoryException { - return ISO9075.encodePath(getSession().getNodeByIdentifier(mailbox.getMailboxId().serialize()).getPath()); - } - - private List<MailboxMessage> findMessagesInMailboxAfterUID(Mailbox mailbox, MessageUid from, int batchSize) - throws RepositoryException { - List<MailboxMessage> list = new ArrayList<>(); - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.UID_PROPERTY + ">=" + from + "] order by @" + JCRMailboxMessage.UID_PROPERTY; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - Query query = manager.createQuery(queryString, XPATH_LANGUAGE); - if (batchSize > 0) { - query.setLimit(batchSize); - } - QueryResult result = query.execute(); - - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - list.add(new JCRMailboxMessage(iterator.nextNode(), LOGGER)); - } - return list; - } - - private List<MailboxMessage> findMessageInMailboxWithUID(Mailbox mailbox, MessageUid from) - throws RepositoryException { - List<MailboxMessage> list = new ArrayList<>(); - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.UID_PROPERTY + "=" + from + "]"; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - Query query = manager.createQuery(queryString, XPATH_LANGUAGE); - query.setLimit(1); - QueryResult result = query.execute(); - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - list.add(new JCRMailboxMessage(iterator.nextNode(), LOGGER)); - } - return list; - } - - private List<MailboxMessage> findMessagesInMailboxBetweenUIDs(Mailbox mailbox, MessageUid from, MessageUid to, - int batchSize) throws RepositoryException { - List<MailboxMessage> list = new ArrayList<>(); - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.UID_PROPERTY + ">=" + from + " and @" + JCRMailboxMessage.UID_PROPERTY + "<=" + to - + "] order by @" + JCRMailboxMessage.UID_PROPERTY; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - Query query = manager.createQuery(queryString, XPATH_LANGUAGE); - if (batchSize > 0) { - query.setLimit(batchSize); - } - QueryResult result = query.execute(); - - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - list.add(new JCRMailboxMessage(iterator.nextNode(), LOGGER)); - } - return list; - } - - private List<MailboxMessage> findMessagesInMailbox(Mailbox mailbox, int batchSize) - throws RepositoryException { - List<MailboxMessage> list = new ArrayList<>(); - - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message) order by @" - + JCRMailboxMessage.UID_PROPERTY; - QueryManager manager = getSession().getWorkspace().getQueryManager(); - Query query = manager.createQuery(queryString, XPATH_LANGUAGE); - if (batchSize > 0) { - query.setLimit(batchSize); - } - QueryResult result = query.execute(); - - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - list.add(new JCRMailboxMessage(iterator.nextNode(), LOGGER)); - } - return list; - } - - private List<MailboxMessage> findDeletedMessagesInMailboxAfterUID(Mailbox mailbox, MessageUid from) - throws RepositoryException { - List<MailboxMessage> list = new ArrayList<>(); - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.UID_PROPERTY + ">=" + from + " and @" + JCRMailboxMessage.DELETED_PROPERTY + "='true'] order by @" - + JCRMailboxMessage.UID_PROPERTY; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - QueryResult result = manager.createQuery(queryString, XPATH_LANGUAGE).execute(); - - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - list.add(new JCRMailboxMessage(iterator.nextNode(), LOGGER)); - } - return list; - } - - private List<MailboxMessage> findDeletedMessageInMailboxWithUID(Mailbox mailbox, MessageUid from) - throws RepositoryException { - List<MailboxMessage> list = new ArrayList<>(); - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.UID_PROPERTY + "=" + from + " and @" + JCRMailboxMessage.DELETED_PROPERTY + "='true']"; - QueryManager manager = getSession().getWorkspace().getQueryManager(); - Query query = manager.createQuery(queryString, XPATH_LANGUAGE); - query.setLimit(1); - QueryResult result = query.execute(); - - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - JCRMailboxMessage member = new JCRMailboxMessage(iterator.nextNode(), LOGGER); - list.add(member); - } - return list; - } - - private List<MailboxMessage> findDeletedMessagesInMailboxBetweenUIDs(Mailbox mailbox, MessageUid from, MessageUid to) - throws RepositoryException { - List<MailboxMessage> list = new ArrayList<>(); - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.UID_PROPERTY + ">=" + from + " and @" + JCRMailboxMessage.UID_PROPERTY + "<=" + to + " and @" - + JCRMailboxMessage.DELETED_PROPERTY + "='true'] order by @" + JCRMailboxMessage.UID_PROPERTY; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - QueryResult result = manager.createQuery(queryString, XPATH_LANGUAGE).execute(); - - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - list.add(new JCRMailboxMessage(iterator.nextNode(), LOGGER)); - } - return list; - } - - private List<MailboxMessage> findDeletedMessagesInMailbox(Mailbox mailbox) throws RepositoryException { - - List<MailboxMessage> list = new ArrayList<>(); - String queryString = "/jcr:root" + getMailboxPath(mailbox) + "//element(*,jamesMailbox:message)[@" - + JCRMailboxMessage.DELETED_PROPERTY + "='true'] order by @" + JCRMailboxMessage.UID_PROPERTY; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - QueryResult result = manager.createQuery(queryString, XPATH_LANGUAGE).execute(); - - NodeIterator iterator = result.getNodes(); - while (iterator.hasNext()) { - JCRMailboxMessage member = new JCRMailboxMessage(iterator.nextNode(), LOGGER); - list.add(member); - } - return list; - } - - /** - * Convert the given int value to a String. If the int value is smaller then - * 9 it will prefix the String with 0. - * - * @param value - * @return stringValue - */ - private String convertIntToString(int value) { - if (value <= 9) { - return "0" + String.valueOf(value); - } else { - return String.valueOf(value); - } - } - -}
http://git-wip-us.apache.org/repos/asf/james-project/blob/876987c8/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java ---------------------------------------------------------------------- diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java deleted file mode 100644 index db4ec9e..0000000 --- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRModSeqProvider.java +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailbox.jcr.mail; - -import javax.jcr.Node; -import javax.jcr.RepositoryException; -import javax.jcr.Session; - -import org.apache.commons.lang3.NotImplementedException; -import org.apache.james.mailbox.MailboxPathLocker; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository; -import org.apache.james.mailbox.jcr.mail.model.JCRMailbox; -import org.apache.james.mailbox.model.MailboxId; -import org.apache.james.mailbox.store.mail.AbstractLockingModSeqProvider; -import org.apache.james.mailbox.store.mail.model.Mailbox; - -public class JCRModSeqProvider extends AbstractLockingModSeqProvider { - - private final MailboxSessionJCRRepository repository; - - public JCRModSeqProvider(MailboxPathLocker locker, MailboxSessionJCRRepository repository) { - super(locker); - this.repository = repository; - } - - @Override - public long highestModSeq(MailboxSession session, Mailbox mailbox) throws MailboxException { - try { - Session s = repository.login(session); - Node node = s.getNodeByIdentifier(mailbox.getMailboxId().serialize()); - return node.getProperty(JCRMailbox.HIGHESTMODSEQ_PROPERTY).getLong(); - } catch (RepositoryException e) { - throw new MailboxException("Unable to get highest mod-sequence for mailbox " + mailbox, e); - } - } - - @Override - protected long lockedNextModSeq(MailboxSession session, Mailbox mailbox) throws MailboxException { - try { - Session s = repository.login(session); - Node node = s.getNodeByIdentifier(mailbox.getMailboxId().serialize()); - long modseq = node.getProperty(JCRMailbox.HIGHESTMODSEQ_PROPERTY).getLong(); - modseq++; - node.setProperty(JCRMailbox.HIGHESTMODSEQ_PROPERTY, modseq); - s.save(); - return modseq; - } catch (RepositoryException e) { - throw new MailboxException("Unable to consume next uid for mailbox " + mailbox, e); - } - } - - @Override - public long highestModSeq(MailboxSession session, MailboxId mailboxId) throws MailboxException { - throw new NotImplementedException("Not implemented"); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/876987c8/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java ---------------------------------------------------------------------- diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java deleted file mode 100644 index f26e187..0000000 --- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/JCRUidProvider.java +++ /dev/null @@ -1,77 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailbox.jcr.mail; - -import java.util.Optional; -import javax.jcr.Node; -import javax.jcr.RepositoryException; -import javax.jcr.Session; - -import org.apache.james.mailbox.MailboxPathLocker; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository; -import org.apache.james.mailbox.jcr.mail.model.JCRMailbox; -import org.apache.james.mailbox.store.mail.AbstractLockingUidProvider; -import org.apache.james.mailbox.store.mail.model.Mailbox; - -public class JCRUidProvider extends AbstractLockingUidProvider { - - private final MailboxSessionJCRRepository repository; - - public JCRUidProvider(MailboxPathLocker locker, MailboxSessionJCRRepository repository) { - super(locker); - this.repository = repository; - } - - @Override - public Optional<MessageUid> lastUid(MailboxSession mailboxSession, Mailbox mailbox) throws MailboxException { - try { - Session s = repository.login(mailboxSession); - Node node = s.getNodeByIdentifier(mailbox.getMailboxId().serialize()); - long rawUid = node.getProperty(JCRMailbox.LASTUID_PROPERTY).getLong(); - if (rawUid == 0) { - return Optional.empty(); - } - return Optional.of(MessageUid.of(rawUid)); - } catch (RepositoryException e) { - throw new MailboxException("Unable to get last uid for mailbox " + mailbox, e); - } - - } - - @Override - protected MessageUid lockedNextUid(MailboxSession session, Mailbox mailbox) throws MailboxException { - try { - Session s = repository.login(session); - Node node = s.getNodeByIdentifier(mailbox.getMailboxId().serialize()); - MessageUid uid = MessageUid.of(node.getProperty(JCRMailbox.LASTUID_PROPERTY).getLong()); - MessageUid nextUid = uid.next(); - node.setProperty(JCRMailbox.LASTUID_PROPERTY, nextUid.asLong()); - s.save(); - return nextUid; - } catch (RepositoryException e) { - throw new MailboxException("Unable to consume next uid for mailbox " + mailbox, e); - } - } - - - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/876987c8/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java ---------------------------------------------------------------------- diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java deleted file mode 100644 index 85feb13..0000000 --- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailbox.java +++ /dev/null @@ -1,304 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailbox.jcr.mail.model; - -import javax.jcr.Node; -import javax.jcr.RepositoryException; - -import org.apache.jackrabbit.JcrConstants; -import org.apache.jackrabbit.util.Text; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.jcr.JCRId; -import org.apache.james.mailbox.jcr.JCRImapConstants; -import org.apache.james.mailbox.jcr.Persistent; -import org.apache.james.mailbox.model.MailboxACL; -import org.apache.james.mailbox.model.MailboxId; -import org.apache.james.mailbox.model.MailboxPath; -import org.apache.james.mailbox.store.mail.model.Mailbox; -import org.apache.james.mailbox.store.mail.model.MailboxUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -/** - * JCR implementation of a {@link Mailbox} - */ -public class JCRMailbox implements Mailbox, JCRImapConstants, Persistent { - - private static final Logger LOGGER = LoggerFactory.getLogger(JCRMailbox.class); - - private static final String TAB = " "; - - - public static final String USER_PROPERTY = "jamesMailbox:mailboxUser"; - public static final String NAMESPACE_PROPERTY = "jamesMailbox:mailboxNamespace"; - public static final String NAME_PROPERTY = "jamesMailbox:mailboxName"; - public static final String UIDVALIDITY_PROPERTY = "jamesMailbox:mailboxUidValidity"; - public static final String LASTUID_PROPERTY = "jamesMailbox:mailboxLastUid"; - public static final String HIGHESTMODSEQ_PROPERTY = "jamesMailbox:mailboxHighestModSeq"; - - private String name; - private long uidValidity; - private Node node; - - - private String namespace; - private String user; - private long lastKnownUid; - private long highestKnownModSeq; - - - public JCRMailbox(final MailboxPath path, long uidValidity) { - this.name = path.getName(); - this.namespace = path.getNamespace(); - this.user = path.getUser(); - this.uidValidity = uidValidity; - } - - public JCRMailbox(final Node node) { - this.node = node; - } - - @Override - public MailboxPath generateAssociatedPath() { - return new MailboxPath(getNamespace(), getUser(), getName()); - } - - @Override - public String getName() { - if (isPersistent()) { - try { - return node.getProperty(NAME_PROPERTY).getString(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + NAME_PROPERTY, e); - } - } - return name; - } - - @Override - public long getUidValidity() { - if (isPersistent()) { - try { - return node.getProperty(UIDVALIDITY_PROPERTY).getLong(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + UIDVALIDITY_PROPERTY, e); - } - } - return uidValidity; - } - - @Override - public void setName(String name) { - if (isPersistent()) { - try { - node.setProperty(NAME_PROPERTY, name); - // move the node - // See https://issues.apache.org/jira/browse/IMAP-162 - node.getSession().move(node.getPath(), node.getParent().getPath() + NODE_DELIMITER + Text.escapePath(name)); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + NAME_PROPERTY, e); - } - } else { - this.name = name; - } - } - - - @Override - public Node getNode() { - return node; - } - - @Override - public boolean isPersistent() { - return node != null; - } - - @Override - public void merge(Node node) throws RepositoryException { - node.setProperty(NAME_PROPERTY, getName()); - node.setProperty(UIDVALIDITY_PROPERTY, getUidValidity()); - String user = getUser(); - if (user == null) { - user = ""; - } - node.setProperty(USER_PROPERTY, user); - node.setProperty(NAMESPACE_PROPERTY, getNamespace()); - node.setProperty(HIGHESTMODSEQ_PROPERTY, getHighestModSeq()); - node.setProperty(LASTUID_PROPERTY, getLastUid()); - this.node = node; - } - - @Override - public String toString() { - return "Mailbox ( " - + "mailboxUID = " + this.getMailboxId() + TAB - + "name = " + this.getName() + TAB - + "uidValidity = " + this.getUidValidity() + TAB - + " )"; - } - - @Override - public int hashCode() { - final int PRIME = 31; - int result = 1; - result = PRIME * result + (int) getMailboxId().hashCode(); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final JCRMailbox other = (JCRMailbox) obj; - if (getMailboxId() != null) { - if (!getMailboxId().equals(other.getMailboxId())) { - return false; - } - } else { - if (other.getMailboxId() != null) { - return false; - } - } - return true; - } - - @Override - public JCRId getMailboxId() { - if (isPersistent()) { - try { - return JCRId.of(node.getIdentifier()); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + JcrConstants.JCR_UUID, e); - } - } - return null; - } - - @Override - public void setMailboxId(MailboxId mailboxId) { - - } - - @Override - public String getNamespace() { - if (isPersistent()) { - try { - return node.getProperty(NAMESPACE_PROPERTY).getString(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + NAMESPACE_PROPERTY, e); - } - } - return namespace; - } - - @Override - public String getUser() { - if (isPersistent()) { - try { - String user = node.getProperty(USER_PROPERTY).getString(); - if (user.trim().length() == 0) { - return null; - } else { - return user; - } - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + USER_PROPERTY, e); - } - } - return user; - } - - @Override - public void setNamespace(String namespace) { - if (isPersistent()) { - try { - node.setProperty(NAMESPACE_PROPERTY, namespace); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + NAMESPACE_PROPERTY, e); - } - } else { - this.namespace = namespace; - } - } - - - @Override - public void setUser(String user) { - if (isPersistent()) { - try { - if (user == null) { - user = ""; - } - node.setProperty(USER_PROPERTY, user); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + NAME_PROPERTY, e); - } - } else { - this.user = user; - } - } - - private long getLastUid() { - if (isPersistent()) { - try { - return node.getProperty(LASTUID_PROPERTY).getLong(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + LASTUID_PROPERTY, e); - } - } - return lastKnownUid; - } - - private long getHighestModSeq() { - if (isPersistent()) { - try { - return node.getProperty(HIGHESTMODSEQ_PROPERTY).getLong(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + HIGHESTMODSEQ_PROPERTY, e); - } - } - return highestKnownModSeq; - } - - @Override - public MailboxACL getACL() { - // TODO ACL support - return MailboxACL.OWNER_FULL_ACL; - } - - @Override - public void setACL(MailboxACL acl) { - // TODO ACL support - } - - @Override - public boolean isChildOf(Mailbox potentialParent, MailboxSession mailboxSession) { - return MailboxUtil.isMailboxChildOf(this, potentialParent, mailboxSession); - } - -} http://git-wip-us.apache.org/repos/asf/james-project/blob/876987c8/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailboxMessage.java ---------------------------------------------------------------------- diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailboxMessage.java b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailboxMessage.java deleted file mode 100644 index ff095e9..0000000 --- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRMailboxMessage.java +++ /dev/null @@ -1,720 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailbox.jcr.mail.model; - -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Comparator; -import java.util.Date; -import java.util.List; - -import javax.jcr.Binary; -import javax.jcr.Node; -import javax.jcr.NodeIterator; -import javax.jcr.RepositoryException; -import javax.mail.Flags; -import javax.mail.internet.SharedInputStream; -import javax.mail.util.SharedByteArrayInputStream; - -import org.apache.commons.io.IOUtils; -import org.apache.commons.io.input.BoundedInputStream; -import org.apache.jackrabbit.JcrConstants; -import org.apache.jackrabbit.commons.JcrUtils; -import org.apache.james.mailbox.MessageUid; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.jcr.JCRId; -import org.apache.james.mailbox.jcr.JCRImapConstants; -import org.apache.james.mailbox.jcr.Persistent; -import org.apache.james.mailbox.model.ComposedMessageId; -import org.apache.james.mailbox.model.ComposedMessageIdWithMetaData; -import org.apache.james.mailbox.model.MessageAttachment; -import org.apache.james.mailbox.model.MessageId; -import org.apache.james.mailbox.store.mail.model.FlagsFactory; -import org.apache.james.mailbox.store.mail.model.MailboxMessage; -import org.apache.james.mailbox.store.mail.model.Property; -import org.apache.james.mailbox.store.mail.model.impl.MessageParser; -import org.apache.james.mailbox.store.mail.model.impl.PropertyBuilder; -import org.apache.james.mailbox.store.search.comparator.UidComparator; -import org.apache.james.mime4j.MimeException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.github.steveash.guavate.Guavate; - -public class JCRMailboxMessage implements MailboxMessage, JCRImapConstants, Persistent { - - private static final Logger LOGGER = LoggerFactory.getLogger(JCRMailboxMessage.class); - - private static final Comparator<MailboxMessage> MESSAGE_UID_COMPARATOR = new UidComparator(); - - private Node node; - private SharedInputStream content; - private String mediaType; - private Long textualLineCount; - private String subType; - private List<JCRProperty> properties; - private int bodyStartOctet; - - private JCRId mailboxUUID; - private MessageUid uid; - private MessageId messageId; - private Date internalDate; - private long size; - private boolean answered; - private boolean deleted; - private boolean draft; - private boolean flagged; - private boolean recent; - private boolean seen; - private String[] userFlags; - private long modSeq; - - private static final String TOSTRING_SEPARATOR = " "; - - public static final String MAILBOX_UUID_PROPERTY = "jamesMailbox:mailboxUUID"; - public static final String UID_PROPERTY = "jamesMailbox:uid"; - public static final String SIZE_PROPERTY = "jamesMailbox:size"; - public static final String ANSWERED_PROPERTY = "jamesMailbox:answered"; - public static final String DELETED_PROPERTY = "jamesMailbox:deleted"; - public static final String DRAFT_PROPERTY = "jamesMailbox:draft"; - public static final String FLAGGED_PROPERTY = "jamesMailbox:flagged"; - public static final String USERFLAGS_PROPERTY = "jamesMailbox:userFlags"; - - public static final String RECENT_PROPERTY = "jamesMailbox:recent"; - public static final String SEEN_PROPERTY = "jamesMailbox:seen"; - public static final String INTERNAL_DATE_PROPERTY = "jamesMailbox:internalDate"; - - public static final String BODY_START_OCTET_PROPERTY = "jamesMailbox:messageBodyStartOctet"; - public static final String HEADER_NODE_TYPE = "jamesMailbox:messageHeader"; - - public static final String PROPERTY_NODE_TYPE = "jamesMailbox:messageProperty"; - public static final String TEXTUAL_LINE_COUNT_PROPERTY = "jamesMailbox:messageTextualLineCount"; - public static final String SUBTYPE_PROPERTY = "jamesMailbox:messageSubType"; - public static final String MODSEQ_PROPERTY = "jamesMailbox:modSeq"; - - public JCRMailboxMessage(Node node, Logger logger) { - this.node = node; - } - - public JCRMailboxMessage(JCRId mailboxUUID, MessageId messageId, Date internalDate, int size, Flags flags, SharedInputStream content, - int bodyStartOctet, PropertyBuilder propertyBuilder) { - super(); - this.mailboxUUID = mailboxUUID; - this.messageId = messageId; - this.internalDate = internalDate; - this.size = size; - setFlags(flags); - this.content = content; - - this.bodyStartOctet = bodyStartOctet; - this.textualLineCount = propertyBuilder.getTextualLineCount(); - this.mediaType = propertyBuilder.getMediaType(); - this.subType = propertyBuilder.getSubType(); - final List<Property> properties = propertyBuilder.toProperties(); - this.properties = new ArrayList<>(properties.size()); - for (Property property:properties) { - this.properties.add(new JCRProperty(property)); - } - - } - - /** - * Create a copy of the given message - */ - public JCRMailboxMessage(JCRId mailboxUUID, MessageUid uid, MessageId messageId, long modSeq, JCRMailboxMessage message) throws MailboxException { - this.mailboxUUID = mailboxUUID; - this.messageId = messageId; - this.internalDate = message.getInternalDate(); - this.size = message.getFullContentOctets(); - setFlags(message.createFlags()); - this.uid = uid; - this.modSeq = modSeq; - try { - this.content = new SharedByteArrayInputStream(IOUtils.toByteArray(message.getFullContent())); - } catch (IOException e) { - throw new MailboxException("Unable to parse message",e); - } - - this.bodyStartOctet = (int) (message.getFullContentOctets() - message.getBodyOctets()); - - PropertyBuilder pBuilder = new PropertyBuilder(message.getProperties()); - this.textualLineCount = message.getTextualLineCount(); - this.mediaType = message.getMediaType(); - this.subType = message.getSubType(); - final List<Property> properties = pBuilder.toProperties(); - this.properties = new ArrayList<>(properties.size()); - for (Property property:properties) { - this.properties.add(new JCRProperty(property)); - } - } - - @Override - public ComposedMessageIdWithMetaData getComposedMessageIdWithMetaData() { - return ComposedMessageIdWithMetaData.builder() - .modSeq(modSeq) - .flags(createFlags()) - .composedMessageId(new ComposedMessageId(getMailboxId(), getMessageId(), uid)) - .build(); - } - - @Override - public long getFullContentOctets() { - if (isPersistent()) { - try { - return node.getProperty(SIZE_PROPERTY).getLong(); - } catch (RepositoryException e) { - LOGGER.error("Unable to retrieve property " + SIZE_PROPERTY, e); - - } - return 0; - } - return size; - } - - @Override - public String getMediaType() { - if (isPersistent()) { - try { - return node.getNode(JcrConstants.JCR_CONTENT).getProperty(JcrConstants.JCR_MIMETYPE).getString(); - } catch (RepositoryException e) { - LOGGER.error("Unable to retrieve node " + JcrConstants.JCR_MIMETYPE, e); - } - return null; - } - return mediaType; - } - - @Override - public List<Property> getProperties() { - if (isPersistent()) { - try { - List<Property> properties = new ArrayList<>(); - NodeIterator nodeIt = node.getNodes("messageProperty"); - while (nodeIt.hasNext()) { - properties.add(new JCRProperty(nodeIt.nextNode())); - } - return properties; - } catch (RepositoryException e) { - LOGGER.error("Unable to retrieve nodes messageProperty", e); - } - } - return new ArrayList<>(properties); - } - - @Override - public String getSubType() { - if (isPersistent()) { - try { - return node.getProperty(SUBTYPE_PROPERTY).getString(); - } catch (RepositoryException e) { - LOGGER.error("Unable to retrieve node " + SUBTYPE_PROPERTY, e); - } - return null; - } - return subType; - } - - @Override - public long getBodyOctets() { - return getFullContentOctets() - getBodyStartOctet(); - } - - @Override - public Long getTextualLineCount() { - if (isPersistent()) { - try { - if (node.hasProperty(TEXTUAL_LINE_COUNT_PROPERTY)) { - return node.getProperty(TEXTUAL_LINE_COUNT_PROPERTY).getLong(); - } - } catch (RepositoryException e) { - LOGGER.error("Unable to retrieve property " + TEXTUAL_LINE_COUNT_PROPERTY, e); - - } - return null; - } - return textualLineCount; - } - - @Override - public Node getNode() { - return node; - } - - @Override - public boolean isPersistent() { - return node != null; - } - - public String getUUID() { - if (isPersistent()) { - try { - return node.getIdentifier(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access UUID", e); - } - } - return null; - } - - @Override - public void merge(Node node) throws RepositoryException, IOException { - - // update the flags - node.setProperty(ANSWERED_PROPERTY, isAnswered()); - node.setProperty(DELETED_PROPERTY, isDeleted()); - node.setProperty(DRAFT_PROPERTY, isDraft()); - node.setProperty(FLAGGED_PROPERTY, isFlagged()); - node.setProperty(RECENT_PROPERTY, isRecent()); - node.setProperty(SEEN_PROPERTY, isSeen()); - node.setProperty(USERFLAGS_PROPERTY, createFlags().getUserFlags()); - // This stuff is only ever changed on a new message - // so if it is persistent we don'T need to set all the of this. - // - // This also fix https://issues.apache.org/jira/browse/IMAP-159 - if (isPersistent() == false) { - node.setProperty(SIZE_PROPERTY, getFullContentOctets()); - node.setProperty(MAILBOX_UUID_PROPERTY, getMailboxId().serialize()); - node.setProperty(UID_PROPERTY, getUid().asLong()); - node.setProperty(MODSEQ_PROPERTY, getModSeq()); - - if (getInternalDate() == null) { - internalDate = new Date(); - } - - Calendar cal = Calendar.getInstance(); - - cal.setTime(getInternalDate()); - node.setProperty(INTERNAL_DATE_PROPERTY, cal); - - Node contentNode = JcrUtils.getOrAddNode(node, JcrConstants.JCR_CONTENT, JcrConstants.NT_RESOURCE); - Binary binaryContent = contentNode.getSession().getValueFactory().createBinary(getFullContent()); - contentNode.setProperty(JcrConstants.JCR_DATA, binaryContent); - contentNode.setProperty(JcrConstants.JCR_MIMETYPE, getMediaType()); - - if (getTextualLineCount() != null) { - node.setProperty(TEXTUAL_LINE_COUNT_PROPERTY, getTextualLineCount()); - } - node.setProperty(SUBTYPE_PROPERTY, getSubType()); - node.setProperty(BODY_START_OCTET_PROPERTY, getBodyStartOctet()); - - - List<Property> currentProperties = getProperties(); - List<Property> newProperties = currentProperties.stream() - .map(JCRProperty::new) - .collect(Guavate.toImmutableList()); - // remove old properties, we will add a bunch of new ones - NodeIterator iterator = node.getNodes("messageProperty"); - while (iterator.hasNext()) { - iterator.nextNode().remove(); - } - - // store new properties - for (Property newProperty : newProperties) { - JCRProperty prop = (JCRProperty) newProperty; - Node propNode = node.addNode("messageProperty", "nt:unstructured"); - propNode.addMixin(PROPERTY_NODE_TYPE); - prop.merge(propNode); - } - } - this.node = node; - - } - - private int getBodyStartOctet() { - if (isPersistent()) { - try { - return (int)node.getProperty(BODY_START_OCTET_PROPERTY).getLong(); - } catch (RepositoryException e) { - LOGGER.error("Unable to retrieve property " + TEXTUAL_LINE_COUNT_PROPERTY, e); - - } - return 0; - } - return bodyStartOctet; - } - - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - - final JCRMailboxMessage other = (JCRMailboxMessage) obj; - - if (getUUID() != null) { - if (!getUUID().equals(other.getUUID())) { - return false; - } - } else { - if (other.getUUID() != null) { - return false; - } - } - if (getMailboxId() != null) { - if (!getMailboxId().equals(other.getMailboxId())) { - return false; - } - } else { - if (other.getMailboxId() != null) { - return false; - } - } - if (getId() != null) { - if (!getId().equals(other.getId())) { - return false; - } - } else { - if (other.getId() != null) { - return false; - } - } - return true; - } - - - @Override - public MessageId getMessageId() { - return messageId; - } - - @Override - public Date getInternalDate() { - if (isPersistent()) { - try { - if (node.hasProperty(INTERNAL_DATE_PROPERTY)) { - return node.getProperty(INTERNAL_DATE_PROPERTY).getDate().getTime(); - } - - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + FLAGGED_PROPERTY, - e); - } - return null; - } - return internalDate; - } - - @Override - public JCRId getMailboxId() { - if (isPersistent()) { - try { - return JCRId.of(node.getProperty(MAILBOX_UUID_PROPERTY).getString()); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " - + MAILBOX_UUID_PROPERTY, e); - } - } - return mailboxUUID; - } - - - @Override - public MessageUid getUid() { - if (isPersistent()) { - try { - return MessageUid.of(node.getProperty(UID_PROPERTY).getLong()); - - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + UID_PROPERTY, e); - } - return MessageUid.MIN_VALUE; - } - return uid; - } - - @Override - public boolean isAnswered() { - if (isPersistent()) { - try { - if (node.hasProperty(ANSWERED_PROPERTY)) { - return node.getProperty(ANSWERED_PROPERTY).getBoolean(); - } - - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + ANSWERED_PROPERTY, - e); - } - return false; - } - return answered; - } - - @Override - public boolean isDeleted() { - if (isPersistent()) { - try { - if (node.hasProperty(DELETED_PROPERTY)) { - return node.getProperty(DELETED_PROPERTY).getBoolean(); - } - - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + DELETED_PROPERTY, - e); - } - return false; - } - return deleted; - } - - @Override - public boolean isDraft() { - if (isPersistent()) { - try { - if (node.hasProperty(DRAFT_PROPERTY)) { - return node.getProperty(DRAFT_PROPERTY).getBoolean(); - } - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + DRAFT_PROPERTY, e); - } - return false; - } - return draft; - } - - @Override - public boolean isFlagged() { - if (isPersistent()) { - try { - if (node.hasProperty(FLAGGED_PROPERTY)) { - return node.getProperty(FLAGGED_PROPERTY).getBoolean(); - } - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + FLAGGED_PROPERTY, - e); - } - return false; - } - return flagged; - } - - @Override - public boolean isRecent() { - if (isPersistent()) { - try { - if (node.hasProperty(RECENT_PROPERTY)) { - return node.getProperty(RECENT_PROPERTY).getBoolean(); - } - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + RECENT_PROPERTY, e); - } - return false; - } - return recent; - } - - @Override - public boolean isSeen() { - if (isPersistent()) { - try { - return node.getProperty(SEEN_PROPERTY).getBoolean(); - - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + SEEN_PROPERTY, e); - } - return false; - } - return seen; - } - - @Override - public void setFlags(Flags flags) { - if (isPersistent()) { - try { - node.setProperty(ANSWERED_PROPERTY, - flags.contains(Flags.Flag.ANSWERED)); - node.setProperty(DELETED_PROPERTY, - flags.contains(Flags.Flag.DELETED)); - node.setProperty(DRAFT_PROPERTY, - flags.contains(Flags.Flag.DRAFT)); - node.setProperty(FLAGGED_PROPERTY, - flags.contains(Flags.Flag.FLAGGED)); - node.setProperty(RECENT_PROPERTY, - flags.contains(Flags.Flag.RECENT)); - node.setProperty(SEEN_PROPERTY, - flags.contains(Flags.Flag.SEEN)); - node.setProperty(USERFLAGS_PROPERTY, flags.getUserFlags()); - } catch (RepositoryException e) { - LOGGER.error("Unable to set flags", e); - } - } else { - answered = flags.contains(Flags.Flag.ANSWERED); - deleted = flags.contains(Flags.Flag.DELETED); - draft = flags.contains(Flags.Flag.DRAFT); - flagged = flags.contains(Flags.Flag.FLAGGED); - recent = flags.contains(Flags.Flag.RECENT); - seen = flags.contains(Flags.Flag.SEEN); - userFlags = flags.getUserFlags(); - } - } - - @Override - public Flags createFlags() { - return FlagsFactory.createFlags(this, userFlags); - } - - public void unsetRecent() { - if (isPersistent()) { - try { - node.setProperty(RECENT_PROPERTY, false); - - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + RECENT_PROPERTY, e); - } - } else { - recent = false; - } - } - - - public String getId() { - if (isPersistent()) { - try { - return node.getIdentifier(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + JcrConstants.JCR_UUID, e); - } - } - return null; - } - - @Override - public int hashCode() { - final int PRIME = 31; - int result = 1; - result = PRIME * result + getUUID().hashCode(); - result = PRIME * result + getMailboxId().hashCode(); - return result; - } - - - public String toString() { - - return "message(" - + "uuid = " + getUUID() - + "mailboxUUID = " + this.getMailboxId() + TOSTRING_SEPARATOR - + "uuid = " + this.getId() + TOSTRING_SEPARATOR - + "internalDate = " + this.getInternalDate() + TOSTRING_SEPARATOR - + "size = " + this.getFullContentOctets() + TOSTRING_SEPARATOR - + "answered = " + this.isAnswered() + TOSTRING_SEPARATOR - + "deleted = " + this.isDeleted() + TOSTRING_SEPARATOR - + "draft = " + this.isDraft() + TOSTRING_SEPARATOR - + "flagged = " + this.isFlagged() + TOSTRING_SEPARATOR - + "recent = " + this.isRecent() + TOSTRING_SEPARATOR - + "seen = " + this.isSeen() + TOSTRING_SEPARATOR - + " )"; - } - - - @Override - public InputStream getFullContent() throws IOException { - if (isPersistent()) { - try { - //TODO: Maybe we should cache this somehow... - return node.getNode(JcrConstants.JCR_CONTENT).getProperty(JcrConstants.JCR_DATA).getBinary().getStream(); - } catch (RepositoryException e) { - throw new IOException("Unable to retrieve property " + JcrConstants.JCR_CONTENT, e); - } - } - return content.newStream(0, -1); - } - - @Override - public InputStream getBodyContent() throws IOException { - InputStream body = getFullContent(); - IOUtils.skipFully(body, getBodyStartOctet()); - return body; - } - - @Override - public long getModSeq() { - if (isPersistent()) { - try { - return node.getProperty(MODSEQ_PROPERTY).getLong(); - - } catch (RepositoryException e) { - LOGGER.error("Unable to access property " + MODSEQ_PROPERTY, e); - } - return 0; - } - return modSeq; - } - - @Override - public void setModSeq(long modSeq) { - if (isPersistent()) { - try { - node.setProperty(MODSEQ_PROPERTY, modSeq); - } catch (RepositoryException e) { - LOGGER.error("Unable to set mod-sequence", e); - } - } else { - this.modSeq = modSeq; - } - } - - @Override - public void setUid(MessageUid uid) { - if (isPersistent()) { - try { - node.setProperty(UID_PROPERTY, uid.asLong()); - } catch (RepositoryException e) { - LOGGER.error("Unable to set uid", e); - } - } else { - this.uid = uid; - } - } - - @Override - public InputStream getHeaderContent() throws IOException { - long limit = getBodyStartOctet(); - if (limit < 0) { - limit = 0; - } - return new BoundedInputStream(getFullContent(), limit); - } - - @Override - public long getHeaderOctets() { - return getBodyStartOctet(); - } - - @Override - public int compareTo(MailboxMessage other) { - return MESSAGE_UID_COMPARATOR.compare(this, other); - } - - @Override - public List<MessageAttachment> getAttachments() { - try { - return new MessageParser().retrieveAttachments(getFullContent()); - } catch (MimeException | IOException e) { - throw new RuntimeException(e); - } - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/876987c8/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRProperty.java ---------------------------------------------------------------------- diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRProperty.java b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRProperty.java deleted file mode 100644 index 0d935af..0000000 --- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/mail/model/JCRProperty.java +++ /dev/null @@ -1,209 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailbox.jcr.mail.model; - -import javax.jcr.Node; -import javax.jcr.RepositoryException; - -import org.apache.james.mailbox.jcr.JCRImapConstants; -import org.apache.james.mailbox.jcr.Persistent; -import org.apache.james.mailbox.store.mail.model.Property; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * JCR implementation of {@link Property} - * - */ -public class JCRProperty implements JCRImapConstants, Persistent, Property { - - private static final Logger LOGGER = LoggerFactory.getLogger(JCRProperty.class); - - private Node node; - private String namespace; - private String localName; - private String value; - private int order; - - public static final String NAMESPACE_PROPERTY = "jamesMailbox:propertyNamespace"; - public static final String LOCALNAME_PROPERTY = "jamesMailbox:propertyLocalName"; - public static final String VALUE_PROPERTY = "jamesMailbox:propertyValue"; - public static final String ORDER_PROPERTY = "jamesMailbox:propertyOrder"; - - public JCRProperty(Node node) { - this.node = node; - } - - public JCRProperty(String namespace, String localName, String value) { - this.namespace = namespace; - this.localName = localName; - this.value = value; - } - - public JCRProperty(Property property) { - this(property.getNamespace(), property.getLocalName(), property.getValue()); - } - - public int getOrder() { - if (isPersistent()) { - try { - return (int)node.getProperty(ORDER_PROPERTY).getLong(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access Property " + ORDER_PROPERTY, e); - } - return 0; - } - return order; - } - - @Override - public Node getNode() { - return node; - } - - @Override - public String getLocalName() { - if (isPersistent()) { - try { - return node.getProperty(LOCALNAME_PROPERTY).getString(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access Property " + LOCALNAME_PROPERTY, e); - } - return null; - } - return localName; - } - - @Override - public String getNamespace() { - if (isPersistent()) { - try { - return node.getProperty(NAMESPACE_PROPERTY).getString(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access Property " + NAMESPACE_PROPERTY, e); - } - return null; - } - return namespace; - } - - @Override - public String getValue() { - if (isPersistent()) { - try { - return node.getProperty(VALUE_PROPERTY).getString(); - } catch (RepositoryException e) { - LOGGER.error("Unable to access Property " + VALUE_PROPERTY, e); - } - return null; - } - return value; - } - - @Override - public boolean isPersistent() { - return node != null; - } - - @Override - public void merge(Node node) throws RepositoryException { - node.setProperty(NAMESPACE_PROPERTY, getNamespace()); - node.setProperty(ORDER_PROPERTY, getOrder()); - node.setProperty(LOCALNAME_PROPERTY, getLocalName()); - node.setProperty(VALUE_PROPERTY, getValue()); - - this.node = node; - /* - namespace = null; - order = 0; - localName = null; - value = null; - */ - } - - @Override - public int hashCode() { - final int PRIME = 31; - int result = 1; - result = PRIME * result + getLocalName().hashCode(); - result = PRIME * result + getNamespace().hashCode(); - result = PRIME * result + getValue().hashCode(); - - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final JCRProperty other = (JCRProperty) obj; - - if (getLocalName() != null) { - if (!getLocalName().equals(other.getLocalName())) { - return false; - } - } else { - if (other.getLocalName() != null) { - return false; - } - } - if (getNamespace() != null) { - if (!getNamespace().equals(other.getNamespace())) { - return false; - } - } else { - if (other.getNamespace() != null) { - return false; - } - } - if (getValue() != null) { - if (!getValue().equals(other.getValue())) { - return false; - } - } else { - if (other.getValue() != null) { - return false; - } - } - return true; - } - - /** - * Constructs a <code>String</code> with all attributes - * in name = value format. - * - * @return a <code>String</code> representation - * of this object. - */ - public String toString() { - - return "Property ( " - + "localName = " + this.getLocalName() + " " - + "namespace = " + this.getNamespace() + " " - + "value = " + this.getValue() - + " )"; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/876987c8/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/user/JCRSubscriptionMapper.java ---------------------------------------------------------------------- diff --git a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/user/JCRSubscriptionMapper.java b/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/user/JCRSubscriptionMapper.java deleted file mode 100644 index ed0c1f0..0000000 --- a/mailbox/jcr/src/main/java/org/apache/james/mailbox/jcr/user/JCRSubscriptionMapper.java +++ /dev/null @@ -1,170 +0,0 @@ -/**************************************************************** - * Licensed to the Apache Software Foundation (ASF) under one * - * or more contributor license agreements. See the NOTICE file * - * distributed with this work for additional information * - * regarding copyright ownership. The ASF licenses this file * - * to you under the Apache License, Version 2.0 (the * - * "License"); you may not use this file except in compliance * - * with the License. You may obtain a copy of the License at * - * * - * http://www.apache.org/licenses/LICENSE-2.0 * - * * - * Unless required by applicable law or agreed to in writing, * - * software distributed under the License is distributed on an * - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * - * KIND, either express or implied. See the License for the * - * specific language governing permissions and limitations * - * under the License. * - ****************************************************************/ -package org.apache.james.mailbox.jcr.user; - -import java.util.ArrayList; -import java.util.List; - -import javax.jcr.Node; -import javax.jcr.NodeIterator; -import javax.jcr.PathNotFoundException; -import javax.jcr.Property; -import javax.jcr.RepositoryException; -import javax.jcr.Value; -import javax.jcr.query.Query; -import javax.jcr.query.QueryManager; -import javax.jcr.query.QueryResult; - -import org.apache.jackrabbit.commons.JcrUtils; -import org.apache.jackrabbit.util.Text; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.exception.SubscriptionException; -import org.apache.james.mailbox.jcr.AbstractJCRScalingMapper; -import org.apache.james.mailbox.jcr.MailboxSessionJCRRepository; -import org.apache.james.mailbox.jcr.user.model.JCRSubscription; -import org.apache.james.mailbox.model.MailboxConstants; -import org.apache.james.mailbox.store.user.SubscriptionMapper; -import org.apache.james.mailbox.store.user.model.Subscription; - -/** - * JCR implementation of a SubscriptionManager - * - */ -public class JCRSubscriptionMapper extends AbstractJCRScalingMapper implements SubscriptionMapper { - - @SuppressWarnings("deprecation") - private static final String XPATH_LANGUAGE = Query.XPATH; - - public JCRSubscriptionMapper(MailboxSessionJCRRepository repos, MailboxSession session, int scaling) { - super(repos,session, scaling); - } - - @Override - public void delete(Subscription subscription) throws SubscriptionException { - - JCRSubscription sub = (JCRSubscription) subscription; - try { - - Node node = sub.getNode(); - if (node != null) { - Property prop = node.getProperty(JCRSubscription.MAILBOXES_PROPERTY); - Value[] values = prop.getValues(); - List<String> newValues = new ArrayList<>(); - for (Value value : values) { - String m = value.getString(); - if (m.equals(sub.getMailbox()) == false) { - newValues.add(m); - } - } - if (newValues.isEmpty() == false) { - prop.setValue(newValues.toArray(new String[newValues.size()])); - } else { - prop.remove(); - } - } - } catch (PathNotFoundException e) { - // do nothing - } catch (RepositoryException e) { - throw new SubscriptionException(e); - } - - } - - @Override - public Subscription findMailboxSubscriptionForUser(String user, String mailbox) throws SubscriptionException { - try { - String queryString = "/jcr:root/" + MAILBOXES_PATH + "//element(*,jamesMailbox:user)[@" + JCRSubscription.USERNAME_PROPERTY + "='" + user + "'] AND [@" + JCRSubscription.MAILBOXES_PROPERTY + "='" + mailbox + "']"; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - QueryResult result = manager.createQuery(queryString, XPATH_LANGUAGE).execute(); - - NodeIterator nodeIt = result.getNodes(); - if (nodeIt.hasNext()) { - return new JCRSubscription(nodeIt.nextNode(), mailbox); - } - - } catch (PathNotFoundException e) { - // nothing todo here - } catch (RepositoryException e) { - throw new SubscriptionException(e); - } - return null; - - } - - @Override - public List<Subscription> findSubscriptionsForUser(String user) throws SubscriptionException { - List<Subscription> subList = new ArrayList<>(); - try { - String queryString = "/jcr:root/" + MAILBOXES_PATH + "//element(*,jamesMailbox:user)[@" + JCRSubscription.USERNAME_PROPERTY + "='" + user + "']"; - - QueryManager manager = getSession().getWorkspace().getQueryManager(); - QueryResult result = manager.createQuery(queryString, XPATH_LANGUAGE).execute(); - - NodeIterator nodeIt = result.getNodes(); - while (nodeIt.hasNext()) { - Node node = nodeIt.nextNode(); - if (node.hasProperty(JCRSubscription.MAILBOXES_PROPERTY)) { - Value[] values = node.getProperty(JCRSubscription.MAILBOXES_PROPERTY).getValues(); - for (Value value : values) { - subList.add(new JCRSubscription(node, value.getString())); - } - } - } - } catch (PathNotFoundException e) { - // Do nothing just return the empty list later - } catch (RepositoryException e) { - throw new SubscriptionException(e); - } - return subList; - - } - - - - @Override - public void save(Subscription subscription) throws SubscriptionException { - String username = subscription.getUser(); - String mailbox = subscription.getMailbox(); - try { - - Node node = null; - - JCRSubscription sub = (JCRSubscription) findMailboxSubscriptionForUser(username, mailbox); - - // its a new subscription - if (sub == null) { - node = JcrUtils.getOrAddNode(getSession().getRootNode(), MAILBOXES_PATH); - node = JcrUtils.getOrAddNode(node, Text.escapeIllegalJcrChars(MailboxConstants.USER_NAMESPACE)); - - // This is needed to minimize the child nodes a bit - node = createUserPathStructure(node, Text.escapeIllegalJcrChars(username)); - } else { - node = sub.getNode(); - } - - // Copy new properties to the node - ((JCRSubscription)subscription).merge(node); - - } catch (RepositoryException e) { - throw new SubscriptionException(e); - } - } - -} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
