Skip to content

Commit 292f9a2

Browse files
Merge pull request #9 from manojlh/release_v1.1.0
Release v1.1.0
2 parents 60fd7fc + 38f0aaa commit 292f9a2

File tree

2 files changed

+152
-123
lines changed

2 files changed

+152
-123
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<groupId>com.wso2telco</groupId>
2424
<artifactId>logging-extension</artifactId>
2525
<name>logging-extension</name>
26-
<version>1.0.1-SNAPSHOT</version>
26+
<version>1.1.0</version>
2727
<packaging>bundle</packaging>
2828
<dependencies>
2929
<dependency>
@@ -91,6 +91,11 @@
9191
<artifactId>neethi</artifactId>
9292
<version>2.0.4.wso2v5</version>
9393
</dependency>
94+
<dependency>
95+
<groupId>org.json</groupId>
96+
<artifactId>json</artifactId>
97+
<version>20171018</version>
98+
</dependency>
9499
</dependencies>
95100
<repositories>
96101
<repository>

src/main/java/com/wso2telco/logging/PropertyLogHandler.java

Lines changed: 146 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -16,136 +16,160 @@
1616

1717
package com.wso2telco.logging;
1818

19-
import java.util.TreeMap;
2019
import org.apache.axiom.om.impl.llom.OMTextImpl;
2120
import org.apache.commons.logging.Log;
2221
import org.apache.commons.logging.LogFactory;
2322
import org.apache.synapse.MessageContext;
2423
import org.apache.synapse.config.Entry;
2524
import org.apache.synapse.core.axis2.Axis2MessageContext;
2625
import org.apache.synapse.mediators.AbstractMediator;
26+
import org.json.XML;
27+
28+
import java.util.TreeMap;
2729

