Skip to content

Commit

Permalink
[RESTEASY-3007] Introduce some hacks for the Jakarta XML Binding RI.
Browse files Browse the repository at this point in the history
https://issues.redhat.com/browse/RESTEASY-3007
Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Nov 9, 2021
1 parent 1ba8ced commit ad136cf
Show file tree
Hide file tree
Showing 21 changed files with 728 additions and 126 deletions.
@@ -1,6 +1,7 @@
package org.jboss.resteasy.plugins.providers.jaxb.fastinfoset;

import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer;
import org.jboss.resteasy.plugins.providers.jaxb.hacks.RiHacks;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;

Expand Down Expand Up @@ -34,7 +35,7 @@ public class FastinfoSetMarshaller implements Marshaller
public FastinfoSetMarshaller(final JAXBContext context) throws JAXBException
{
this.context = context;
marshaller = this.context.createMarshaller();
marshaller = RiHacks.createMarshaller(context);
}


Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.sun.xml.fastinfoset.stax.StAXDocumentParser;

import org.jboss.resteasy.plugins.providers.fastinfoset.i18n.Messages;
import org.jboss.resteasy.plugins.providers.jaxb.hacks.RiHacks;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

Expand Down Expand Up @@ -40,7 +41,7 @@ public class FastinfoSetUnmarshaller implements Unmarshaller

public FastinfoSetUnmarshaller(final JAXBContext context) throws JAXBException
{
unmarshaller = context.createUnmarshaller();
unmarshaller = RiHacks.createUnmarshaller(context);
}

