Adds the ability to log the HTTP response status code to the log plugin.
MODIFY
log-policy/src/main/apiman/plugin.json
log-policy/src/main/apiman/plugin.json
{
"frameworkVersion" : 1.0,
"name" : "Log Policy Plugin",
- "description" : "This plugin has policies for logging to std out.",
+ "description" : "This plugin has policies for logging HTTP headers.",
"version" : "${project.version}"
}

MODIFY
log-policy/src/main/apiman/policyDefs/schemas/log-policyDef.schema
log-policy/src/main/apiman/policyDefs/schemas/log-policyDef.schema
"title": "Direction",
"description": "You may choose to log either the Request headers, Response headers, or both.",
"enum": [ "request", "response", "both" ]
+ },
+ "logHeaders": {
+ "title": "Log Headers",
+ "description": "Whether to log the HTTP headers.",
+ "type": "boolean",
+ "default": true
+ },
+ "logStatusCode": {
+ "title": "Log Status Code",
+ "description": "Whether to log the HTTP response status code.",
+ "type": "boolean",
+ "default": false
}
}
}

MODIFY
log-policy/src/main/java/io/apiman/plugins/log_policy/LogHeadersPolicy.java
log-policy/src/main/java/io/apiman/plugins/log_policy/LogHeadersPolicy.java
@@ -26,6 +26,7 @@ import io.apiman.plugins.log_policy.beans.LogHeadersConfigBean;
import io.apiman.plugins.log_policy.beans.LogHeadersDirectionType;
import java.util.Map.Entry;
+import java.util.Set;
import java.util.TreeSet;
/**
@@ -75,7 +76,7 @@ public class LogHeadersPolicy extends AbstractMappedPolicy<LogHeadersConfigBean>
IApimanLogger logger = context.getLogger(getClass());
String endpoint = request.getApi().getEndpoint();
context.setAttribute(ENDPOINT_ATTRIBUTE, endpoint);
- if (config.getDirection() != LogHeadersDirectionType.response) {
+ if (config.isLogHeaders() && config.getDirection() != LogHeadersDirectionType.response) {
logHeaders(logger, request.getHeaders(), HttpDirection.REQUEST, endpoint);
}
chain.doApply(request);
@@ -90,8 +91,13 @@ public class LogHeadersPolicy extends AbstractMappedPolicy<LogHeadersConfigBean>
IApimanLogger logger = context.getLogger(getClass());
if (config.getDirection() != LogHeadersDirectionType.request) {
String endpoint = context.getAttribute(ENDPOINT_ATTRIBUTE, ""); //$NON-NLS-1$
+ if (config.isLogStatusCode()) {
+ logger.info(String.format("Status code %d for %s", response.getCode(), endpoint));
+ }
+ if (config.isLogHeaders()) {
logHeaders(logger, response.getHeaders(), HttpDirection.RESPONSE, endpoint);
}
+ }
chain.doApply(response);
}
@@ -103,13 +109,14 @@ public class LogHeadersPolicy extends AbstractMappedPolicy<LogHeadersConfigBean>
* @param endpoint
*/
private void logHeaders(IApimanLogger logger, final HeaderMap headers, final HttpDirection direction, final String endpoint) {
- logger.info(String.format("Logging %d %s headers for %s", headers.size(), direction.getDescription(), endpoint)); //$NON-NLS-1$
- TreeSet<String> sortedKeys = new TreeSet<>(headers.keySet());
+ final StringBuilder sb = new StringBuilder();
+ sb.append(String.format("Logging %d %s headers for %s", headers.size(), direction.getDescription(), endpoint)); //$NON-NLS-1$
+ final Set<String> sortedKeys = new TreeSet<>(headers.keySet());
for (String key : sortedKeys) {
for (Entry<String, String> pair : headers.getAllEntries(key)) {
- logger.info(String.format("Key : %s, Value : %s", pair.getKey(), pair.getValue())); //$NON-NLS-1$
+ sb.append(String.format("\r\nKey : %s, Value : %s", pair.getKey(), pair.getValue())); //$NON-NLS-1$
}
}
+ logger.info(sb.toString());
}
-
}

MODIFY
log-policy/src/main/java/io/apiman/plugins/log_policy/beans/LogHeadersConfigBean.java
log-policy/src/main/java/io/apiman/plugins/log_policy/beans/LogHeadersConfigBean.java
@@ -27,6 +27,19 @@ public class LogHeadersConfigBean {
@JsonProperty
private LogHeadersDirectionType direction;
+ /**
+ * Whether to log the request/response headers.
+ * Defaults to {@code true} for backwards compatibility.
+ */
+ @JsonProperty
+ private boolean logHeaders = true;
+
+ /**
+ * Whether to log the response status code.
+ */
+ @JsonProperty
+ private boolean logStatusCode;
+
/**
* Constructor.
*/
@@ -47,4 +60,17 @@ public class LogHeadersConfigBean {
this.direction = direction;
}
+ /**
+ * @return whether to log the headers
+ */
+ public boolean isLogHeaders() {
+ return logHeaders;
+ }
+
+ /**
+ * @return whether to log the status code
+ */
+ public boolean isLogStatusCode() {
+ return logStatusCode;
+ }
}

MODIFY
log-policy/src/test/java/io/apiman/plugins/log_policy/LogHeadersPolicyTest.java
log-policy/src/test/java/io/apiman/plugins/log_policy/LogHeadersPolicyTest.java
@@ -23,7 +23,7 @@ public class LogHeadersPolicyTest extends ApimanPolicyTest {
* A simple happy flow test to verify the policy does not blow up in our face.
*/
@Test
- @Configuration("{ \"direction\" : \"both\" }")
+ @Configuration("{ \"direction\" : \"both\", \"logStatusCode\" : true }")
public void testLogHeadersWithoutAnyRequestHeaders() throws PolicyFailureError, Throwable {
PrintStream out = System.out;
ByteArrayOutputStream testOutput = new ByteArrayOutputStream();
@@ -35,11 +35,12 @@ public class LogHeadersPolicyTest extends ApimanPolicyTest {
output = redactDates(output);
output = normalize(output);
String expected = "INFO: Logging 0 HTTP Request headers for io.apiman.test.policies.EchoBackEndApi\n" +
+ "INFO: Status code 200 for io.apiman.test.policies.EchoBackEndApi\n" +
"INFO: Logging 4 HTTP Response headers for io.apiman.test.policies.EchoBackEndApi\n" +
- "INFO: Key : Content-Length, Value : 167\n" +
- "INFO: Key : Content-Type, Value : application/json\n" +
- "INFO: Key : Date, Value : XXX\n" +
- "INFO: Key : Server, Value : apiman.policy-test\n" +
+ "Key : Content-Length, Value : 167\n" +
+ "Key : Content-Type, Value : application/json\n" +
+ "Key : Date, Value : XXX\n" +
+ "Key : Server, Value : apiman.policy-test\n" +
"";
Assert.assertEquals(expected, output);
} finally {
@@ -51,7 +52,7 @@ public class LogHeadersPolicyTest extends ApimanPolicyTest {
* A simple happy flow test to verify the policy does not blow up in our face.
*/
@Test
- @Configuration("{ \"direction\" : \"both\" }")
+ @Configuration("{ \"direction\" : \"both\", \"logStatusCode\" : true }")
public void testLogHeadersHappyFlow() throws PolicyFailureError, Throwable {
PrintStream out = System.out;
ByteArrayOutputStream testOutput = new ByteArrayOutputStream();
@@ -66,12 +67,13 @@ public class LogHeadersPolicyTest extends ApimanPolicyTest {
output = redactDates(output);
output = normalize(output);
String expected = "INFO: Logging 1 HTTP Request headers for io.apiman.test.policies.EchoBackEndApi\n" +
- "INFO: Key : X-Test-Name, Value : testGet\n" +
+ "Key : X-Test-Name, Value : testGet\n" +
+ "INFO: Status code 200 for io.apiman.test.policies.EchoBackEndApi\n" +
"INFO: Logging 4 HTTP Response headers for io.apiman.test.policies.EchoBackEndApi\n" +
- "INFO: Key : Content-Length, Value : 199\n" +
- "INFO: Key : Content-Type, Value : application/json\n" +
- "INFO: Key : Date, Value : XXX\n" +
- "INFO: Key : Server, Value : apiman.policy-test\n" +
+ "Key : Content-Length, Value : 199\n" +
+ "Key : Content-Type, Value : application/json\n" +
+ "Key : Date, Value : XXX\n" +
+ "Key : Server, Value : apiman.policy-test\n" +
"";
Assert.assertEquals(expected, output);
} finally {
@@ -98,7 +100,7 @@ public class LogHeadersPolicyTest extends ApimanPolicyTest {
output = redactDates(output);
output = normalize(output);
String expected = "INFO: Logging 1 HTTP Request headers for io.apiman.test.policies.EchoBackEndApi\n" +
- "INFO: Key : X-Test-Name, Value : testGet\n" +
+ "Key : X-Test-Name, Value : testGet\n" +
"";
Assert.assertEquals(expected, output);
} finally {
@@ -110,7 +112,7 @@ public class LogHeadersPolicyTest extends ApimanPolicyTest {
* A simple happy flow test to verify the policy does not blow up in our face.
*/
@Test
- @Configuration("{ \"direction\" : \"response\" }")
+ @Configuration("{ \"direction\" : \"response\", \"logStatusCode\" : true }")
public void testLogHeadersHappyFlowResponseOnly() throws PolicyFailureError, Throwable {
PrintStream out = System.out;
ByteArrayOutputStream testOutput = new ByteArrayOutputStream();
@@ -124,11 +126,12 @@ public class LogHeadersPolicyTest extends ApimanPolicyTest {
String output = testOutput.toString("UTF-8");
output = redactDates(output);
output = normalize(output);
- String expected = "INFO: Logging 4 HTTP Response headers for io.apiman.test.policies.EchoBackEndApi\n" +
- "INFO: Key : Content-Length, Value : 199\n" +
- "INFO: Key : Content-Type, Value : application/json\n" +
- "INFO: Key : Date, Value : XXX\n" +
- "INFO: Key : Server, Value : apiman.policy-test\n" +
+ String expected = "INFO: Status code 200 for io.apiman.test.policies.EchoBackEndApi\n" +
+ "INFO: Logging 4 HTTP Response headers for io.apiman.test.policies.EchoBackEndApi\n" +
+ "Key : Content-Length, Value : 199\n" +
+ "Key : Content-Type, Value : application/json\n" +
+ "Key : Date, Value : XXX\n" +
+ "Key : Server, Value : apiman.policy-test\n" +
"";
Assert.assertEquals(expected, output);
} finally {

Adds the ability to log the HTTP response status code to the log plugin.