2830
public class PropertyLogHandler extends AbstractMediator {
2931

30-
private static final String REGISTRY_PATH = "gov:/apimgt/";
31-
private static final String MESSAGE_TYPE = "message.type";
32-
private static final String PAYLOAD_LOGGING_ENABLED = "payload.logging.enabled";
33-
private static final String APPLICATION_ID = "api.ut.application.id";
34-
private static final String API_PUBLISHER = "api.ut.apiPublisher";
35-
private static final String API_NAME = "API_NAME";
36-
private static final String REQUEST = "request";
37-
private static final String RESPONSE = "response";
38-
private static final String API_VERSION ="SYNAPSE_REST_API_VERSION";
39-
private static final String API_CONTEXT = "api.ut.context";
40-
private static final String USER_ID="api.ut.userId";
41-
private static final String JWT="X-JWT-Assertion";
42-
private static final String UUID = "MESSAGE_ID";
43-
private static final String ERROR="error";
44-
private static final String REQUEST_ID = "mife.prop.requestId";
45-
private static final String APPLICATION_NAME = "api.ut.application.name";
46-
private static final String REST_FULL_REQUEST_PATH="REST_FULL_REQUEST_PATH";
47-
private static final String SYNAPSE_REST_API="SYNAPSE_REST_API";
48-
private static final String ERROR_EXCEPTION="ERROR_EXCEPTION";
49-
private static final String API_RESOURCE_CACHE_KEY="API_RESOURCE_CACHE_KEY";
50-
private static final String ERROR_MESSAGE="ERROR_MESSAGE";
51-
private static final String ERROR_CODE="ERROR_CODE";
52-
private static final String HTTP_SC= "HTTP_SC";
53-
54-
private static final Log logHandler = LogFactory.getLog("REQUEST_RESPONSE_LOGGER");
55-
56-
public boolean mediate(MessageContext messageContext) {
57-
58-
boolean isPayloadLoggingEnabled = false;
59-
60-
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext)
61-
.getAxis2MessageContext();
62-
isPayloadLoggingEnabled = extractPayloadLoggingStatus(messageContext);
63-
String direction = (String) axis2MessageContext.getProperty(MESSAGE_TYPE);
64-
if (direction != null && direction.equalsIgnoreCase(REQUEST)) {
65-
logRequestProperties(messageContext, axis2MessageContext, isPayloadLoggingEnabled);
66-
} else if (direction != null && direction.equalsIgnoreCase(RESPONSE)) {
67-
logResponseProperties(messageContext, axis2MessageContext, isPayloadLoggingEnabled);
68-
} else if (direction != null && direction.equalsIgnoreCase(ERROR)) {
69-
logErrorProperties(messageContext, axis2MessageContext, isPayloadLoggingEnabled);
70-
}
71-
72-
return true;
73-
}
74-
75-
private void logRequestProperties(MessageContext messageContext,
76-
org.apache.axis2.context.MessageContext axis2MessageContext, boolean isPayloadLoggingEnabled) {
77-
TreeMap<String,String> headers = (TreeMap<String, String>) axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
78-
String jwtToken=headers.get(JWT);
79-
if (isPayloadLoggingEnabled) {
80-
String requestPayload = messageContext.getEnvelope().getBody().toString();
81-
logHandler.info("TRANSACTION:request,API_REQUEST_ID:"+messageContext.getProperty(UUID)+"" +
82-
",API_NAME:"+messageContext.getProperty(API_NAME)+"" +
83-
",API_PUBLISHER:"+messageContext.getProperty(API_PUBLISHER)+"" +
84-
",API_VERSION:"+messageContext.getProperty(API_VERSION)+
85-
",API_CONTEXT:"+messageContext.getProperty(API_CONTEXT)+
86-
",APPLICATION_NAME:"+messageContext.getProperty(APPLICATION_NAME)+
87-
",APPLICATION_ID:"+(String) messageContext.getProperty(APPLICATION_ID)+"" +
88-
",USER_ID:"+messageContext.getProperty(USER_ID)+
89-
",JWT_TOKEN:"+jwtToken+"" +
90-
",BODY:" + requestPayload.replaceAll("\n",""));
91-
}
92-
}
93-
94-
private void logResponseProperties(MessageContext messageContext,
95-
org.apache.axis2.context.MessageContext axis2MessageContext, boolean isPayloadLoggingEnabled) {
96-
if (isPayloadLoggingEnabled) {
97-
String responsePayload = messageContext.getEnvelope().getBody().toString();
98-
logHandler.info("TRANSACTION:response," +
99-
"API_REQUEST_ID:"+messageContext.getProperty(UUID)+""+
100-
",HTTP_STATUS:"+axis2MessageContext.getProperty(HTTP_SC)+""+
101-
",BODY:" + responsePayload.replaceAll("\n",""));
102-
}
103-
}
104-
105-
private void logErrorProperties(MessageContext messageContext,
106-
org.apache.axis2.context.MessageContext axis2MessageContext, boolean isPayloadLoggingEnabled) {
107-
UniqueIDGenerator.generateAndSetUniqueID("EX", axis2MessageContext);
108-
if (isPayloadLoggingEnabled) {
109-
logHandler.info("TRANSACTION:errorResponse," +
110-
",API_REQUEST_ID:"+axis2MessageContext.getProperty(REQUEST_ID)+
111-
",REQUEST_BODY:"+messageContext.getEnvelope().getBody().toString()+
112-
",REST_FULL_REQUEST_PATH:"+messageContext.getProperty(REST_FULL_REQUEST_PATH)+
113-
",SYNAPSE_REST_API:"+messageContext.getProperty(SYNAPSE_REST_API)+
114-
",SYNAPSE_REST_API_VERSION:"+messageContext.getProperty(API_VERSION)+
115-
",API_RESOURCE_CACHE_KEY:"+messageContext.getProperty(API_RESOURCE_CACHE_KEY)+
116-
",ERROR_EXCEPTION:"+messageContext.getProperty(ERROR_EXCEPTION)+
117-
",APPLICATION_NAME:"+messageContext.getProperty(APPLICATION_NAME)+
118-
",APPLICATION_ID:"+messageContext.getProperty(APPLICATION_ID)+
119-
",ERROR_CODE:"+messageContext.getProperty(ERROR_CODE)+
120-
",HTTP_STATUS:"+axis2MessageContext.getProperty(HTTP_SC)+""+
121-
",ERROR_MESSAGE:"+messageContext.getProperty(ERROR_MESSAGE));
122-
}
123-
}
124-
125-
private boolean extractPayloadLoggingStatus (MessageContext messageContext) {
126-
boolean isPayloadLoggingEnabled = false;
127-
128-
Entry payloadEntry = new Entry(REGISTRY_PATH + PAYLOAD_LOGGING_ENABLED);
129-
130-
OMTextImpl payloadEnableRegistryValue = (OMTextImpl) messageContext.getConfiguration().getRegistry()
131-
.getResource(payloadEntry, null);
132-
133-
if (payloadEnableRegistryValue != null) {
134-
String payloadLogEnabled = payloadEnableRegistryValue.getText();
135-
136-
if (nullOrTrimmed(payloadLogEnabled) != null) {
137-
isPayloadLoggingEnabled = Boolean.valueOf(payloadLogEnabled);
138-
}
139-
}
140-
141-
return isPayloadLoggingEnabled;
142-
}
143-
144-
private static String nullOrTrimmed(String inputString) {
145-
String result = null;
146-
if (inputString != null && inputString.trim().length() > 0) {
147-
result = inputString.trim();
148-
}
149-
return result;
150-
}
32+
private static final String REGISTRY_PATH = "gov:/apimgt/";
33+
private static final String MESSAGE_TYPE = "message.type";
34+
private static final String PAYLOAD_LOGGING_ENABLED = "payload.logging.enabled";
35+
private static final String APPLICATION_ID = "api.ut.application.id";
36+
private static final String API_PUBLISHER = "api.ut.apiPublisher";
37+
private static final String API_NAME = "API_NAME";
38+
private static final String SP_NAME = "api.ut.userName";
39+
private static final String REQUEST = "request";
40+
private static final String RESPONSE = "response";
41+
private static final String API_VERSION = "SYNAPSE_REST_API_VERSION";
42+
private static final String API_CONTEXT = "api.ut.context";
43+
private static final String USER_ID = "api.ut.userId";
44+
private static final String JWT = "X-JWT-Assertion";
45+
private static final String UUID = "MESSAGE_ID";
46+
private static final String ERROR = "error";
47+
private static final String REQUEST_ID = "mife.prop.requestId";
48+
private static final String APPLICATION_NAME = "api.ut.application.name";
49+
private static final String REST_FULL_REQUEST_PATH = "REST_FULL_REQUEST_PATH";
50+
private static final String REST_SUB_REQUEST_PATH = "REST_SUB_REQUEST_PATH";
51+
private static final String SYNAPSE_REST_API = "SYNAPSE_REST_API";
52+
private static final String ERROR_EXCEPTION = "ERROR_EXCEPTION";
53+
private static final String API_RESOURCE_CACHE_KEY = "API_RESOURCE_CACHE_KEY";
54+
private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
55+
private static final String ERROR_CODE = "ERROR_CODE";
56+
private static final String HTTP_SC = "HTTP_SC";
57+
private static final String METHOD = "api.ut.HTTP_METHOD";
58+
private static final String RESPONSE_TIME = "RESPONSE_TIME";
59+
private static final String CONTENT_TYPE = "messageType";
60+
private static final String CONSUMER_KEY = "api.ut.consumerKey";
61+
62+
private static final Log logHandler = LogFactory.getLog("REQUEST_RESPONSE_LOGGER");
63+
64+
public boolean mediate(MessageContext messageContext) {
65+
66+
boolean isPayloadLoggingEnabled = false;
67+
68+
org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext).getAxis2MessageContext();
69+
isPayloadLoggingEnabled = extractPayloadLoggingStatus(messageContext);
70+
String direction = (String) axis2MessageContext.getProperty(MESSAGE_TYPE);
71+
if (direction != null && direction.equalsIgnoreCase(REQUEST)) {
72+
logRequestProperties(messageContext, axis2MessageContext, isPayloadLoggingEnabled);
73+
} else if (direction != null && direction.equalsIgnoreCase(RESPONSE)) {
74+
logResponseProperties(messageContext, axis2MessageContext, isPayloadLoggingEnabled);
75+
} else if (direction != null && direction.equalsIgnoreCase(ERROR)) {
76+
logErrorProperties(messageContext, axis2MessageContext, isPayloadLoggingEnabled);
77+
}
78+
79+
return true;
80+
}
81+
82+
private void logRequestProperties(MessageContext messageContext, org.apache.axis2.context.MessageContext axis2MessageContext, boolean isPayloadLoggingEnabled) {
83+
TreeMap<String, String> headers = (TreeMap<String, String>) axis2MessageContext.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
84+
//String jwtToken = headers.get(JWT);
85+
if (isPayloadLoggingEnabled) {
86+
String requestPayload = messageContext.getEnvelope().getBody().toString();
87+
logHandler.info("TRANSACTION:request,API_REQUEST_ID:" + messageContext.getProperty(UUID) + "" +
88+
",API_NAME:" + messageContext.getProperty(API_NAME) + "" +
89+
",SP_NAME:" + messageContext.getProperty(SP_NAME) + "" +
90+
",API_PUBLISHER:" + messageContext.getProperty(API_PUBLISHER) + "" +
91+
",API_VERSION:" + messageContext.getProperty(API_VERSION) +
92+
",API_CONTEXT:" + messageContext.getProperty(API_CONTEXT) +
93+
",APPLICATION_NAME:" + messageContext.getProperty(APPLICATION_NAME) +
94+
",APPLICATION_ID:" + (String) messageContext.getProperty(APPLICATION_ID) + "" +
95+
",CONSUMER_KEY:" + messageContext.getProperty(CONSUMER_KEY) +
96+
",API_RESOURCE_PATH:" + messageContext.getProperty(REST_SUB_REQUEST_PATH) +
97+
",METHOD:" + messageContext.getProperty(METHOD) +
98+
//",JWT_TOKEN:" + jwtToken + "" + /*removed the JWT token from the log*/
99+
",BODY:" + requestPayload.replaceAll("\n", ""));
100+
}
101+
}
102+
103+
private void logResponseProperties(MessageContext messageContext, org.apache.axis2.context.MessageContext axis2MessageContext, boolean isPayloadLoggingEnabled) {
104+
if (isPayloadLoggingEnabled) {
105+
String responsePayload = messageContext.getEnvelope().getBody().toString();
106+
logHandler.info("TRANSACTION:response," +
107+
"API_REQUEST_ID:" + messageContext.getProperty(UUID) + "" +
108+
",HTTP_STATUS:" + axis2MessageContext.getProperty(HTTP_SC) + "" +
109+
",RESPONSE_TIME:" + messageContext.getProperty(RESPONSE_TIME) + "" +
110+
",BODY:" + responsePayload.replaceAll("\n", ""));
111+
}
112+
}
113+
114+
private void logErrorProperties(MessageContext messageContext, org.apache.axis2.context.MessageContext axis2MessageContext, boolean isPayloadLoggingEnabled) {
115+
UniqueIDGenerator.generateAndSetUniqueID("EX", axis2MessageContext);
116+
if (isPayloadLoggingEnabled) {
117+
logHandler.info("TRANSACTION:errorResponse," +
118+
",API_REQUEST_ID:" + axis2MessageContext.getProperty(REQUEST_ID) +
119+
",REQUEST_BODY:" + messageContext.getEnvelope().getBody().toString() +
120+
",REST_FULL_REQUEST_PATH:" + messageContext.getProperty(REST_FULL_REQUEST_PATH) +
121+
",SYNAPSE_REST_API:" + messageContext.getProperty(SYNAPSE_REST_API) +
122+
",SYNAPSE_REST_API_VERSION:" + messageContext.getProperty(API_VERSION) +
123+
",API_RESOURCE_CACHE_KEY:" + messageContext.getProperty(API_RESOURCE_CACHE_KEY) +
124+
",ERROR_EXCEPTION:" + messageContext.getProperty(ERROR_EXCEPTION) +
125+
",APPLICATION_NAME:" + messageContext.getProperty(APPLICATION_NAME) +
126+
",APPLICATION_ID:" + messageContext.getProperty(APPLICATION_ID) +
127+
",ERROR_CODE:" + messageContext.getProperty(ERROR_CODE) +
128+
",HTTP_STATUS:" + axis2MessageContext.getProperty(HTTP_SC) + "" +
129+
",ERROR_MESSAGE:" + messageContext.getProperty(ERROR_MESSAGE));
130+
}
131+
}
132+
133+
private boolean extractPayloadLoggingStatus(MessageContext messageContext) {
134+
boolean isPayloadLoggingEnabled = false;
135+
136+
Entry payloadEntry = new Entry(REGISTRY_PATH + PAYLOAD_LOGGING_ENABLED);
137+
138+
OMTextImpl payloadEnableRegistryValue = (OMTextImpl) messageContext.getConfiguration().getRegistry()
139+
.getResource(payloadEntry, null);
140+
141+
if (payloadEnableRegistryValue != null) {
142+
String payloadLogEnabled = payloadEnableRegistryValue.getText();
143+
144+
if (nullOrTrimmed(payloadLogEnabled) != null) {
145+
isPayloadLoggingEnabled = Boolean.valueOf(payloadLogEnabled);
146+
}
147+
}
148+
149+
return isPayloadLoggingEnabled;
150+
}
151+
152+
private static String nullOrTrimmed(String inputString) {
153+
String result = null;
154+
if (inputString != null && inputString.trim().length() > 0) {
155+
result = inputString.trim();
156+
}
157+
return result;
158+
}
159+
160+
/** this method can be used if we need to get extract only json as body**/
161+
private String getPayloadSting(MessageContext messageContext, org.apache.axis2.context.MessageContext axis2MessageContext) {
162+
String payload;
163+
if (axis2MessageContext.getProperty(CONTENT_TYPE).equals("application/json")) {
164+
/**if content type is json */
165+
payload = XML.toJSONObject(messageContext.getEnvelope().getBody().getFirstElement().getFirstElement().toString()).toString();
166+
} else if (axis2MessageContext.getProperty(CONTENT_TYPE).equals("text/plain")) {
167+
/**if content type is text/plain */
168+
payload = messageContext.getEnvelope().getBody().getFirstElement().toString();
169+
} else {
170+
/** if content type is xml */
171+
payload = messageContext.getEnvelope().getBody().toString();
172+
}
173+
return payload;
174+
}
151175
}

0 commit comments

Comments
 (0)