Created
May 5, 2014 22:04
-
-
Save rrajendran/0f2936493d41efe4f487 to your computer and use it in GitHub Desktop.
Custom LoggingInInterceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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