Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.commons.xmlutil; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 29 | import com.sun.istack.internal.logging.Logger; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.message.Message; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.message.Messages; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.message.Packet; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.util.xml.XmlUtil; |
aoqi@0 | 34 | |
aoqi@0 | 35 | import javax.xml.stream.*; |
aoqi@0 | 36 | import javax.xml.xpath.XPathFactoryConfigurationException; |
aoqi@0 | 37 | import java.io.*; |
aoqi@0 | 38 | import java.lang.reflect.Constructor; |
aoqi@0 | 39 | import java.util.concurrent.atomic.AtomicBoolean; |
aoqi@0 | 40 | import java.util.logging.Level; |
aoqi@0 | 41 | |
aoqi@0 | 42 | /** |
aoqi@0 | 43 | * Utility class that provides conversion of different XML representations |
aoqi@0 | 44 | * from/to various other formats |
aoqi@0 | 45 | * |
aoqi@0 | 46 | * @author Marek Potociar |
aoqi@0 | 47 | */ |
aoqi@0 | 48 | public final class Converter { |
aoqi@0 | 49 | |
aoqi@0 | 50 | public static final String UTF_8 = "UTF-8"; |
aoqi@0 | 51 | |
aoqi@0 | 52 | private Converter() { |
aoqi@0 | 53 | // prevents instantiation |
aoqi@0 | 54 | } |
aoqi@0 | 55 | private static final Logger LOGGER = Logger.getLogger(Converter.class); |
aoqi@0 | 56 | private static final ContextClassloaderLocal<XMLOutputFactory> xmlOutputFactory = new ContextClassloaderLocal<XMLOutputFactory>() { |
aoqi@0 | 57 | @Override |
aoqi@0 | 58 | protected XMLOutputFactory initialValue() throws Exception { |
aoqi@0 | 59 | return XMLOutputFactory.newInstance(); |
aoqi@0 | 60 | } |
aoqi@0 | 61 | }; |
aoqi@0 | 62 | private static final AtomicBoolean logMissingStaxUtilsWarning = new AtomicBoolean(false); |
aoqi@0 | 63 | |
aoqi@0 | 64 | /** |
aoqi@0 | 65 | * Converts a throwable to String |
aoqi@0 | 66 | * |
aoqi@0 | 67 | * @param throwable |
aoqi@0 | 68 | * @return String representation of throwable |
aoqi@0 | 69 | */ |
aoqi@0 | 70 | public static String toString(Throwable throwable) { |
aoqi@0 | 71 | if (throwable == null) { |
aoqi@0 | 72 | return "[ No exception ]"; |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | StringWriter stringOut = new StringWriter(); |
aoqi@0 | 76 | throwable.printStackTrace(new PrintWriter(stringOut)); |
aoqi@0 | 77 | |
aoqi@0 | 78 | return stringOut.toString(); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | public static String toString(Packet packet) { |
aoqi@0 | 82 | if (packet == null) { |
aoqi@0 | 83 | return "[ Null packet ]"; |
aoqi@0 | 84 | } else if (packet.getMessage() == null) { |
aoqi@0 | 85 | return "[ Empty packet ]"; |
aoqi@0 | 86 | } |
aoqi@0 | 87 | |
aoqi@0 | 88 | return toString(packet.getMessage()); |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | public static String toStringNoIndent(Packet packet) { |
aoqi@0 | 92 | if (packet == null) { |
aoqi@0 | 93 | return "[ Null packet ]"; |
aoqi@0 | 94 | } else if (packet.getMessage() == null) { |
aoqi@0 | 95 | return "[ Empty packet ]"; |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | return toStringNoIndent(packet.getMessage()); |
aoqi@0 | 99 | } |
aoqi@0 | 100 | |
aoqi@0 | 101 | public static String toString(Message message) { |
aoqi@0 | 102 | return toString(message, true); |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | public static String toStringNoIndent(Message message) { |
aoqi@0 | 106 | return toString(message, false); |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | private static String toString(Message message, boolean createIndenter) { |
aoqi@0 | 110 | if (message == null) { |
aoqi@0 | 111 | return "[ Null message ]"; |
aoqi@0 | 112 | } |
aoqi@0 | 113 | StringWriter stringOut = null; |
aoqi@0 | 114 | try { |
aoqi@0 | 115 | stringOut = new StringWriter(); |
aoqi@0 | 116 | XMLStreamWriter writer = null; |
aoqi@0 | 117 | try { |
aoqi@0 | 118 | writer = xmlOutputFactory.get().createXMLStreamWriter(stringOut); |
aoqi@0 | 119 | if (createIndenter) { |
aoqi@0 | 120 | writer = createIndenter(writer); |
aoqi@0 | 121 | } |
aoqi@0 | 122 | message.copy().writeTo(writer); |
aoqi@0 | 123 | } catch (Exception e) { // WSIT-1596 - Message Dumping should not affect other processing |
aoqi@0 | 124 | LOGGER.log(Level.WARNING, "Unexpected exception occured while dumping message", e); |
aoqi@0 | 125 | } finally { |
aoqi@0 | 126 | if (writer != null) { |
aoqi@0 | 127 | try { |
aoqi@0 | 128 | writer.close(); |
aoqi@0 | 129 | } catch (XMLStreamException ignored) { |
aoqi@0 | 130 | LOGGER.fine("Unexpected exception occured while closing XMLStreamWriter", ignored); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | } |
aoqi@0 | 133 | } |
aoqi@0 | 134 | return stringOut.toString(); |
aoqi@0 | 135 | } finally { |
aoqi@0 | 136 | if (stringOut != null) { |
aoqi@0 | 137 | try { |
aoqi@0 | 138 | stringOut.close(); |
aoqi@0 | 139 | } catch (IOException ex) { |
aoqi@0 | 140 | LOGGER.finest("An exception occured when trying to close StringWriter", ex); |
aoqi@0 | 141 | } |
aoqi@0 | 142 | } |
aoqi@0 | 143 | } |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | public static byte[] toBytes(Message message, String encoding) throws XMLStreamException { |
aoqi@0 | 147 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
aoqi@0 | 148 | |
aoqi@0 | 149 | try { |
aoqi@0 | 150 | if (message != null) { |
aoqi@0 | 151 | XMLStreamWriter xsw = xmlOutputFactory.get().createXMLStreamWriter(baos, encoding); |
aoqi@0 | 152 | try { |
aoqi@0 | 153 | message.writeTo(xsw); |
aoqi@0 | 154 | } finally { |
aoqi@0 | 155 | try { |
aoqi@0 | 156 | xsw.close(); |
aoqi@0 | 157 | } catch (XMLStreamException ex) { |
aoqi@0 | 158 | LOGGER.warning("Unexpected exception occured while closing XMLStreamWriter", ex); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | } |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | return baos.toByteArray(); |
aoqi@0 | 164 | } finally { |
aoqi@0 | 165 | try { |
aoqi@0 | 166 | baos.close(); |
aoqi@0 | 167 | } catch (IOException ex) { |
aoqi@0 | 168 | LOGGER.warning("Unexpected exception occured while closing ByteArrayOutputStream", ex); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | } |
aoqi@0 | 171 | } |
aoqi@0 | 172 | |
aoqi@0 | 173 | /** |
aoqi@0 | 174 | * Converts JAX-WS RI message represented as input stream back to Message |
aoqi@0 | 175 | * object. |
aoqi@0 | 176 | * |
aoqi@0 | 177 | * @param dataStream message data stream |
aoqi@0 | 178 | * @param encoding message data stream encoding |
aoqi@0 | 179 | * |
aoqi@0 | 180 | * @return {@link com.sun.xml.internal.ws.api.message.Message} object created from the data stream |
aoqi@0 | 181 | */ |
aoqi@0 | 182 | public static Message toMessage(@NotNull InputStream dataStream, String encoding) throws XMLStreamException { |
aoqi@0 | 183 | XMLStreamReader xsr = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(dataStream, encoding); |
aoqi@0 | 184 | return Messages.create(xsr); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | public static String messageDataToString(final byte[] data, final String encoding) { |
aoqi@0 | 188 | try { |
aoqi@0 | 189 | return toString(toMessage(new ByteArrayInputStream(data), encoding)); |
aoqi@0 | 190 | // closing ByteArrayInputStream has no effect, so ignoring the redundant call |
aoqi@0 | 191 | } catch (XMLStreamException ex) { |
aoqi@0 | 192 | LOGGER.warning("Unexpected exception occured while converting message data to string", ex); |
aoqi@0 | 193 | return "[ Message Data Conversion Failed ]"; |
aoqi@0 | 194 | } |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | /** |
aoqi@0 | 198 | * Wraps {@link javax.xml.stream.XMLStreamWriter} by an indentation engine if possible. |
aoqi@0 | 199 | * |
aoqi@0 | 200 | * <p> |
aoqi@0 | 201 | * We can do this only when we have <tt>stax-utils.jar</tt> in the class path. |
aoqi@0 | 202 | */ |
aoqi@0 | 203 | private static XMLStreamWriter createIndenter(XMLStreamWriter writer) { |
aoqi@0 | 204 | try { |
aoqi@0 | 205 | Class<?> clazz = Converter.class.getClassLoader().loadClass("javanet.staxutils.IndentingXMLStreamWriter"); |
aoqi@0 | 206 | Constructor<?> c = clazz.getConstructor(XMLStreamWriter.class); |
aoqi@0 | 207 | writer = XMLStreamWriter.class.cast(c.newInstance(writer)); |
aoqi@0 | 208 | } catch (Exception ex) { |
aoqi@0 | 209 | // if stax-utils.jar is not in the classpath, this will fail |
aoqi@0 | 210 | // so, we'll just have to do without indentation |
aoqi@0 | 211 | if (logMissingStaxUtilsWarning.compareAndSet(false, true)) { |
aoqi@0 | 212 | LOGGER.log(Level.WARNING, "Put stax-utils.jar to the classpath to indent the dump output", ex); |
aoqi@0 | 213 | } |
aoqi@0 | 214 | } |
aoqi@0 | 215 | return writer; |
aoqi@0 | 216 | } |
aoqi@0 | 217 | } |