Skip to content

Commit

Permalink
JAMES-1838 Extract Path building from AbstractMailboxProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Oct 21, 2016
1 parent 8bba513 commit 490bd69
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 57 deletions.
@@ -0,0 +1,78 @@
/****************************************************************
* 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.imap.main;

import org.apache.james.imap.api.ImapSessionUtils;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.model.MailboxConstants;
import org.apache.james.mailbox.model.MailboxPath;

public class PathConverter {

public static PathConverter forSession(ImapSession session) {
return new PathConverter(session);
}

private final ImapSession session;

public PathConverter(ImapSession session) {
this.session = session;
}

public MailboxPath buildFullPath(String mailboxName) {
String namespace = null;
String name = null;
final MailboxSession mailboxSession = ImapSessionUtils.getMailboxSession(session);

if (mailboxName == null || mailboxName.length() == 0) {
return new MailboxPath("", "", "");
}
if (mailboxName.charAt(0) == MailboxConstants.NAMESPACE_PREFIX_CHAR) {
int namespaceLength = mailboxName.indexOf(mailboxSession.getPathDelimiter());
if (namespaceLength > -1) {
namespace = mailboxName.substring(0, namespaceLength);
if (mailboxName.length() > namespaceLength)
name = mailboxName.substring(++namespaceLength);
} else {
namespace = mailboxName;
}
} else {
namespace = MailboxConstants.USER_NAMESPACE;
name = mailboxName;
}
String user = null;
// we only use the user as part of the MailboxPath if its a private user
// namespace
if (namespace.equals(MailboxConstants.USER_NAMESPACE)) {
user = ImapSessionUtils.getUserName(session);
}

// use uppercase for INBOX
//
// See IMAP-349
if (name.equalsIgnoreCase(MailboxConstants.INBOX)) {
name = MailboxConstants.INBOX;
}

return new MailboxPath(namespace, user, name);
}

}
Expand Up @@ -25,6 +25,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.MailboxSession;
import org.apache.james.mailbox.exception.BadCredentialsException;
Expand Down Expand Up @@ -56,7 +57,7 @@ protected void doAuth(String userid, String passwd, ImapSession session, String
final MailboxSession mailboxSession = mailboxManager.login(userid, passwd, session.getLog());
session.authenticated();
session.setAttribute(ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY, mailboxSession);
final MailboxPath inboxPath = buildFullPath(session, MailboxConstants.INBOX);
final MailboxPath inboxPath = PathConverter.forSession(session).buildFullPath(MailboxConstants.INBOX);
if (mailboxManager.mailboxExists(inboxPath, mailboxSession)) {
if (session.getLog().isDebugEnabled()) {
session.getLog().debug("INBOX exists. No need to create it.");
Expand Down
Expand Up @@ -351,44 +351,6 @@ protected void bye(ImapProcessor.Responder responder, HumanReadableText key) {

protected abstract void doProcess(M message, ImapSession session, String tag, ImapCommand command, Responder responder);

public MailboxPath buildFullPath(ImapSession session, String mailboxName) {
String namespace = null;
String name = null;
final MailboxSession mailboxSession = ImapSessionUtils.getMailboxSession(session);

if (mailboxName == null || mailboxName.length() == 0) {
return new MailboxPath("", "", "");
}
if (mailboxName.charAt(0) == MailboxConstants.NAMESPACE_PREFIX_CHAR) {
int namespaceLength = mailboxName.indexOf(mailboxSession.getPathDelimiter());
if (namespaceLength > -1) {
namespace = mailboxName.substring(0, namespaceLength);
if (mailboxName.length() > namespaceLength)
name = mailboxName.substring(++namespaceLength);
} else {
namespace = mailboxName;
}
} else {
namespace = MailboxConstants.USER_NAMESPACE;
name = mailboxName;
}
String user = null;
// we only use the user as part of the MailboxPath if its a private user
// namespace
if (namespace.equals(MailboxConstants.USER_NAMESPACE)) {
user = ImapSessionUtils.getUserName(session);
}

// use uppercase for INBOX
//
// See IMAP-349
if (name.equalsIgnoreCase(MailboxConstants.INBOX)) {
name = MailboxConstants.INBOX;
}

return new MailboxPath(namespace, user, name);
}

/**
* Joins the elements of a mailboxPath together and returns them as a string
*
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.api.process.SelectedMailbox;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.AbstractMessageRangeRequest;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.MailboxSession;
Expand All @@ -56,7 +57,7 @@ abstract protected List<MessageRange> process(final MailboxPath targetMailbox,

@Override
protected void doProcess(M request, ImapSession session, String tag, ImapCommand command, Responder responder) {
final MailboxPath targetMailbox = buildFullPath(session, request.getMailboxName());
final MailboxPath targetMailbox = PathConverter.forSession(session).buildFullPath(request.getMailboxName());
final IdRange[] idSet = request.getIdSet();
final boolean useUids = request.isUseUids();
final SelectedMailbox currentMailbox = session.getSelected();
Expand Down
Expand Up @@ -38,6 +38,7 @@
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.api.process.SearchResUtil;
import org.apache.james.imap.api.process.SelectedMailbox;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.AbstractMailboxSelectionRequest;
import org.apache.james.imap.message.response.ExistsResponse;
import org.apache.james.imap.message.response.RecentResponse;
Expand Down Expand Up @@ -80,7 +81,7 @@ public AbstractSelectionProcessor(Class<M> acceptableClass, ImapProcessor next,
protected void doProcess(M request, ImapSession session, String tag, ImapCommand command, Responder responder) {
final String mailboxName = request.getMailboxName();
try {
final MailboxPath fullMailboxPath = buildFullPath(session, mailboxName);
final MailboxPath fullMailboxPath = PathConverter.forSession(session).buildFullPath(mailboxName);

respond(tag, command, session, fullMailboxPath, request, responder);

Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.api.process.SelectedMailbox;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.AppendRequest;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.MailboxSession;
Expand Down Expand Up @@ -64,7 +65,7 @@ protected void doProcess(AppendRequest request, ImapSession session, String tag,
final InputStream messageIn = request.getMessage();
final Date datetime = request.getDatetime();
final Flags flags = request.getFlags();
final MailboxPath mailboxPath = buildFullPath(session, mailboxName);
final MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(mailboxName);

try {

Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.CreateRequest;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.exception.MailboxException;
Expand All @@ -45,7 +46,7 @@ public CreateProcessor(ImapProcessor next, MailboxManager mailboxManager, Status
* org.apache.james.imap.api.process.ImapProcessor.Responder)
*/
protected void doProcess(CreateRequest request, ImapSession session, String tag, ImapCommand command, Responder responder) {
final MailboxPath mailboxPath = buildFullPath(session, request.getMailboxName());
final MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(request.getMailboxName());
try {
final MailboxManager mailboxManager = getMailboxManager();
mailboxManager.createMailbox(mailboxPath, ImapSessionUtils.getMailboxSession(session));
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.DeleteACLRequest;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.MailboxSession;
Expand Down Expand Up @@ -65,7 +66,7 @@ protected void doProcess(DeleteACLRequest message, ImapSession session, String t
final String identifier = message.getIdentifier();
try {

MailboxPath mailboxPath = buildFullPath(session, mailboxName);
MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(mailboxName);
// Check that mailbox exists
mailboxManager.getMailbox(mailboxPath, mailboxSession);
/*
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.api.process.SelectedMailbox;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.DeleteRequest;
import org.apache.james.mailbox.MailboxManager;
import org.apache.james.mailbox.exception.MailboxException;
Expand All @@ -46,7 +47,7 @@ public DeleteProcessor(ImapProcessor next, MailboxManager mailboxManager, Status
* org.apache.james.imap.api.process.ImapProcessor.Responder)
*/
protected void doProcess(DeleteRequest request, ImapSession session, String tag, ImapCommand command, Responder responder) {
final MailboxPath mailboxPath = buildFullPath(session, request.getMailboxName());
final MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(request.getMailboxName());
try {
final SelectedMailbox selected = session.getSelected();
if (selected != null && selected.getPath().equals(mailboxPath)) {
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.GetACLRequest;
import org.apache.james.imap.message.response.ACLResponse;
import org.apache.james.mailbox.MailboxManager;
Expand Down Expand Up @@ -62,7 +63,7 @@ protected void doProcess(GetACLRequest message, ImapSession session, String tag,
final String mailboxName = message.getMailboxName();
try {

MailboxPath mailboxPath = buildFullPath(session, mailboxName);
MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(mailboxName);
// Check that mailbox exists
MessageManager messageManager = mailboxManager.getMailbox(mailboxPath, mailboxSession);
/*
Expand Down
Expand Up @@ -34,6 +34,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.GetAnnotationRequest;
import org.apache.james.imap.message.response.AnnotationResponse;
import org.apache.james.mailbox.MailboxManager;
Expand Down Expand Up @@ -73,7 +74,7 @@ protected void doProcess(GetAnnotationRequest message, ImapSession session, Stri
private void proceed(GetAnnotationRequest message, ImapSession session, String tag, ImapCommand command, Responder responder) throws MailboxException {
String mailboxName = message.getMailboxName();
Optional<Integer> maxsize = message.getMaxsize();
MailboxPath mailboxPath = buildFullPath(session, mailboxName);
MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(mailboxName);

List<MailboxAnnotation> mailboxAnnotations = getMailboxAnnotations(session, message.getKeys(), message.getDepth(), mailboxPath);
Optional<Integer> maximumOversizedSize = getMaxSizeValue(mailboxAnnotations, maxsize);
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.GetQuotaRootRequest;
import org.apache.james.imap.message.response.QuotaRootResponse;
import org.apache.james.imap.message.response.QuotaResponse;
Expand Down Expand Up @@ -66,7 +67,7 @@ protected void doProcess(GetQuotaRootRequest message, ImapSession session, Strin
final MailboxSession mailboxSession = ImapSessionUtils.getMailboxSession(session);
final MailboxManager mailboxManager = getMailboxManager();

final MailboxPath mailboxPath = buildFullPath(session, message.getMailboxName());
final MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(message.getMailboxName());

// First check mailbox exists
try {
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.LsubRequest;
import org.apache.james.imap.message.response.LSubResponse;
import org.apache.james.mailbox.MailboxManager;
Expand Down Expand Up @@ -62,7 +63,7 @@ private void listSubscriptions(ImapSession session, Responder responder, String
if (isRelative) {
basePath = new MailboxPath(MailboxConstants.USER_NAMESPACE, mailboxSession.getUser().getUserName(), CharsetUtil.decodeModifiedUTF7(finalReferencename));
} else {
basePath = buildFullPath(session, CharsetUtil.decodeModifiedUTF7(finalReferencename));
basePath = PathConverter.forSession(session).buildFullPath(CharsetUtil.decodeModifiedUTF7(finalReferencename));
}

final MailboxQuery expression = new MailboxQuery(basePath, CharsetUtil.decodeModifiedUTF7(mailboxName), mailboxSession.getPathDelimiter());
Expand Down
Expand Up @@ -33,6 +33,7 @@
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.api.process.MailboxType;
import org.apache.james.imap.api.process.MailboxTyper;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.ListRequest;
import org.apache.james.imap.message.response.ListResponse;
import org.apache.james.mailbox.MailboxManager;
Expand Down Expand Up @@ -169,7 +170,7 @@ public MailboxPath getPath() {
if (isRelative) {
basePath = new MailboxPath(MailboxConstants.USER_NAMESPACE, user, finalReferencename);
} else {
basePath = buildFullPath(session, finalReferencename);
basePath = PathConverter.forSession(session).buildFullPath(finalReferencename);
}

results = getMailboxManager().search(new MailboxQuery(basePath, CharsetUtil.decodeModifiedUTF7(mailboxName), mailboxSession.getPathDelimiter()), mailboxSession);
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.ListRightsRequest;
import org.apache.james.imap.message.response.ListRightsResponse;
import org.apache.james.mailbox.MailboxManager;
Expand Down Expand Up @@ -64,7 +65,7 @@ protected void doProcess(ListRightsRequest message, ImapSession session, String
final String identifier = message.getIdentifier();
try {

MailboxPath mailboxPath = buildFullPath(session, mailboxName);
MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(mailboxName);
// Check that mailbox exists
mailboxManager.getMailbox(mailboxPath, mailboxSession);

Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.apache.james.imap.api.message.response.StatusResponseFactory;
import org.apache.james.imap.api.process.ImapProcessor;
import org.apache.james.imap.api.process.ImapSession;
import org.apache.james.imap.main.PathConverter;
import org.apache.james.imap.message.request.MyRightsRequest;
import org.apache.james.imap.message.response.MyRightsResponse;
import org.apache.james.mailbox.MailboxManager;
Expand Down Expand Up @@ -61,7 +62,7 @@ protected void doProcess(MyRightsRequest message, ImapSession session, String ta
final String mailboxName = message.getMailboxName();
try {

MailboxPath mailboxPath = buildFullPath(session, mailboxName);
MailboxPath mailboxPath = PathConverter.forSession(session).buildFullPath(mailboxName);
// Check that mailbox exists
mailboxManager.getMailbox(mailboxPath, mailboxSession);
MailboxACLRights myRights = mailboxManager.myRights(mailboxPath, mailboxSession);
Expand Down

0 comments on commit 490bd69

Please sign in to comment.