src/share/jaxws_classes/com/sun/xml/internal/ws/commons/xmlutil/Converter.java

changeset 368
0989ad8c0860
child 397
b99d7e355d4b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/commons/xmlutil/Converter.java	Tue Apr 09 14:51:13 2013 +0100
     1.3 @@ -0,0 +1,193 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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.commons.xmlutil;
    1.30 +
    1.31 +import com.sun.istack.internal.NotNull;
    1.32 +import com.sun.istack.internal.logging.Logger;
    1.33 +import com.sun.xml.internal.ws.api.message.Message;
    1.34 +import com.sun.xml.internal.ws.api.message.Messages;
    1.35 +import com.sun.xml.internal.ws.api.message.Packet;
    1.36 +import com.sun.xml.internal.ws.util.xml.XmlUtil;
    1.37 +
    1.38 +import javax.xml.stream.*;
    1.39 +import javax.xml.xpath.XPathFactoryConfigurationException;
    1.40 +import java.io.*;
    1.41 +import java.lang.reflect.Constructor;
    1.42 +import java.util.concurrent.atomic.AtomicBoolean;
    1.43 +import java.util.logging.Level;
    1.44 +
    1.45 +/**
    1.46 + * Utility class that provides conversion of different XML representations
    1.47 + * from/to various other formats
    1.48 + *
    1.49 + * @author Marek Potociar
    1.50 + */
    1.51 +public final class Converter {
    1.52 +
    1.53 +    public static final String UTF_8 = "UTF-8";
    1.54 +
    1.55 +    private Converter() {
    1.56 +        // prevents instantiation
    1.57 +    }
    1.58 +    private static final Logger LOGGER = Logger.getLogger(Converter.class);
    1.59 +    private static final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    1.60 +    private static final AtomicBoolean logMissingStaxUtilsWarning = new AtomicBoolean(false);
    1.61 +
    1.62 +    /**
    1.63 +     * Converts a throwable to String
    1.64 +     *
    1.65 +     * @param throwable
    1.66 +     * @return String representation of throwable
    1.67 +     */
    1.68 +    public static String toString(Throwable throwable) {
    1.69 +        if (throwable == null) {
    1.70 +            return "[ No exception ]";
    1.71 +        }
    1.72 +
    1.73 +        StringWriter stringOut = new StringWriter();
    1.74 +        throwable.printStackTrace(new PrintWriter(stringOut));
    1.75 +
    1.76 +        return stringOut.toString();
    1.77 +    }
    1.78 +
    1.79 +    public static String toString(Packet packet) {
    1.80 +        if (packet == null) {
    1.81 +            return "[ Null packet ]";
    1.82 +        } else if (packet.getMessage() == null) {
    1.83 +                return "[ Empty packet ]";
    1.84 +        }
    1.85 +
    1.86 +        return toString(packet.getMessage());
    1.87 +
    1.88 +    }
    1.89 +
    1.90 +    public static String toString(Message message) {
    1.91 +        if (message == null) {
    1.92 +            return "[ Null message ]";
    1.93 +        }
    1.94 +        StringWriter stringOut = null;
    1.95 +        try {
    1.96 +            stringOut = new StringWriter();
    1.97 +            XMLStreamWriter writer = null;
    1.98 +            try {
    1.99 +                writer = xmlOutputFactory.createXMLStreamWriter(stringOut);
   1.100 +                writer = createIndenter(writer);
   1.101 +                message.copy().writeTo(writer);
   1.102 +            } catch (Exception e) { // WSIT-1596 - Message Dumping should not affect other processing
   1.103 +                LOGGER.log(Level.WARNING, "Unexpected exception occured while dumping message", e);
   1.104 +            } finally {
   1.105 +                if (writer != null) {
   1.106 +                    try {
   1.107 +                        writer.close();
   1.108 +                    } catch (XMLStreamException ignored) {
   1.109 +                        LOGGER.fine("Unexpected exception occured while closing XMLStreamWriter", ignored);
   1.110 +                    }
   1.111 +                }
   1.112 +            }
   1.113 +            return stringOut.toString();
   1.114 +        } finally {
   1.115 +            if (stringOut != null) {
   1.116 +                try {
   1.117 +                    stringOut.close();
   1.118 +                } catch (IOException ex) {
   1.119 +                    LOGGER.finest("An exception occured when trying to close StringWriter", ex);
   1.120 +                }
   1.121 +            }
   1.122 +        }
   1.123 +    }
   1.124 +
   1.125 +    public static byte[] toBytes(Message message, String encoding) throws XMLStreamException {
   1.126 +        ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1.127 +
   1.128 +        try {
   1.129 +            if (message != null) {
   1.130 +                XMLStreamWriter xsw = xmlOutputFactory.createXMLStreamWriter(baos, encoding);
   1.131 +                try {
   1.132 +                    message.writeTo(xsw);
   1.133 +                } finally {
   1.134 +                    try {
   1.135 +                        xsw.close();
   1.136 +                    } catch (XMLStreamException ex) {
   1.137 +                        LOGGER.warning("Unexpected exception occured while closing XMLStreamWriter", ex);
   1.138 +                    }
   1.139 +                }
   1.140 +            }
   1.141 +
   1.142 +            return baos.toByteArray();
   1.143 +        } finally {
   1.144 +            try {
   1.145 +                baos.close();
   1.146 +            } catch (IOException ex) {
   1.147 +                LOGGER.warning("Unexpected exception occured while closing ByteArrayOutputStream", ex);
   1.148 +            }
   1.149 +        }
   1.150 +    }
   1.151 +
   1.152 +    /**
   1.153 +     * Converts JAX-WS RI message represented as input stream back to Message
   1.154 +     * object.
   1.155 +     *
   1.156 +     * @param dataStream message data stream
   1.157 +     * @param encoding message data stream encoding
   1.158 +     *
   1.159 +     * @return {@link com.sun.xml.internal.ws.api.message.Message} object created from the data stream
   1.160 +     */
   1.161 +    public static Message toMessage(@NotNull InputStream dataStream, String encoding) throws XMLStreamException {
   1.162 +        XMLStreamReader xsr = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(dataStream, encoding);
   1.163 +        return Messages.create(xsr);
   1.164 +    }
   1.165 +
   1.166 +    public static String messageDataToString(final byte[] data, final String encoding) {
   1.167 +        try {
   1.168 +            return toString(toMessage(new ByteArrayInputStream(data), encoding));
   1.169 +            // closing ByteArrayInputStream has no effect, so ignoring the redundant call
   1.170 +        } catch (XMLStreamException ex) {
   1.171 +            LOGGER.warning("Unexpected exception occured while converting message data to string", ex);
   1.172 +            return "[ Message Data Conversion Failed ]";
   1.173 +        }
   1.174 +    }
   1.175 +
   1.176 +    /**
   1.177 +     * Wraps {@link javax.xml.stream.XMLStreamWriter} by an indentation engine if possible.
   1.178 +     *
   1.179 +     * <p>
   1.180 +     * We can do this only when we have <tt>stax-utils.jar</tt> in the class path.
   1.181 +     */
   1.182 +    private static XMLStreamWriter createIndenter(XMLStreamWriter writer) {
   1.183 +        try {
   1.184 +            Class<?> clazz = Converter.class.getClassLoader().loadClass("javanet.staxutils.IndentingXMLStreamWriter");
   1.185 +            Constructor<?> c = clazz.getConstructor(XMLStreamWriter.class);
   1.186 +            writer = XMLStreamWriter.class.cast(c.newInstance(writer));
   1.187 +        } catch (Exception ex) {
   1.188 +            // if stax-utils.jar is not in the classpath, this will fail
   1.189 +            // so, we'll just have to do without indentation
   1.190 +            if (logMissingStaxUtilsWarning.compareAndSet(false, true)) {
   1.191 +                LOGGER.log(Level.WARNING, "Put stax-utils.jar to the classpath to indent the dump output", ex);
   1.192 +            }
   1.193 +        }
   1.194 +        return writer;
   1.195 +    }
   1.196 +}

mercurial