01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.interceptor;
19:
20: import java.io.ByteArrayOutputStream;
21: import java.io.OutputStream;
22: import java.util.logging.Level;
23: import java.util.logging.Logger;
24:
25: import org.apache.cxf.common.logging.LogUtils;
26: import org.apache.cxf.io.CacheAndWriteOutputStream;
27: import org.apache.cxf.io.CachedOutputStream;
28: import org.apache.cxf.io.CachedOutputStreamCallback;
29: import org.apache.cxf.message.Message;
30: import org.apache.cxf.phase.AbstractPhaseInterceptor;
31: import org.apache.cxf.phase.Phase;
32:
33: /**
34: *
35: */
36: public class LoggingOutInterceptor extends AbstractPhaseInterceptor {
37:
38: private static final Logger LOG = LogUtils
39: .getL7dLogger(LoggingOutInterceptor.class);
40:
41: public LoggingOutInterceptor() {
42: super (Phase.PRE_STREAM);
43: addBefore(StaxOutInterceptor.class.getName());
44: }
45:
46: public void handleMessage(Message message) throws Fault {
47: final OutputStream os = message.getContent(OutputStream.class);
48: if (os == null) {
49: return;
50: }
51:
52: // Write the output while caching it for the log message
53: final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(
54: os);
55: message.setContent(OutputStream.class, newOut);
56: newOut.registerCallback(new LoggingCallback());
57: }
58:
59: class LoggingCallback implements CachedOutputStreamCallback {
60:
61: public void onFlush(CachedOutputStream cos) {
62:
63: }
64:
65: public void onClose(CachedOutputStream cos) {
66: OutputStream os = cos.getOut();
67: if (os instanceof ByteArrayOutputStream
68: && LOG.isLoggable(Level.INFO)) {
69: // TODO - make this work with large messages
70: LOG.info("Outbound Message \n"
71: + "--------------------------------------\n"
72: + os.toString()
73: + "\n--------------------------------------");
74: }
75: }
76:
77: }
78: }
|