package com.sun.xml.internal.ws.commons.xmlutil;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.logging.Logger;
import com.sun.xml.internal.ws.api.message.Message;
import com.sun.xml.internal.ws.api.message.Messages;
import com.sun.xml.internal.ws.api.message.Packet;
import com.sun.xml.internal.ws.util.xml.XmlUtil;
import javax.xml.stream.*;
import javax.xml.xpath.XPathFactoryConfigurationException;
import java.io.*;
import java.lang.reflect.Constructor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
public final class Converter {
public static final String UTF_8 = "UTF-8";
private Converter() {
}
private static final Logger LOGGER = Logger.getLogger(Converter.class);
private static final ContextClassloaderLocal<XMLOutputFactory> xmlOutputFactory = new ContextClassloaderLocal<XMLOutputFactory>() {
@Override
protected XMLOutputFactory initialValue() throws Exception {
return XMLOutputFactory.newInstance();
}
};
private static final AtomicBoolean logMissingStaxUtilsWarning = new AtomicBoolean(false);
public static String toString(Throwable throwable) {
if (throwable == null) {
return "[ No exception ]";
}
StringWriter stringOut = new StringWriter();
throwable.printStackTrace(new PrintWriter(stringOut));
return stringOut.toString();
}
public static String toString(Packet packet) {
if (packet == null) {
return "[ Null packet ]";
} else if (packet.getMessage() == null) {
return "[ Empty packet ]";
}
return toString(packet.getMessage());
}
public static String toStringNoIndent(Packet packet) {
if (packet == null) {
return "[ Null packet ]";
} else if (packet.getMessage() == null) {
return "[ Empty packet ]";
}
return toStringNoIndent(packet.getMessage());
}
public static String toString(Message message) {
return toString(message, true);
}
public static String toStringNoIndent(Message message) {
return toString(message, false);
}
private static String toString(Message message, boolean createIndenter) {
if (message == null) {
return "[ Null message ]";
}
StringWriter stringOut = null;
try {
stringOut = new StringWriter();
XMLStreamWriter writer = null;
try {
writer = xmlOutputFactory.get().createXMLStreamWriter(stringOut);
if (createIndenter) {
writer = createIndenter(writer);
}
message.copy().writeTo(writer);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Unexpected exception occured while dumping message", e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (XMLStreamException ignored) {
LOGGER.fine("Unexpected exception occured while closing XMLStreamWriter", ignored);
}
}
}
return stringOut.toString();
} finally {
if (stringOut != null) {
try {
stringOut.close();
} catch (IOException ex) {
LOGGER.finest("An exception occured when trying to close StringWriter", ex);
}
}
}
}
public static byte[] toBytes(Message message, String encoding) throws XMLStreamException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
if (message != null) {
XMLStreamWriter xsw = xmlOutputFactory.get().createXMLStreamWriter(baos, encoding);
try {
message.writeTo(xsw);
} finally {
try {
xsw.close();
} catch (XMLStreamException ex) {
LOGGER.warning("Unexpected exception occured while closing XMLStreamWriter", ex);
}
}
}
return baos.toByteArray();
} finally {
try {
baos.close();
} catch (IOException ex) {
LOGGER.warning("Unexpected exception occured while closing ByteArrayOutputStream", ex);
}
}
}
public static Message toMessage(@NotNull InputStream dataStream, String encoding) throws XMLStreamException {
XMLStreamReader xsr = XmlUtil.newXMLInputFactory(true).createXMLStreamReader(dataStream, encoding);
return Messages.create(xsr);
}
public static String messageDataToString(final byte[] data, final String encoding) {
try {
return toString(toMessage(new ByteArrayInputStream(data), encoding));
} catch (XMLStreamException ex) {
LOGGER.warning("Unexpected exception occured while converting message data to string", ex);
return "[ Message Data Conversion Failed ]";
}
}
private static XMLStreamWriter createIndenter(XMLStreamWriter writer) {
try {
Class<?> clazz = Converter.class.getClassLoader().loadClass("javanet.staxutils.IndentingXMLStreamWriter");
Constructor<?> c = clazz.getConstructor(XMLStreamWriter.class);
writer = XMLStreamWriter.class.cast(c.newInstance(writer));
} catch (Exception ex) {
if (logMissingStaxUtilsWarning.compareAndSet(false, true)) {
LOGGER.log(Level.WARNING, "Put stax-utils.jar to the classpath to indent the dump output", ex);
}
}
return writer;
}
}