1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/util/pipe/DumpTube.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,146 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.util.pipe; 1.30 + 1.31 +import com.sun.xml.internal.ws.api.message.Packet; 1.32 +import com.sun.xml.internal.ws.api.pipe.NextAction; 1.33 +import com.sun.xml.internal.ws.api.pipe.Pipe; 1.34 +import com.sun.xml.internal.ws.api.pipe.Tube; 1.35 +import com.sun.xml.internal.ws.api.pipe.TubeCloner; 1.36 +import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl; 1.37 +import com.sun.xml.internal.ws.api.pipe.helper.AbstractTubeImpl; 1.38 + 1.39 +import javax.xml.stream.XMLOutputFactory; 1.40 +import javax.xml.stream.XMLStreamException; 1.41 +import javax.xml.stream.XMLStreamWriter; 1.42 +import java.io.PrintStream; 1.43 +import java.lang.reflect.Constructor; 1.44 + 1.45 +/** 1.46 + * {@link Pipe} that dumps messages that pass through. 1.47 + * 1.48 + * @author Kohsuke Kawaguchi 1.49 + */ 1.50 +public class DumpTube extends AbstractFilterTubeImpl { 1.51 + 1.52 + private final String name; 1.53 + 1.54 + private final PrintStream out; 1.55 + 1.56 + private final XMLOutputFactory staxOut; 1.57 + 1.58 + /** 1.59 + * @param name 1.60 + * Specify the name that identifies this {@link DumpTube} 1.61 + * instance. This string will be printed when this pipe 1.62 + * dumps messages, and allows people to distinguish which 1.63 + * pipe instance is dumping a message when multiple 1.64 + * {@link DumpTube}s print messages out. 1.65 + * @param out 1.66 + * The output to send dumps to. 1.67 + * @param next 1.68 + * The next {@link Tube} in the pipeline. 1.69 + */ 1.70 + public DumpTube(String name, PrintStream out, Tube next) { 1.71 + super(next); 1.72 + this.name = name; 1.73 + this.out = out; 1.74 + this.staxOut = XMLOutputFactory.newInstance(); 1.75 + //staxOut.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,true); 1.76 + } 1.77 + 1.78 + /** 1.79 + * Copy constructor. 1.80 + */ 1.81 + protected DumpTube(DumpTube that, TubeCloner cloner) { 1.82 + super(that,cloner); 1.83 + this.name = that.name; 1.84 + this.out = that.out; 1.85 + this.staxOut = that.staxOut; 1.86 + } 1.87 + 1.88 + @Override 1.89 + public NextAction processRequest(Packet request) { 1.90 + dump("request",request); 1.91 + return super.processRequest(request); 1.92 + } 1.93 + 1.94 + @Override 1.95 + public NextAction processResponse(Packet response) { 1.96 + dump("response",response); 1.97 + return super.processResponse(response); 1.98 + } 1.99 + 1.100 + protected void dump(String header, Packet packet) { 1.101 + out.println("====["+name+":"+header+"]===="); 1.102 + if(packet.getMessage()==null) 1.103 + out.println("(none)"); 1.104 + else 1.105 + try { 1.106 + XMLStreamWriter writer = staxOut.createXMLStreamWriter(new PrintStream(out) { 1.107 + @Override 1.108 + public void close() { 1.109 + // noop 1.110 + } 1.111 + }); 1.112 + writer = createIndenter(writer); 1.113 + packet.getMessage().copy().writeTo(writer); 1.114 + writer.close(); 1.115 + } catch (XMLStreamException e) { 1.116 + e.printStackTrace(out); 1.117 + } 1.118 + out.println("============"); 1.119 + } 1.120 + 1.121 + /** 1.122 + * Wraps {@link XMLStreamWriter} by an indentation engine if possible. 1.123 + * 1.124 + * <p> 1.125 + * We can do this only when we have <tt>stax-utils.jar</tt> in the classpath. 1.126 + */ 1.127 + private XMLStreamWriter createIndenter(XMLStreamWriter writer) { 1.128 + try { 1.129 + Class clazz = getClass().getClassLoader().loadClass("javanet.staxutils.IndentingXMLStreamWriter"); 1.130 + Constructor c = clazz.getConstructor(XMLStreamWriter.class); 1.131 + writer = (XMLStreamWriter)c.newInstance(writer); 1.132 + } catch (Exception e) { 1.133 + // if stax-utils.jar is not in the classpath, this will fail 1.134 + // so, we'll just have to do without indentation 1.135 + if(!warnStaxUtils) { 1.136 + warnStaxUtils = true; 1.137 + out.println("WARNING: put stax-utils.jar to the classpath to indent the dump output"); 1.138 + } 1.139 + } 1.140 + return writer; 1.141 + } 1.142 + 1.143 + 1.144 + public AbstractTubeImpl copy(TubeCloner cloner) { 1.145 + return new DumpTube(this,cloner); 1.146 + } 1.147 + 1.148 + private static boolean warnStaxUtils; 1.149 +}