001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.util.pipe;
038:
039: import com.sun.xml.ws.api.message.Packet;
040: import com.sun.xml.ws.api.pipe.NextAction;
041: import com.sun.xml.ws.api.pipe.Pipe;
042: import com.sun.xml.ws.api.pipe.Tube;
043: import com.sun.xml.ws.api.pipe.TubeCloner;
044: import com.sun.xml.ws.api.pipe.helper.AbstractFilterTubeImpl;
045: import com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl;
046:
047: import javax.xml.stream.XMLOutputFactory;
048: import javax.xml.stream.XMLStreamException;
049: import javax.xml.stream.XMLStreamWriter;
050: import java.io.PrintStream;
051: import java.lang.reflect.Constructor;
052:
053: /**
054: * {@link Pipe} that dumps messages that pass through.
055: *
056: * @author Kohsuke Kawaguchi
057: */
058: public class DumpTube extends AbstractFilterTubeImpl {
059:
060: private final String name;
061:
062: private final PrintStream out;
063:
064: private final XMLOutputFactory staxOut;
065:
066: /**
067: * @param name
068: * Specify the name that identifies this {@link DumpTube}
069: * instance. This string will be printed when this pipe
070: * dumps messages, and allows people to distinguish which
071: * pipe instance is dumping a message when multiple
072: * {@link DumpTube}s print messages out.
073: * @param out
074: * The output to send dumps to.
075: * @param next
076: * The next {@link Tube} in the pipeline.
077: */
078: public DumpTube(String name, PrintStream out, Tube next) {
079: super (next);
080: this .name = name;
081: this .out = out;
082: this .staxOut = XMLOutputFactory.newInstance();
083: //staxOut.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES,true);
084: }
085:
086: /**
087: * Copy constructor.
088: */
089: protected DumpTube(DumpTube that, TubeCloner cloner) {
090: super (that, cloner);
091: this .name = that.name;
092: this .out = that.out;
093: this .staxOut = that.staxOut;
094: }
095:
096: public NextAction processRequest(Packet request) {
097: dump("request", request);
098: return super .processRequest(request);
099: }
100:
101: public NextAction processResponse(Packet response) {
102: dump("response", response);
103: return super .processResponse(response);
104: }
105:
106: protected void dump(String header, Packet packet) {
107: out.println("====[" + name + ":" + header + "]====");
108: if (packet.getMessage() == null)
109: out.println("(none)");
110: else
111: try {
112: XMLStreamWriter writer = staxOut
113: .createXMLStreamWriter(new PrintStream(out) {
114: public void close() {
115: // noop
116: }
117: });
118: writer = createIndenter(writer);
119: packet.getMessage().copy().writeTo(writer);
120: writer.close();
121: } catch (XMLStreamException e) {
122: e.printStackTrace(out);
123: }
124: out.println("============");
125: }
126:
127: /**
128: * Wraps {@link XMLStreamWriter} by an indentation engine if possible.
129: *
130: * <p>
131: * We can do this only when we have <tt>stax-utils.jar</tt> in the classpath.
132: */
133: private XMLStreamWriter createIndenter(XMLStreamWriter writer) {
134: try {
135: Class clazz = getClass().getClassLoader().loadClass(
136: "javanet.staxutils.IndentingXMLStreamWriter");
137: Constructor c = clazz.getConstructor(XMLStreamWriter.class);
138: writer = (XMLStreamWriter) c.newInstance(writer);
139: } catch (Exception e) {
140: // if stax-utils.jar is not in the classpath, this will fail
141: // so, we'll just have to do without indentation
142: if (!warnStaxUtils) {
143: warnStaxUtils = true;
144: out
145: .println("WARNING: put stax-utils.jar to the classpath to indent the dump output");
146: }
147: }
148: return writer;
149: }
150:
151: public AbstractTubeImpl copy(TubeCloner cloner) {
152: return new DumpTube(this , cloner);
153: }
154:
155: private static boolean warnStaxUtils;
156: }
|