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.systest.ws.util;
19:
20: import java.io.ByteArrayOutputStream;
21: import java.io.OutputStream;
22: import java.util.ArrayList;
23: import java.util.List;
24: import java.util.logging.Level;
25: import java.util.logging.Logger;
26:
27: import org.apache.cxf.interceptor.Fault;
28: import org.apache.cxf.interceptor.StaxOutInterceptor;
29: import org.apache.cxf.io.CachedOutputStream;
30: import org.apache.cxf.io.CachedOutputStreamCallback;
31: import org.apache.cxf.io.WriteOnCloseOutputStream;
32: import org.apache.cxf.message.Message;
33: import org.apache.cxf.phase.AbstractPhaseInterceptor;
34: import org.apache.cxf.phase.Phase;
35: import org.apache.cxf.ws.rm.RMUtils;
36: import org.apache.cxf.ws.rm.RetransmissionInterceptor;
37:
38: /**
39: *
40: */
41: public class OutMessageRecorder extends AbstractPhaseInterceptor {
42:
43: private static final Logger LOG = Logger
44: .getLogger(OutMessageRecorder.class.getName());
45: private List<byte[]> outbound;
46:
47: public OutMessageRecorder() {
48: super (Phase.PRE_STREAM);
49: outbound = new ArrayList<byte[]>();
50: addAfter(RetransmissionInterceptor.class.getName());
51: addBefore(StaxOutInterceptor.class.getName());
52: }
53:
54: public void handleMessage(Message message) throws Fault {
55: OutputStream os = message.getContent(OutputStream.class);
56: if (null == os) {
57: return;
58: }
59:
60: WriteOnCloseOutputStream stream = RMUtils.createCachedStream(
61: message, os);
62: stream.registerCallback(new RecorderCallback());
63: }
64:
65: public List<byte[]> getOutboundMessages() {
66: return outbound;
67: }
68:
69: class RecorderCallback implements CachedOutputStreamCallback {
70:
71: public void onFlush(CachedOutputStream cos) {
72:
73: }
74:
75: public void onClose(CachedOutputStream cos) {
76: // bytes were already copied after flush
77: OutputStream os = cos.getOut();
78: if (os instanceof ByteArrayOutputStream) {
79: ByteArrayOutputStream bos = (ByteArrayOutputStream) os;
80: outbound.add(bos.toByteArray());
81: if (LOG.isLoggable(Level.FINE)) {
82: LOG.fine("outbound: " + bos.toString());
83: }
84: } else {
85: LOG
86: .fine("Can't record message from output stream class: "
87: + os.getClass().getName());
88: }
89: }
90:
91: }
92:
93: }
|