package org.glassfish.jersey.jaxb.internal;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.ext.Providers;
import jakarta.inject.Provider;
import jakarta.inject.Singleton;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import javax.xml.parsers.SAXParserFactory;
public abstract class XmlJaxbElementProvider extends AbstractJaxbElementProvider {
private final Provider<SAXParserFactory> spf;
public XmlJaxbElementProvider(Provider<SAXParserFactory> spf, Providers ps) {
super(ps);
this.spf = spf;
}
public XmlJaxbElementProvider(Provider<SAXParserFactory> spf, Providers ps, MediaType mt) {
super(ps, mt);
this.spf = spf;
}
@Produces("application/xml")
@Consumes("application/xml")
@Singleton
public static final class App extends XmlJaxbElementProvider {
public App(@Context Provider<SAXParserFactory> spf, @Context Providers ps) {
super(spf, ps, MediaType.APPLICATION_XML_TYPE);
}
}
@Produces("text/xml")
@Consumes("text/xml")
@Singleton
public static final class Text extends XmlJaxbElementProvider {
public Text(@Context Provider<SAXParserFactory> spf, @Context Providers ps) {
super(spf, ps, MediaType.TEXT_XML_TYPE);
}
}
@Produces("*/*,*/*+xml")
@Consumes("*/*,*/*+xml")
@Singleton
public static final class General extends XmlJaxbElementProvider {
public General(@Context Provider<SAXParserFactory> spf, @Context Providers ps) {
super(spf, ps);
}
@Override
protected boolean isSupported(MediaType m) {
return m.getSubtype().endsWith("+xml");
}
}
@Override
protected final JAXBElement<?> readFrom(Class<?> type, MediaType mediaType,
Unmarshaller unmarshaller, InputStream entityStream) throws JAXBException {
return unmarshaller.unmarshal(getSAXSource(spf.get(), entityStream), type);
}
@Override
protected final void writeTo(JAXBElement<?> t, MediaType mediaType, Charset c,
Marshaller m, OutputStream entityStream) throws JAXBException {
m.marshal(t, entityStream);
}
}