001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.ws.server.endpoint;
018:
019: import java.io.StringWriter;
020: import javax.xml.transform.OutputKeys;
021: import javax.xml.transform.Source;
022: import javax.xml.transform.Transformer;
023: import javax.xml.transform.TransformerConfigurationException;
024: import javax.xml.transform.TransformerException;
025: import javax.xml.transform.stream.StreamResult;
026:
027: import org.springframework.ws.WebServiceMessage;
028: import org.springframework.ws.context.MessageContext;
029: import org.springframework.ws.server.EndpointInterceptor;
030: import org.springframework.xml.transform.TransformerObjectSupport;
031:
032: /**
033: * Abstract base class for <code>EndpointInterceptor</code> instances that log a part of a
034: * <code>WebServiceMessage</code>. By default, both request and response messages are logged, but this behaviour can be
035: * changed using the <code>logRequest</code> and <code>logResponse</code> properties.
036: *
037: * @author Arjen Poutsma
038: * @since 1.0.0
039: */
040: public abstract class AbstractLoggingInterceptor extends
041: TransformerObjectSupport implements EndpointInterceptor {
042:
043: private boolean logRequest = true;
044:
045: private boolean logResponse = true;
046:
047: /** Indicates whether the request should be logged. Default is <code>true</code>. */
048: public final void setLogRequest(boolean logRequest) {
049: this .logRequest = logRequest;
050: }
051:
052: /** Indicates whether the response should be logged. Default is <code>true</code>. */
053: public final void setLogResponse(boolean logResponse) {
054: this .logResponse = logResponse;
055: }
056:
057: /**
058: * Logs the request message payload. Logging only ocurs if <code>logRequest</code> is set to <code>true</code>,
059: * which is the default.
060: *
061: * @param messageContext the message context
062: * @return <code>true</code>
063: * @throws TransformerException when the payload cannot be transformed to a string
064: */
065: public final boolean handleRequest(MessageContext messageContext,
066: Object endpoint) throws TransformerException {
067: if (logRequest && logger.isDebugEnabled()) {
068: logMessageSource("Request: ", getSource(messageContext
069: .getRequest()));
070: }
071: return true;
072: }
073:
074: /**
075: * Logs the response message payload. Logging only ocurs if <code>logResponse</code> is set to <code>true</code>,
076: * which is the default.
077: *
078: * @param messageContext the message context
079: * @return <code>true</code>
080: * @throws TransformerException when the payload cannot be transformed to a string
081: */
082: public boolean handleResponse(MessageContext messageContext,
083: Object endpoint) throws Exception {
084: if (logResponse && logger.isDebugEnabled()) {
085: logMessageSource("Response: ", getSource(messageContext
086: .getResponse()));
087: }
088: return true;
089: }
090:
091: /** Does nothing by default. Faults are not logged. */
092: public boolean handleFault(MessageContext messageContext,
093: Object endpoint) throws Exception {
094: return true;
095: }
096:
097: private Transformer createNonIndentingTransformer()
098: throws TransformerConfigurationException {
099: Transformer transformer = createTransformer();
100: transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
101: "yes");
102: transformer.setOutputProperty(OutputKeys.INDENT, "no");
103: return transformer;
104: }
105:
106: protected void logMessageSource(String logMessage, Source source)
107: throws TransformerException {
108: if (source != null) {
109: Transformer transformer = createNonIndentingTransformer();
110: StringWriter writer = new StringWriter();
111: transformer.transform(source, new StreamResult(writer));
112: logger.debug(logMessage + writer.toString());
113: }
114: }
115:
116: /**
117: * Abstract template method that returns the <code>Source</code> for the given <code>WebServiceMessage</code>.
118: *
119: * @param message the message
120: * @return the source of the message
121: */
122: protected abstract Source getSource(WebServiceMessage message);
123: }
|