Skip to content

Commit

Permalink
JAMES-1869: mailbox functions should limit the size of a mailbox name
Browse files Browse the repository at this point in the history
  • Loading branch information
Quynh Nguyen authored and aduprat committed Jan 26, 2017
1 parent ab03b63 commit e943623
Show file tree
Hide file tree
Showing 15 changed files with 979 additions and 13 deletions.
Expand Up @@ -17,11 +17,19 @@
* under the License. *
****************************************************************/

package org.apache.james.jmap.exceptions;
package org.apache.james.mailbox.exception;

public class MailboxNameException extends RuntimeException {
public class MailboxNameException extends MailboxException {
public MailboxNameException() {
super();
}

public MailboxNameException(String message) {
super(message);
}

public MailboxNameException(String message, Throwable cause) {
super(message, cause);
}

}
@@ -0,0 +1,35 @@
/****************************************************************
* 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.exception;

public class TooLongMailboxNameException extends MailboxNameException {
public TooLongMailboxNameException() {
super();
}

public TooLongMailboxNameException(String message) {
super(message);
}

public TooLongMailboxNameException(String message, Throwable cause) {
super(message, cause);
}

}
Expand Up @@ -45,5 +45,4 @@ public interface MailboxConstants {

/** The maximum number of annotations on a mailbox */
int DEFAULT_LIMIT_ANNOTATIONS_ON_MAILBOX = 10;

}
Expand Up @@ -46,7 +46,9 @@
import org.apache.james.mailbox.exception.BadCredentialsException;
import org.apache.james.mailbox.exception.MailboxException;
import org.apache.james.mailbox.exception.MailboxExistsException;
import org.apache.james.mailbox.exception.MailboxNameException;
import org.apache.james.mailbox.exception.MailboxNotFoundException;
import org.apache.james.mailbox.exception.TooLongMailboxNameException;
import org.apache.james.mailbox.model.MailboxACL;
import org.apache.james.mailbox.model.MailboxAnnotation;
import org.apache.james.mailbox.model.MailboxAnnotationKey;
Expand Down Expand Up @@ -504,6 +506,7 @@ public void createMailbox(MailboxPath mailboxPath, final MailboxSession mailboxS
if (length == 0) {
mailboxSession.getLog().warn("Ignoring mailbox with empty name");
} else {
validateMailboxName(mailboxPath.getName());
if (mailboxPath.getName().charAt(length - 1) == getDelimiter())
mailboxPath.setName(mailboxPath.getName().substring(0, length - 1));
if (mailboxExists(mailboxPath, mailboxSession))
Expand Down Expand Up @@ -541,6 +544,7 @@ public void runVoid() throws MailboxException {
@Override
public void deleteMailbox(final MailboxPath mailboxPath, final MailboxSession session) throws MailboxException {
session.getLog().info("deleteMailbox " + mailboxPath);
validateMailboxName(mailboxPath.getName());
final MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session);

Mailbox mailbox = mapper.execute(new Mapper.Transaction<Mailbox>() {
Expand Down Expand Up @@ -569,6 +573,7 @@ public void renameMailbox(final MailboxPath from, final MailboxPath to, final Ma
final Logger log = session.getLog();
if (log.isDebugEnabled())
log.debug("renameMailbox " + from + " to " + to);
validateMailboxName(to.getName());
if (mailboxExists(to, session)) {
throw new MailboxExistsException(to.toString());
}
Expand Down Expand Up @@ -909,4 +914,10 @@ public boolean hasChildren(MailboxPath mailboxPath, MailboxSession session) thro
Mailbox mailbox = mapper.findMailboxByPath(mailboxPath);
return mapper.hasChildren(mailbox, session.getPathDelimiter());
}

private void validateMailboxName(String mailboxName) throws MailboxNameException {
if (mailboxName.length() >= MailboxConstants.DEFAULT_LIMIT_MAILBOX_NAME_LENGTH) {
throw new TooLongMailboxNameException("too long mailbox name");
}
}
}
Expand Up @@ -28,7 +28,7 @@


public class ByteBufferInputStream extends InputStream {
private final ByteBuffer buffer = ByteBuffer.allocate(16384);
private final ByteBuffer buffer = ByteBuffer.allocate(160384);

private final CharsetEncoder encoder = Charset.forName("ASCII").newEncoder();

Expand Down
Expand Up @@ -61,6 +61,11 @@ public void testCreateUS() throws Exception {
scriptTest("Create", Locale.US);
}

@Test
public void testWithLongMailboxNameUS() throws Exception {
scriptTest("CreateWithLongName", Locale.US);
}

@Test
public void testExamineEmptyUS() throws Exception {
scriptTest("ExamineEmpty", Locale.US);
Expand Down

0 comments on commit e943623

Please sign in to comment.