Skip to content

Commit

Permalink
Merge branch 'master' into dependency-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangoerdes authored Mar 14, 2024
2 parents cc90736 + af42041 commit 750dfd8
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import com.predic8.membrane.core.interceptor.AbstractInterceptor;
import com.predic8.membrane.core.interceptor.Outcome;

import java.util.*;
import java.util.ArrayList;
import java.util.List;

import static com.predic8.membrane.core.interceptor.Outcome.CONTINUE;

Expand Down Expand Up @@ -57,6 +58,11 @@ public Outcome handleResponse(Exchange exc) throws Exception {
return CONTINUE;
}

@Override
public void handleAbort(Exchange exc) {
accessLogInterceptorService.handleAccessLogging(exc);
}

@SuppressWarnings("unused")
public List<AdditionalVariable> getAdditionalPatternList() {
return additionalVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
package com.predic8.membrane.core.interceptor.log;

import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.http.*;
import com.predic8.membrane.core.http.Message;
import com.predic8.membrane.core.lang.spel.ExchangeEvaluationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.io.IOException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -154,9 +153,7 @@ private Map<String, String> getAdditionalProvidedPattern(Exchange exc) {
private Function<AdditionalVariable, AbstractMap.SimpleEntry<String, String>> additionalPatternToMapEntry(Exchange exc) {
return additionalPattern -> new AbstractMap.SimpleEntry<>(
additionalPattern.getName(),
safe(() -> new SpelExpressionParser()
.parseExpression(additionalPattern.getExpression())
.getValue(new ExchangeEvaluationContext(exc)),
safe(() -> additionalPattern.getExpression().getValue(new ExchangeEvaluationContext(exc)),
additionalPattern.getDefaultValue()
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.annot.Required;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;

@MCElement(name = "additionalVariable", topLevel = false, id = "accessLog-scope")
public class AdditionalVariable {

private String name;
private String expression;
private Expression expression;
private String defaultValue = "-";

public String getExpression() {
public Expression getExpression() {
return expression;
}

Expand All @@ -37,7 +39,8 @@ public String getExpression() {
@Required
@MCAttribute
public void setExpression(String expression) {
this.expression = expression;
this.expression = new SpelExpressionParser()
.parseExpression(expression);
}

public String getName() {
Expand Down
4 changes: 2 additions & 2 deletions distribution/examples/logging/access/log4j2_access.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<PatternLayout pattern="%d{ABSOLUTE} %5p %c{1}:%L - %m%n" />
</Console>
<Console name="ACCESS" target="SYSTEM_OUT">
<PatternLayout pattern="%X{ip} [%X{time.req.received.format}] &quot;%X{http.method} %X{uri} %X{proto}/%X{http.version}&quot; %X{statusCode} %X{req.payload.size} [%X{res.contentType}]%n" />
<PatternLayout pattern="%X{ip} [%X{time.req.received.format}] X-Forwarded-For: %X{forwarded} &quot;%X{http.method} %X{uri} %X{proto}/%X{http.version}&quot; %X{statusCode} %X{req.payload.size} [%X{res.contentType}]%n" />
</Console>
<RollingFile name="ACCESS_FILE" fileName="access.log" filePattern="access.log.%i">
<PatternLayout pattern="%X{ip} [%X{time.req.received.format}] &quot;%X{http.method} %X{uri} %X{proto}/%X{http.version}&quot; %X{statusCode} %X{req.payload.size} [%X{res.contentType}]%n" />
<PatternLayout pattern="%X{ip} [%X{time.req.received.format}] X-Forwarded-For: %X{forwarded} &quot;%X{http.method} %X{uri} %X{proto}/%X{http.version}&quot; %X{statusCode} %X{req.payload.size} [%X{res.contentType}]%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
Expand Down
1 change: 1 addition & 0 deletions distribution/examples/logging/access/proxies.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<api port="2000">
<accessLog>
<additionalVariable name="res.contentType" expression="response?.headers.contentType" />
<additionalVariable name="forwarded" expression="headers['x-forwarded-for']" />
</accessLog>
<target host="api.predic8.de">
<ssl />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.predic8.membrane.examples.tests.loadbalancing.Loadbalancing1StaticTest;
import com.predic8.membrane.examples.tests.loadbalancing.Loadbalancing3ClientTest;
import com.predic8.membrane.examples.tests.loadbalancing.Loadbalancing5MultipleTest;
import com.predic8.membrane.examples.tests.log.AccessLogTest;
import com.predic8.membrane.examples.tests.message_transformation.Json2XmlTest;
import com.predic8.membrane.examples.tests.message_transformation.TransformationUsingJavascriptTest;
import com.predic8.membrane.examples.tests.message_transformation.Xml2JsonTest;
Expand Down Expand Up @@ -66,6 +67,9 @@
XMLValidationTest.class,
SampleSoapServiceTest.class,

// Logging
AccessLogTest.class,

// Transformation
Xml2JsonTest.class,
Json2XmlTest.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import org.junit.jupiter.api.Test;

import static com.predic8.membrane.test.AssertUtils.assertContains;
import static org.junit.jupiter.api.Assertions.*;
import static com.predic8.membrane.test.AssertUtils.getAndAssert200;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AccessLogTest extends DistributionExtractingTestcase {

Expand All @@ -33,7 +33,7 @@ protected String getExampleDirName() {
@Test
void testConsole() throws Exception {
try (var process = startServiceProxyScript()) {
var console = new WaitableConsoleEvent(process, p -> p.equals("127.0.0.1 \"GET / HTTP/1.1\" 200 0 [application/json]"));
var console = new WaitableConsoleEvent(process, p -> p.contains("\"GET / HTTP/1.1\" 200 0 [application/json]"));
getAndAssert200("http://localhost:2000");
assertTrue(console.occurred());
}
Expand All @@ -44,6 +44,14 @@ void testRollingFile() throws Exception {
try (var ignore = startServiceProxyScript()) {
getAndAssert200("http://localhost:2000");
}
assertContains("127.0.0.1 \"GET / HTTP/1.1\" 200 0 [application/json]", readFile("access.log"));
assertContains("\"GET / HTTP/1.1\" 200 0 [application/json]", readFile("access.log"));
}

@Test
void testHeader() throws Exception {
try (var ignore = startServiceProxyScript()) {
getAndAssert200("http://localhost:2000");
}
assertContains("X-Forwarded-For: 127.0.0.1", readFile("access.log"));
}
}

0 comments on commit 750dfd8

Please sign in to comment.