/*
* Copyright 2011 Federal Chancellery Austria and
* Graz University of Technology
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at.gv.util;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.apache.cxf.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Handler to log JAX-WS messages to slf4j logging framework. Does currently not
* distinguish between incomig and outgoing messages.
*
* @author Arne Tauber
*
*/
public class LoggingHandler implements SOAPHandler {
Logger log = LoggerFactory.getLogger(LoggingHandler.class);
@Override
public boolean handleMessage(SOAPMessageContext context) {
final SOAPMessage msg = context.getMessage();
final boolean request = ((Boolean) context
.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
if (request) {
msg.writeTo(bos);
} else { // This is the response message
msg.writeTo(bos);
}
final OutputStreamWriter writer = new OutputStreamWriter(bos);
writer.getEncoding();
//log.trace(bos.toString());
log.trace(new String(bos.toByteArray()));
log.trace("B64: " + Base64.getEncoder().encodeToString(bos.toByteArray()));
final Map headers = (Map) context.get(Message.PROTOCOL_HEADERS);
if (headers != null) {
for (final Entry el : headers.entrySet()) {
if (el.getValue() != null)
log.trace("Header: " + el.getKey() + " | Value: " + Arrays.toString(el.getValue().toArray()));
else
log.trace("Header: " + el.getKey() + " | Value: " + "'null'");
}
} else
log.trace("No HTTP headers");
} catch (final Exception e) {
log.trace(e.getMessage(), e);
}
return true;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return handleMessage(context);
}
@Override
public void close(MessageContext context) {
}
@Override
public Set getHeaders() {
return null;
}
}