-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
server/src/main/java/nl/altindag/server/aspect/LogFromHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2018 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.server.aspect; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.METHOD) | ||
public @interface LogFromHeader { | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
server/src/main/java/nl/altindag/server/aspect/LogFromHeaderAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2018 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.server.aspect; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.EnableAspectJAutoProxy; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import static java.util.Objects.nonNull; | ||
|
||
@Aspect | ||
@Configuration | ||
@EnableAspectJAutoProxy | ||
public class LogFromHeaderAspect { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(LogFromHeaderAspect.class); | ||
private static final String HEADER_KEY_FROM = "from"; | ||
|
||
@Before("@annotation(nl.altindag.server.aspect.LogFromHeader)") | ||
public void logIfPresent() { | ||
String from = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()) | ||
.getRequest() | ||
.getHeader(HEADER_KEY_FROM); | ||
|
||
if (nonNull(from)) { | ||
LOGGER.info("Hello there! It seems like {} is knocking on my door...", from); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
server/src/test/java/nl/altindag/server/aspect/LogFromAspectShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2018 Thunderberry. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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 nl.altindag.server.aspect; | ||
|
||
import nl.altindag.log.LogCaptor; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class LogFromAspectShould { | ||
|
||
private final LogFromHeaderAspect logFromHeaderAspect = new LogFromHeaderAspect(); | ||
|
||
@Test | ||
void logFromHeaderIfPresent() { | ||
LogCaptor logCaptor = LogCaptor.forClass(LogFromHeaderAspect.class); | ||
|
||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
request.addHeader("from", "Foo"); | ||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); | ||
|
||
logFromHeaderAspect.logIfPresent(); | ||
|
||
List<String> logs = logCaptor.getLogs(); | ||
assertThat(logs).containsExactly("Hello there! It seems like Foo is knocking on my door..."); | ||
} | ||
|
||
@Test | ||
void notLogFromHeaderIfAbsent() { | ||
LogCaptor logCaptor = LogCaptor.forClass(LogFromHeaderAspect.class); | ||
|
||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); | ||
|
||
logFromHeaderAspect.logIfPresent(); | ||
|
||
List<String> logs = logCaptor.getLogs(); | ||
assertThat(logs).isEmpty(); | ||
} | ||
|
||
} |