package com.sun.xml.internal.ws.handler;
import com.sun.xml.internal.ws.api.message.MessageHeaders;
import com.sun.xml.internal.ws.api.message.Packet;
import com.sun.xml.internal.ws.api.message.Message;
import com.sun.xml.internal.ws.api.message.AttachmentSet;
import com.sun.xml.internal.ws.api.WSBinding;
import com.sun.xml.internal.ws.spi.db.BindingContext;
import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
import com.sun.xml.internal.ws.util.xml.XmlUtil;
import com.sun.xml.internal.ws.message.EmptyMessageImpl;
import com.sun.xml.internal.ws.message.DOMMessage;
import com.sun.xml.internal.ws.message.jaxb.JAXBMessage;
import com.sun.xml.internal.ws.message.source.PayloadSourceMessage;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.ws.LogicalMessage;
import javax.xml.ws.WebServiceException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
class LogicalMessageImpl implements LogicalMessage {
private Packet packet;
protected BindingContext defaultJaxbContext;
private ImmutableLM lm = null;
public LogicalMessageImpl(BindingContext defaultJaxbContext, Packet
packet) {
this.packet = packet;
this.defaultJaxbContext = defaultJaxbContext;
}
public Source getPayload() {
if (lm == null) {
Source payload = packet.getMessage().copy().readPayloadAsSource();
if (payload instanceof DOMSource) {
lm = createLogicalMessageImpl(payload);
}
return payload;
} else {
return lm.getPayload();
}
}
public void setPayload(Source payload) {
lm = createLogicalMessageImpl(payload);
}
private ImmutableLM createLogicalMessageImpl(Source payload) {
if (payload == null) {
lm = new EmptyLogicalMessageImpl();
} else if (payload instanceof DOMSource) {
lm = new DOMLogicalMessageImpl((DOMSource) payload);
} else {
lm = new SourceLogicalMessageImpl(payload);
}
return lm;
}
public Object getPayload(BindingContext context) {
if (context == null) {
context = defaultJaxbContext;
}
if (context == null)
throw new WebServiceException("JAXBContext parameter cannot be null");
Object o;
if (lm == null) {
try {
o = packet.getMessage().copy().readPayloadAsJAXB(context.createUnmarshaller());
} catch (JAXBException e) {
throw new WebServiceException(e);
}
} else {
o = lm.getPayload(context);
lm = new JAXBLogicalMessageImpl(context.getJAXBContext(), o);
}
return o;
}
public Object getPayload(JAXBContext context) {
if (context == null) {
return getPayload(defaultJaxbContext);
}
if (context == null)
throw new WebServiceException("JAXBContext parameter cannot be null");
Object o;
if (lm == null) {
try {
o = packet.getMessage().copy().readPayloadAsJAXB(context.createUnmarshaller());
} catch (JAXBException e) {
throw new WebServiceException(e);
}
} else {
o = lm.getPayload(context);
lm = new JAXBLogicalMessageImpl(context, o);
}
return o;
}
public void setPayload(Object payload, BindingContext context) {
if (context == null) {
context = defaultJaxbContext;
}
if (payload == null) {
lm = new EmptyLogicalMessageImpl();
} else {
lm = new JAXBLogicalMessageImpl(context.getJAXBContext(), payload);
}
}
public void setPayload(Object payload, JAXBContext context) {
if (context == null) {
setPayload(payload, defaultJaxbContext);
}
if (payload == null) {
lm = new EmptyLogicalMessageImpl();
} else {
lm = new JAXBLogicalMessageImpl(context, payload);
}
}
public boolean isPayloadModifed() {
return (lm != null);
}
public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
assert isPayloadModifed();
if(isPayloadModifed()) {
return lm.getMessage(headers,attachments,binding);
} else {
return packet.getMessage();
}
}
private abstract class ImmutableLM {
public abstract Source getPayload();
public abstract Object getPayload(BindingContext context);
public abstract Object getPayload(JAXBContext context);
public abstract Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding);
}
private class DOMLogicalMessageImpl extends SourceLogicalMessageImpl {
private DOMSource dom;
public DOMLogicalMessageImpl(DOMSource dom) {
super(dom);
this.dom = dom;
}
@Override
public Source getPayload() {
return dom;
}
public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
Node n = dom.getNode();
if(n.getNodeType()== Node.DOCUMENT_NODE) {
n = ((Document)n).getDocumentElement();
}
return new DOMMessage(binding.getSOAPVersion(), headers, (Element)n, attachments);
}
}
private class EmptyLogicalMessageImpl extends ImmutableLM {
public EmptyLogicalMessageImpl() {
}
@Override
public Source getPayload() {
return null;
}
@Override
public Object getPayload(JAXBContext context) {
return null;
}
@Override
public Object getPayload(BindingContext context) {
return null;
}
public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
return new EmptyMessageImpl(headers,attachments,binding.getSOAPVersion());
}
}
private class JAXBLogicalMessageImpl extends ImmutableLM {
private JAXBContext ctxt;
private Object o;
public JAXBLogicalMessageImpl(JAXBContext ctxt, Object o) {
this.ctxt = ctxt;
this.o = o;
}
@Override
public Source getPayload() {
JAXBContext context = ctxt;
if (context == null) {
context = defaultJaxbContext.getJAXBContext();
}
try {
return new JAXBSource(context, o);
} catch (JAXBException e) {
throw new WebServiceException(e);
}
}
@Override
public Object getPayload(JAXBContext context) {
try {
Source payloadSrc = getPayload();
if (payloadSrc == null)
return null;
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(payloadSrc);
} catch (JAXBException e) {
throw new WebServiceException(e);
}
}
public Object getPayload(BindingContext context) {
try {
Source payloadSrc = getPayload();
if (payloadSrc == null)
return null;
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(payloadSrc);
} catch (JAXBException e) {
throw new WebServiceException(e);
}
}
public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
return JAXBMessage.create(BindingContextFactory.create(ctxt), o,binding.getSOAPVersion(), headers,attachments);
}
}
private class SourceLogicalMessageImpl extends ImmutableLM {
private Source payloadSrc;
public SourceLogicalMessageImpl(Source source) {
this.payloadSrc = source;
}
public Source getPayload() {
assert (!(payloadSrc instanceof DOMSource));
try {
Transformer transformer = XmlUtil.newTransformer();
DOMResult domResult = new DOMResult();
transformer.transform(payloadSrc, domResult);
DOMSource dom = new DOMSource(domResult.getNode());
lm = new DOMLogicalMessageImpl((DOMSource) dom);
payloadSrc = null;
return dom;
} catch (TransformerException te) {
throw new WebServiceException(te);
}
}
public Object getPayload(JAXBContext context) {
try {
Source payloadSrc = getPayload();
if (payloadSrc == null)
return null;
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(payloadSrc);
} catch (JAXBException e) {
throw new WebServiceException(e);
}
}
public Object getPayload(BindingContext context) {
try {
Source payloadSrc = getPayload();
if (payloadSrc == null)
return null;
Unmarshaller unmarshaller = context.createUnmarshaller();
return unmarshaller.unmarshal(payloadSrc);
} catch (JAXBException e) {
throw new WebServiceException(e);
}
}
public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
assert (payloadSrc!=null);
return new PayloadSourceMessage(headers, payloadSrc, attachments,binding.getSOAPVersion());
}
}
}