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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     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	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,217 @@
     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 ContextClassloaderLocal<XMLOutputFactory> xmlOutputFactory = new ContextClassloaderLocal<XMLOutputFactory>() {
    1.60 +        @Override
    1.61 +        protected XMLOutputFactory initialValue() throws Exception {
    1.62 +            return XMLOutputFactory.newInstance();
    1.63 +        }
    1.64 +    };
    1.65 +    private static final AtomicBoolean logMissingStaxUtilsWarning = new AtomicBoolean(false);
    1.66 +
    1.67 +    /**
    1.68 +     * Converts a throwable to String
    1.69 +     *
    1.70 +     * @param throwable
    1.71 +     * @return String representation of throwable
    1.72 +     */
    1.73 +    public static String toString(Throwable throwable) {
    1.74 +        if (throwable == null) {
    1.75 +            return "[ No exception ]";
    1.76 +        }
    1.77 +
    1.78 +        StringWriter stringOut = new StringWriter();
    1.79 +        throwable.printStackTrace(new PrintWriter(stringOut));
    1.80 +
    1.81 +        return stringOut.toString();
    1.82 +    }
    1.83 +
    1.84 +    public static String toString(Packet packet) {
    1.85 +        if (packet == null) {
    1.86 +            return "[ Null packet ]";
    1.87 +        } else if (packet.getMessage() == null) {
    1.88 +                return "[ Empty packet ]";
    1.89 +        }
    1.90 +
    1.91 +        return toString(packet.getMessage());
    1.92 +    }
    1.93 +
    1.94 +    public static String toStringNoIndent(Packet packet) {
    1.95 +        if (packet == null) {
    1.96 +            return "[ Null packet ]";
    1.97 +        } else if (packet.getMessage() == null) {
    1.98 +                return "[ Empty packet ]";
    1.99 +        }
   1.100 +
   1.101 +        return toStringNoIndent(packet.getMessage());
   1.102 +    }
   1.103 +
   1.104 +    public static String toString(Message message) {
   1.105 +        return toString(message, true);
   1.106 +    }
   1.107 +
   1.108 +    public static String toStringNoIndent(Message message) {
   1.109 +        return toString(message, false);
   1.110 +    }
   1.111 +
   1.112 +    private static String toString(Message message, boolean createIndenter) {
   1.113 +        if (message == null) {
   1.114 +            return "[ Null message ]";
   1.115 +        }
   1.116 +        StringWriter stringOut = null;
   1.117 +        try {
   1.118 +            stringOut = new StringWriter();
   1.119 +            XMLStreamWriter writer = null;
   1.120 +            try {
   1.121 +                writer = xmlOutputFactory.get().createXMLStreamWriter(stringOut);
   1.122 +                if (createIndenter) {
   1.123 +                    writer = createIndenter(writer);
   1.124 +                }
   1.125 +                message.copy().writeTo(writer);
   1.126 +            } catch (Exception e) { // WSIT-1596 - Message Dumping should not affect other processing
   1.127 +                LOGGER.log(Level.WARNING, "Unexpected exception occured while dumping message", e);
   1.128 +            } finally {
   1.129 +                if (writer != null) {
   1.130 +                    try {
   1.131 +                        writer.close();
   1.132 +                    } catch (XMLStreamException ignored) {
   1.133 +                        LOGGER.fine("Unexpected exception occured while closing XMLStreamWriter", ignored);
   1.134 +                    }
   1.135 +                }
   1.136 +            }
   1.137 +            return stringOut.toString();
   1.138 +        } finally {
   1.139 +            if (stringOut != null) {
   1.140 +                try {
   1.141 +                    stringOut.close();
   1.142 +                } catch (IOException ex) {
   1.143 +                    LOGGER.finest("An exception occured when trying to close StringWriter", ex);
   1.144 +                }
   1.145 +            }
   1.146 +        }
   1.147 +    }
   1.148 +
   1.149 +    public static byte[] toBytes(Message message, String encoding) throws XMLStreamException {
   1.150 +        ByteArrayOutputStream baos = new ByteArrayOutputStream();
   1.151 +
   1.152 +        try {
   1.153 +            if (message != null) {
   1.154 +                XMLStreamWriter xsw = xmlOutputFactory.get().createXMLStreamWriter(baos, encoding);
   1.155 +                try {
   1.156 +                    message.writeTo(xsw);
   1.157 +                } finally {
   1.158 +                    try {
   1.159 +                        xsw.close();
   1.160 +                    } catch (XMLStreamException ex) {
   1.161 +                        LOGGER.warning("Unexpected exception occured while closing XMLStreamWriter", ex);
   1.162 +                    }
   1.163 +                }
   1.164 +            }
   1.165 +
   1.166 +            return baos.toByteArray();
   1.167 +        } finally {
   1.168 +            try {
   1.169 +                baos.close();
   1.170 +            } catch (IOException ex) {
   1.171 +                LOGGER.warning("Unexpected exception occured while closing ByteArrayOutputStream", ex);
   1.172 +            }
   1.173 +        }
   1.174 +    }
   1.175 +
   1.176 +    /**
   1.177 +     * Converts JAX-WS RI message represented as input stream back to Message
   1.178 +     * object.
   1.179 +     *
   1.180 +     * @param dataStream message data stream
   1.181 +     * @param encoding message data stream encoding
   1.182 +     *
   1.183 +     * @return {@link com.sun.xml.internal.ws.api.message.Message} object created from the data stream
   1.184 +     */
   1.185 +    public static Message toMessage(@NotNull InputStream dataStream, String encoding) throws XMLStreamException {
   1.186 +        XMLStreamReader xsr = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(dataStream, encoding);
   1.187 +        return Messages.create(xsr);
   1.188 +    }
   1.189 +
   1.190 +    public static String messageDataToString(final byte[] data, final String encoding) {
   1.191 +        try {
   1.192 +            return toString(toMessage(new ByteArrayInputStream(data), encoding));
   1.193 +            // closing ByteArrayInputStream has no effect, so ignoring the redundant call
   1.194 +        } catch (XMLStreamException ex) {
   1.195 +            LOGGER.warning("Unexpected exception occured while converting message data to string", ex);
   1.196 +            return "[ Message Data Conversion Failed ]";
   1.197 +        }
   1.198 +    }
   1.199 +
   1.200 +    /**
   1.201 +     * Wraps {@link javax.xml.stream.XMLStreamWriter} by an indentation engine if possible.
   1.202 +     *
   1.203 +     * <p>
   1.204 +     * We can do this only when we have <tt>stax-utils.jar</tt> in the class path.
   1.205 +     */
   1.206 +    private static XMLStreamWriter createIndenter(XMLStreamWriter writer) {
   1.207 +        try {
   1.208 +            Class<?> clazz = Converter.class.getClassLoader().loadClass("javanet.staxutils.IndentingXMLStreamWriter");
   1.209 +            Constructor<?> c = clazz.getConstructor(XMLStreamWriter.class);
   1.210 +            writer = XMLStreamWriter.class.cast(c.newInstance(writer));
   1.211 +        } catch (Exception ex) {
   1.212 +            // if stax-utils.jar is not in the classpath, this will fail
   1.213 +            // so, we'll just have to do without indentation
   1.214 +            if (logMissingStaxUtilsWarning.compareAndSet(false, true)) {
   1.215 +                LOGGER.log(Level.WARNING, "Put stax-utils.jar to the classpath to indent the dump output", ex);
   1.216 +            }
   1.217 +        }
   1.218 +        return writer;
   1.219 +    }
   1.220 +}

mercurial