Tue, 09 Apr 2013 14:51:13 +0100
8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com
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()); |
alanb@368 | 84 | |
alanb@368 | 85 | } |
alanb@368 | 86 | |
alanb@368 | 87 | public static String toString(Message message) { |
alanb@368 | 88 | if (message == null) { |
alanb@368 | 89 | return "[ Null message ]"; |
alanb@368 | 90 | } |
alanb@368 | 91 | StringWriter stringOut = null; |
alanb@368 | 92 | try { |
alanb@368 | 93 | stringOut = new StringWriter(); |
alanb@368 | 94 | XMLStreamWriter writer = null; |
alanb@368 | 95 | try { |
alanb@368 | 96 | writer = xmlOutputFactory.createXMLStreamWriter(stringOut); |
alanb@368 | 97 | writer = createIndenter(writer); |
alanb@368 | 98 | message.copy().writeTo(writer); |
alanb@368 | 99 | } catch (Exception e) { // WSIT-1596 - Message Dumping should not affect other processing |
alanb@368 | 100 | LOGGER.log(Level.WARNING, "Unexpected exception occured while dumping message", e); |
alanb@368 | 101 | } finally { |
alanb@368 | 102 | if (writer != null) { |
alanb@368 | 103 | try { |
alanb@368 | 104 | writer.close(); |
alanb@368 | 105 | } catch (XMLStreamException ignored) { |
alanb@368 | 106 | LOGGER.fine("Unexpected exception occured while closing XMLStreamWriter", ignored); |
alanb@368 | 107 | } |
alanb@368 | 108 | } |
alanb@368 | 109 | } |
alanb@368 | 110 | return stringOut.toString(); |
alanb@368 | 111 | } finally { |
alanb@368 | 112 | if (stringOut != null) { |
alanb@368 | 113 | try { |
alanb@368 | 114 | stringOut.close(); |
alanb@368 | 115 | } catch (IOException ex) { |
alanb@368 | 116 | LOGGER.finest("An exception occured when trying to close StringWriter", ex); |
alanb@368 | 117 | } |
alanb@368 | 118 | } |
alanb@368 | 119 | } |
alanb@368 | 120 | } |
alanb@368 | 121 | |
alanb@368 | 122 | public static byte[] toBytes(Message message, String encoding) throws XMLStreamException { |
alanb@368 | 123 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
alanb@368 | 124 | |
alanb@368 | 125 | try { |
alanb@368 | 126 | if (message != null) { |
alanb@368 | 127 | XMLStreamWriter xsw = xmlOutputFactory.createXMLStreamWriter(baos, encoding); |
alanb@368 | 128 | try { |
alanb@368 | 129 | message.writeTo(xsw); |
alanb@368 | 130 | } finally { |
alanb@368 | 131 | try { |
alanb@368 | 132 | xsw.close(); |
alanb@368 | 133 | } catch (XMLStreamException ex) { |
alanb@368 | 134 | LOGGER.warning("Unexpected exception occured while closing XMLStreamWriter", ex); |
alanb@368 | 135 | } |
alanb@368 | 136 | } |
alanb@368 | 137 | } |
alanb@368 | 138 | |
alanb@368 | 139 | return baos.toByteArray(); |
alanb@368 | 140 | } finally { |
alanb@368 | 141 | try { |
alanb@368 | 142 | baos.close(); |
alanb@368 | 143 | } catch (IOException ex) { |
alanb@368 | 144 | LOGGER.warning("Unexpected exception occured while closing ByteArrayOutputStream", ex); |
alanb@368 | 145 | } |
alanb@368 | 146 | } |
alanb@368 | 147 | } |
alanb@368 | 148 | |
alanb@368 | 149 | /** |
alanb@368 | 150 | * Converts JAX-WS RI message represented as input stream back to Message |
alanb@368 | 151 | * object. |
alanb@368 | 152 | * |
alanb@368 | 153 | * @param dataStream message data stream |
alanb@368 | 154 | * @param encoding message data stream encoding |
alanb@368 | 155 | * |
alanb@368 | 156 | * @return {@link com.sun.xml.internal.ws.api.message.Message} object created from the data stream |
alanb@368 | 157 | */ |
alanb@368 | 158 | public static Message toMessage(@NotNull InputStream dataStream, String encoding) throws XMLStreamException { |
alanb@368 | 159 | XMLStreamReader xsr = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(dataStream, encoding); |
alanb@368 | 160 | return Messages.create(xsr); |
alanb@368 | 161 | } |
alanb@368 | 162 | |
alanb@368 | 163 | public static String messageDataToString(final byte[] data, final String encoding) { |
alanb@368 | 164 | try { |
alanb@368 | 165 | return toString(toMessage(new ByteArrayInputStream(data), encoding)); |
alanb@368 | 166 | // closing ByteArrayInputStream has no effect, so ignoring the redundant call |
alanb@368 | 167 | } catch (XMLStreamException ex) { |
alanb@368 | 168 | LOGGER.warning("Unexpected exception occured while converting message data to string", ex); |
alanb@368 | 169 | return "[ Message Data Conversion Failed ]"; |
alanb@368 | 170 | } |
alanb@368 | 171 | } |
alanb@368 | 172 | |
alanb@368 | 173 | /** |
alanb@368 | 174 | * Wraps {@link javax.xml.stream.XMLStreamWriter} by an indentation engine if possible. |
alanb@368 | 175 | * |
alanb@368 | 176 | * <p> |
alanb@368 | 177 | * We can do this only when we have <tt>stax-utils.jar</tt> in the class path. |
alanb@368 | 178 | */ |
alanb@368 | 179 | private static XMLStreamWriter createIndenter(XMLStreamWriter writer) { |
alanb@368 | 180 | try { |
alanb@368 | 181 | Class<?> clazz = Converter.class.getClassLoader().loadClass("javanet.staxutils.IndentingXMLStreamWriter"); |
alanb@368 | 182 | Constructor<?> c = clazz.getConstructor(XMLStreamWriter.class); |
alanb@368 | 183 | writer = XMLStreamWriter.class.cast(c.newInstance(writer)); |
alanb@368 | 184 | } catch (Exception ex) { |
alanb@368 | 185 | // if stax-utils.jar is not in the classpath, this will fail |
alanb@368 | 186 | // so, we'll just have to do without indentation |
alanb@368 | 187 | if (logMissingStaxUtilsWarning.compareAndSet(false, true)) { |
alanb@368 | 188 | LOGGER.log(Level.WARNING, "Put stax-utils.jar to the classpath to indent the dump output", ex); |
alanb@368 | 189 | } |
alanb@368 | 190 | } |
alanb@368 | 191 | return writer; |
alanb@368 | 192 | } |
alanb@368 | 193 | } |