protected static XMLStreamReader getFastinfoSetXMLStreamReader(InputStream entityStream)
Expand Down
16 changes: 16 additions & 0 deletions providers/jaxb/pom.xml
Expand Up @@ -39,6 +39,22 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<!-- Required only for the RiHacks. This can be removed when RESTEASY-2959 is resolved. We do not want other
modules using this implementation. Therefore we define it here explicitly. The version is aligned with
what is in WildFly.
-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3-b02</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
Expand Down
Expand Up @@ -24,6 +24,7 @@
import org.jboss.resteasy.core.interception.jaxrs.DecoratorMatcher;
import org.jboss.resteasy.core.messagebody.AsyncBufferedMessageBodyWriter;
import org.jboss.resteasy.plugins.providers.AbstractEntityProvider;
import org.jboss.resteasy.plugins.providers.jaxb.hacks.RiHacks;
import org.jboss.resteasy.plugins.providers.jaxb.i18n.LogMessages;
import org.jboss.resteasy.plugins.providers.jaxb.i18n.Messages;
import org.jboss.resteasy.spi.ResteasyConfiguration;
Expand Down Expand Up @@ -172,7 +173,7 @@ protected Marshaller getMarshaller(Class<?> type,
try
{
JAXBContext jaxb = findJAXBContext(type, annotations, mediaType, false);
Marshaller marshaller = jaxb.createMarshaller();
Marshaller marshaller = RiHacks.createMarshaller(jaxb);
setCharset(mediaType, marshaller);
// Pretty Print the XML response.
Object formatted = mediaType.getParameters().get("formatted");
Expand Down
Expand Up @@ -42,6 +42,7 @@
import org.jboss.resteasy.annotations.providers.jaxb.Wrapped;
import org.jboss.resteasy.core.ResteasyContext;
import org.jboss.resteasy.core.messagebody.AsyncBufferedMessageBodyWriter;
import org.jboss.resteasy.plugins.providers.jaxb.hacks.RiHacks;
import org.jboss.resteasy.plugins.providers.jaxb.i18n.LogMessages;
import org.jboss.resteasy.plugins.providers.jaxb.i18n.Messages;
import org.jboss.resteasy.spi.ResteasyConfiguration;
Expand Down Expand Up @@ -353,7 +354,7 @@ public void writeTo(Object entry, Class<?> type, Type genericType, Annotation[]


JAXBElement<JaxbCollection> collection = new JAXBElement<JaxbCollection>(new QName(namespaceURI, element, prefix), JaxbCollection.class, col);
Marshaller marshaller = ctx.createMarshaller();
Marshaller marshaller = RiHacks.createMarshaller(ctx);
AbstractJAXBProvider.decorateMarshaller(baseType, annotations, mediaType, marshaller);
marshaller.marshal(collection, entityStream);
}
Expand Down
@@ -1,6 +1,8 @@
package org.jboss.resteasy.plugins.providers.jaxb;

import jakarta.xml.bind.annotation.XmlNs;
import org.jboss.resteasy.annotations.providers.jaxb.JAXBConfig;
import org.jboss.resteasy.plugins.providers.jaxb.hacks.RiHacks;
import org.jboss.resteasy.plugins.providers.jaxb.i18n.LogMessages;
import org.jboss.resteasy.plugins.providers.jaxb.i18n.Messages;
import org.w3c.dom.Node;
Expand All @@ -20,12 +22,13 @@

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

/**
* A wrapper class around a JAXBContext that enables additional features
Expand All @@ -38,42 +41,7 @@
public class JAXBContextWrapper extends JAXBContext
{

private static final String NAMESPACE_PREFIX_MAPPER = "org.glassfish.jaxb.namespacePrefixMapper";
private static Constructor mapperConstructor = null;

static
{
try
{
// check to see if NamespacePrefixMapper is in classpath
final Class[] namespace = new Class[1];
final Class[] mapper = new Class[1];

if (System.getSecurityManager() == null)
{
namespace[0] = JAXBContextWrapper.class.getClassLoader().loadClass("org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper");
mapper[0] = JAXBContextWrapper.class.getClassLoader().loadClass("org.jboss.resteasy.plugins.providers.jaxb.XmlNamespacePrefixMapper");
}
else
{
AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
@Override public Void run() throws Exception {
namespace[0] = JAXBContextWrapper.class.getClassLoader().loadClass("org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper");
mapper[0] = JAXBContextWrapper.class.getClassLoader().loadClass("org.jboss.resteasy.plugins.providers.jaxb.XmlNamespacePrefixMapper");

return null;
}
});
}

mapperConstructor = mapper[0].getConstructors()[0];
}
catch (ClassNotFoundException | PrivilegedActionException e)
{

}

}
private static final String NAMESPACE_PREFIX_MAPPER = "com.sun.xml.bind.namespacePrefixMapper";

private final JAXBContext wrappedContext;
private final ThreadLocal<Unmarshaller> unmarshaller = new ThreadLocal<Unmarshaller>();
Expand Down Expand Up @@ -186,17 +154,22 @@ private void processConfig(JAXBConfig config) throws JAXBException
{
if (config.useNameSpacePrefix())
{
if (mapperConstructor == null)
{
throw new JAXBException(Messages.MESSAGES.namespacePrefixMapperNotInClassPath());
}
try
{
mapper = mapperConstructor.newInstance((Object[])config.namespaces());
}
catch (Exception e)
{
throw new JAXBException(e);
try {
final Map<String, String> namespaces = new HashMap<>();
for (XmlNs xmlNs : config.namespaces()) {
namespaces.put(xmlNs.namespaceURI(), xmlNs.prefix());
}
final BiFunction<String, String, String> mapperFunction = (namespace, suggestion) -> {
if (namespaces.containsKey(namespace)) {
return namespaces.get(namespace);
}
return suggestion;
};
mapper = RiHacks.createNamespacePrefixMapper(mapperFunction);
} catch (JAXBException e) {
throw e;
} catch (Exception e) {
throw Messages.MESSAGES.namespacePrefixMapperNotInClassPath(e);
}
}
if (!"".equals(config.schema()))
Expand Down Expand Up @@ -273,7 +246,7 @@ public JAXBIntrospector createJAXBIntrospector()
*/
public Marshaller createMarshaller() throws JAXBException
{
Marshaller marshaller = wrappedContext.createMarshaller();
Marshaller marshaller = RiHacks.createMarshaller(wrappedContext);
if (mapper != null)
{
try
Expand All @@ -298,7 +271,7 @@ public Unmarshaller createUnmarshaller() throws JAXBException
Unmarshaller u = unmarshaller.get();
if (u == null)
{
u = wrappedContext.createUnmarshaller();
u = RiHacks.createUnmarshaller(wrappedContext);
unmarshaller.set(u);
}
return u;
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.jboss.resteasy.annotations.providers.jaxb.WrappedMap;
import org.jboss.resteasy.core.ResteasyContext;
import org.jboss.resteasy.core.messagebody.AsyncBufferedMessageBodyWriter;
import org.jboss.resteasy.plugins.providers.jaxb.hacks.RiHacks;
import org.jboss.resteasy.plugins.providers.jaxb.i18n.LogMessages;
import org.jboss.resteasy.plugins.providers.jaxb.i18n.Messages;
import org.jboss.resteasy.spi.ResteasyConfiguration;
Expand Down Expand Up @@ -303,7 +304,7 @@ public void writeTo(Object target, Class<?> type, Type genericType, Annotation[]
}

JAXBElement<JaxbMap> jaxbMap = new JAXBElement<JaxbMap>(new QName(namespaceURI, mapName, prefix), JaxbMap.class, map);
Marshaller marshaller = ctx.createMarshaller();
Marshaller marshaller = RiHacks.createMarshaller(ctx);
marshaller = AbstractJAXBProvider.decorateMarshaller(valueType, annotations, mediaType, marshaller);
marshaller.marshal(jaxbMap, entityStream);
}
Expand Down
Expand Up @@ -24,7 +24,7 @@ public Marshaller decorate(Marshaller target, Stylesheet annotation, Class type,
String h = "<?xml-stylesheet type='" + doctype + "' href='" + href + "' ?>";
try
{
target.setProperty("org.glassfish.jaxb.xmlHeaders", h);
target.setProperty("com.sun.xml.bind.xmlHeaders", h);
}
catch (PropertyException e)
{
Expand Down
Expand Up @@ -22,7 +22,7 @@ public Marshaller decorate(Marshaller target, XmlHeader annotation, Class type,
String h = StringContextReplacement.replace(annotation.value());
try
{
target.setProperty("org.glassfish.jaxb.xmlHeaders", h);
target.setProperty("com.sun.xml.bind.xmlHeaders", h);
}
catch (PropertyException e)
{
Expand Down
Expand Up @@ -11,7 +11,9 @@
*
* @author <a href="ryan@damnhandy.com">Ryan J. McDonough</a>
* @version $Revision:$
* @deprecated This should no longer be used as it's quite simple to implement and is tied to the implementation
*/
@Deprecated
public class XmlNamespacePrefixMapper extends NamespacePrefixMapper
{

Expand Down

0 comments on commit ad136cf

Please sign in to comment.