@@ -83,7 +83,8 @@ private void logRequestProperties(MessageContext messageContext, org.apache.axis
8383 TreeMap <String , String > headers = (TreeMap <String , String >) axis2MessageContext .getProperty (org .apache .axis2 .context .MessageContext .TRANSPORT_HEADERS );
8484 //String jwtToken = headers.get(JWT);
8585 if (isPayloadLoggingEnabled ) {
86- String requestPayload = messageContext .getEnvelope ().getBody ().toString ();
86+ // String requestPayload = messageContext.getEnvelope().getBody().toString();
87+ String requestPayload = handleAndReturnPayload (messageContext );
8788 logHandler .info ("TRANSACTION:request,API_REQUEST_ID:" + messageContext .getProperty (UUID ) + "" +
8889 ",API_NAME:" + messageContext .getProperty (API_NAME ) + "" +
8990 ",SP_NAME:" + messageContext .getProperty (SP_NAME ) + "" +
@@ -102,7 +103,8 @@ private void logRequestProperties(MessageContext messageContext, org.apache.axis
102103
103104 private void logResponseProperties (MessageContext messageContext , org .apache .axis2 .context .MessageContext axis2MessageContext , boolean isPayloadLoggingEnabled ) {
104105 if (isPayloadLoggingEnabled ) {
105- String responsePayload = messageContext .getEnvelope ().getBody ().toString ();
106+ // String responsePayload = messageContext.getEnvelope().getBody().toString();
107+ String responsePayload = handleAndReturnPayload (messageContext );
106108 logHandler .info ("TRANSACTION:response," +
107109 "API_REQUEST_ID:" + messageContext .getProperty (UUID ) + "" +
108110 ",HTTP_STATUS:" + axis2MessageContext .getProperty (HTTP_SC ) + "" +
@@ -157,7 +159,23 @@ private static String nullOrTrimmed(String inputString) {
157159 return result ;
158160 }
159161
160- /** this method can be used if we need to get extract only json as body**/
162+ /**
163+ * method used to handle invalid payloads
164+ */
165+ private String handleAndReturnPayload (MessageContext messageContext ) {
166+ String payload = "" ;
167+ try {
168+ payload = messageContext .getEnvelope ().getBody ().toString ();
169+ } catch (Exception e ) {
170+ payload = "payload dropped due to invalid format" ;
171+ } finally {
172+ return payload ;
173+ }
174+ }
175+
176+ /**
177+ * this method can be used if we need to get extract only json as body
178+ **/
161179 private String getPayloadSting (MessageContext messageContext , org .apache .axis2 .context .MessageContext axis2MessageContext ) {
162180 String payload ;
163181 if (axis2MessageContext .getProperty (CONTENT_TYPE ).equals ("application/json" )) {
0 commit comments