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