import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
+import org.slf4j.MDC;
import org.sonar.server.platform.Platform;
+import static java.util.Objects.requireNonNull;
+
/**
* A {@link Filter} that puts and removes the HTTP request ID from the {@link org.slf4j.MDC}.
*/
public void destroy() {
// nothing to do
}
+
+ /**
+ * Wraps MDC calls to store the HTTP request ID in the {@link MDC} into an {@link AutoCloseable}.
+ */
+ static class RequestIdMDCStorage implements AutoCloseable {
+ private static final String HTTP_REQUEST_ID_MDC_KEY = "HTTP_REQUEST_ID";
+
+ public RequestIdMDCStorage(String requestId) {
+ MDC.put(HTTP_REQUEST_ID_MDC_KEY, requireNonNull(requestId, "Request ID can't be null"));
+ }
+
+ @Override
+ public void close() {
+ MDC.remove(HTTP_REQUEST_ID_MDC_KEY);
+ }
+ }
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.platform.web.requestid;
-
-import org.slf4j.MDC;
-
-import static java.util.Objects.requireNonNull;
-
-/**
- * Wraps MDC calls to store the HTTP request ID in the {@link MDC} into an {@link AutoCloseable}.
- */
-class RequestIdMDCStorage implements AutoCloseable {
- private static final String HTTP_REQUEST_ID_MDC_KEY = "HTTP_REQUEST_ID";
-
- public RequestIdMDCStorage(String requestId) {
- MDC.put(HTTP_REQUEST_ID_MDC_KEY, requireNonNull(requestId, "Request ID can't be null"));
- }
-
- @Override
- public void close() {
- MDC.remove(HTTP_REQUEST_ID_MDC_KEY);
- }
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.server.platform.web.requestid;
+
+import java.io.IOException;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.MDC;
+import org.sonar.core.platform.ComponentContainer;
+import org.sonar.server.platform.Platform;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class RequestIdFilterTest {
+ private Platform platform = mock(Platform.class);
+ private RequestIdGenerator requestIdGenerator = mock(RequestIdGenerator.class);
+ private ServletRequest servletRequest = mock(ServletRequest.class);
+ private ServletResponse servletResponse = mock(ServletResponse.class);
+ private FilterChain filterChain = mock(FilterChain.class);
+ private RequestIdFilter underTest = new RequestIdFilter(platform);
+
+ @Before
+ public void setUp() throws Exception {
+ ComponentContainer container = new ComponentContainer();
+ container.add(requestIdGenerator);
+ when(platform.getContainer()).thenReturn(container);
+ }
+
+ @Test
+ public void filter_put_id_in_MDC_and_remove_it_after_chain_has_executed() throws IOException, ServletException {
+ String requestId = "request id";
+ when(requestIdGenerator.generate()).thenReturn(requestId);
+ doAnswer(invocation -> assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo(requestId))
+ .when(filterChain)
+ .doFilter(servletRequest, servletResponse);
+
+ underTest.doFilter(servletRequest, servletResponse, filterChain);
+
+ assertThat(MDC.get("HTTP_REQUEST_ID")).isNull();
+ }
+
+ @Test
+ public void filter_put_id_in_MDC_and_remove_it_after_chain_throws_exception() throws IOException, ServletException {
+ RuntimeException exception = new RuntimeException("Simulating chain failing");
+ String requestId = "request id";
+ when(requestIdGenerator.generate()).thenReturn(requestId);
+ doAnswer(invocation -> {
+ assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo(requestId);
+ throw exception;
+ })
+ .when(filterChain)
+ .doFilter(servletRequest, servletResponse);
+
+ try {
+ underTest.doFilter(servletRequest, servletResponse, filterChain);
+ fail("A runtime exception should have been raised");
+ } catch (RuntimeException e) {
+ assertThat(e).isEqualTo(exception);
+ } finally {
+ assertThat(MDC.get("HTTP_REQUEST_ID")).isNull();
+ }
+
+ }
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.platform.web.requestid;
-
-import org.apache.log4j.MDC;
-import org.junit.After;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class RequestIdMDCStorageTest {
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @After
- public void tearDown() throws Exception {
- MDC.clear();
- }
-
- @Test
- public void constructor_fails_with_NPE_when_argument_is_null() {
- expectedException.expect(NullPointerException.class);
- expectedException.expectMessage("Request ID can't be null");
-
- new RequestIdMDCStorage(null);
- }
-
- @Test
- public void constructor_adds_specified_value_in_MDC_under_HTTP_REQUEST_ID_key() {
- new RequestIdMDCStorage("toto");
-
- assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo("toto");
- }
-
- @Test
- public void close_removes_value_from_MDC() {
- RequestIdMDCStorage underTest = new RequestIdMDCStorage("boum");
- assertThat(MDC.get("HTTP_REQUEST_ID")).isEqualTo("boum");
-
- underTest.close();
-
- assertThat(MDC.get("HTTP_REQUEST_ID")).isNull();
- }
-}