Skip to content

Instantly share code, notes, and snippets.

@rrajendran
Created May 5, 2014 22:04
Show Gist options
  • Save rrajendran/0f2936493d41efe4f487 to your computer and use it in GitHub Desktop.
Save rrajendran/0f2936493d41efe4f487 to your computer and use it in GitHub Desktop.
Custom LoggingInInterceptor
import java.io.InputStream;
import java.sql.Timestamp;
import javax.servlet.http.HttpServletRequest;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.transport.http.AbstractHTTPDestination;
import org.apache.log4j.Logger;
public class WSLoggingInInterceptor extends AbstractSoapInterceptor {
private static final Logger LOGGER = Logger.getLogger(WSLoggingInInterceptor.class);
public WSLoggingInInterceptor() {
super(Phase.RECEIVE);
}
@Override
public void handleMessage(SoapMessage message) throws Fault {
// get the remote address
try {
if(LOGGER.isInfoEnabled()){
HttpServletRequest httpRequest = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);
String remoteAddr = httpRequest.getRemoteAddr();
// now get the request xml
InputStream is = message.getContent(InputStream.class);
CachedOutputStream os = new CachedOutputStream();
IOUtils.copy(is, os);
os.flush();
message.setContent(InputStream.class, os.getInputStream());
is.close();
RequestLog requestLog = new RequestLog(remoteAddr, new Timestamp(System.currentTimeMillis()), IOUtils.toString(os.getInputStream()));
LOGGER.info(requestLog.toString());
/* FileOutputStream fos = new FileOutputStream("/logs/messages.log", true);
fos.write(requestLog.toString().getBytes());
fos.close();*/
os.close();
}
}
catch (Exception ex) {
LOGGER.error("Exception while logging soap messages ", ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment