You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ad...@apache.org on 2016/03/01 20:56:34 UTC
[10/10] james-project git commit: JAMES-1695
FirstUserConnectionFilter should only handle authenticated requests
JAMES-1695 FirstUserConnectionFilter should only handle authenticated requests
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/57de0956
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/57de0956
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/57de0956
Branch: refs/heads/master
Commit: 57de09561a9670fb3067f3b4b801ce312edf0381
Parents: 920e261
Author: Matthieu Baechler <ma...@linagora.com>
Authored: Fri Feb 26 10:51:37 2016 +0100
Committer: Antoine Duprat <ad...@linagora.com>
Committed: Tue Mar 1 16:09:33 2016 +0100
----------------------------------------------------------------------
.../james/jmap/FirstUserConnectionFilter.java | 8 ++-
.../jmap/FirstUserConnectionFilterTest.java | 61 ++++++++++++++++++++
2 files changed, 66 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/57de0956/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
index 6af16f6..9a44db5 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
@@ -19,6 +19,7 @@
package org.apache.james.jmap;
import java.io.IOException;
+import java.util.Optional;
import java.util.UUID;
import java.util.function.Function;
@@ -41,6 +42,7 @@ import org.apache.james.user.api.UsersRepositoryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
@@ -52,7 +54,7 @@ public class FirstUserConnectionFilter implements Filter {
private final MailboxManager mailboxManager;
@Inject
- private FirstUserConnectionFilter(UsersRepository usersRepository, MailboxManager mailboxManager) {
+ @VisibleForTesting FirstUserConnectionFilter(UsersRepository usersRepository, MailboxManager mailboxManager) {
this.usersRepository = usersRepository;
this.mailboxManager = mailboxManager;
}
@@ -63,8 +65,8 @@ public class FirstUserConnectionFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
- MailboxSession session = (MailboxSession) request.getAttribute(AuthenticationFilter.MAILBOX_SESSION);
- createAccountIfNeeded(session);
+ Optional<MailboxSession> session = Optional.ofNullable((MailboxSession)request.getAttribute(AuthenticationFilter.MAILBOX_SESSION));
+ session.ifPresent(this::createAccountIfNeeded);
chain.doFilter(request, response);
}
http://git-wip-us.apache.org/repos/asf/james-project/blob/57de0956/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java
new file mode 100644
index 0000000..2da6c0c
--- /dev/null
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java
@@ -0,0 +1,61 @@
+/****************************************************************
+ * 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.jmap;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+import java.io.IOException;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.user.api.UsersRepositoryException;
+import org.apache.james.user.lib.mock.InMemoryUsersRepository;
+import org.junit.Before;
+import org.junit.Test;
+
+public class FirstUserConnectionFilterTest {
+
+ private FirstUserConnectionFilter sut;
+ private InMemoryUsersRepository usersRepository;
+
+ @Before
+ public void setup() {
+ usersRepository = new InMemoryUsersRepository();
+ MailboxManager mailboxManager = mock(MailboxManager.class);
+ sut = new FirstUserConnectionFilter(usersRepository, mailboxManager);
+ }
+
+ @Test
+ public void filterShouldDoNothingOnNullSession() throws IOException, ServletException, UsersRepositoryException {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ HttpServletResponse response = mock(HttpServletResponse.class);
+ FilterChain chain = mock(FilterChain.class);
+ sut.doFilter(request, response, chain);
+ verify(chain).doFilter(request, response);
+ assertThat(usersRepository.list()).isEmpty();
+ }
+
+}
+
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org