001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.webservice.jaxws;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.util.Set;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.ws.handler.MessageContext;
027: import javax.xml.ws.handler.soap.SOAPHandler;
028: import javax.xml.ws.handler.soap.SOAPMessageContext;
029:
030: import org.apache.log4j.Logger;
031:
032: import de.schlund.pfixcore.webservice.ServiceCallContext;
033: import de.schlund.pfixcore.webservice.ServiceRequest;
034: import de.schlund.pfixcore.webservice.ServiceResponse;
035: import de.schlund.pfixcore.webservice.ServiceRuntime;
036: import de.schlund.pfixcore.webservice.config.GlobalServiceConfig;
037: import de.schlund.pfixcore.webservice.utils.RecordingRequestWrapper;
038: import de.schlund.pfixcore.webservice.utils.RecordingResponseWrapper;
039: import de.schlund.pfixcore.webservice.utils.XMLFormatter;
040:
041: /**
042: * Records SOAP requests and responses for monitoring (only appropriate to development mode).
043: *
044: * @author mleidig@schlund.de
045: */
046: public class RecordingHandler implements
047: SOAPHandler<SOAPMessageContext> {
048:
049: private static Logger LOG = Logger.getLogger(RecordingHandler.class
050: .getName());
051:
052: public RecordingHandler() {
053: }
054:
055: /**
056: * Record incoming requests and outgoing responses.
057: */
058: public boolean handleMessage(SOAPMessageContext ctx) {
059: ServiceRuntime runtime = ServiceCallContext.getCurrentContext()
060: .getServiceRuntime();
061: GlobalServiceConfig globConf = runtime.getConfiguration()
062: .getGlobalServiceConfig();
063: if (globConf.getMonitoringEnabled()
064: || globConf.getLoggingEnabled()) {
065: boolean isOutbound = (Boolean) ctx
066: .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
067: if (isOutbound)
068: recordResponse(ctx);
069: else
070: recordRequest(ctx);
071: }
072: return true;
073: }
074:
075: /**
076: * Record error requests/responses.
077: */
078: public boolean handleFault(SOAPMessageContext ctx) {
079: handleMessage(ctx);
080: return true;
081: }
082:
083: public void close(MessageContext arg0) {
084: }
085:
086: public Set<QName> getHeaders() {
087: return null;
088: }
089:
090: /**
091: * Record incoming request.
092: */
093: private void recordRequest(SOAPMessageContext ctx) {
094: ServiceRequest serviceReq = ServiceCallContext
095: .getCurrentContext().getServiceRequest();
096: if (serviceReq instanceof RecordingRequestWrapper) {
097: try {
098: ByteArrayOutputStream out = new ByteArrayOutputStream();
099: ctx.getMessage().writeTo(out);
100: String msg = out.toString("UTF-8");
101: ((RecordingRequestWrapper) serviceReq)
102: .setRecordedMessage(XMLFormatter.format(msg));
103: } catch (Exception x) {
104: LOG.error("Error while recording request message", x);
105: }
106: }
107: }
108:
109: /**
110: * Record outgoing response.
111: */
112: private void recordResponse(SOAPMessageContext ctx) {
113: ServiceResponse serviceRes = ServiceCallContext
114: .getCurrentContext().getServiceResponse();
115: if (serviceRes instanceof RecordingResponseWrapper) {
116: try {
117: ByteArrayOutputStream out = new ByteArrayOutputStream();
118: ctx.getMessage().writeTo(out);
119: String msg = out.toString("UTF-8");
120: ((RecordingResponseWrapper) serviceRes)
121: .setRecordedMessage(XMLFormatter.format(msg));
122: } catch (Exception x) {
123: LOG.error("Error while recording response message", x);
124: }
125: }
126: }
127:
128: }
|