use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.
the class LogicalMessageImpl method setPayload.
public void setPayload(Source s) {
Message message = msgContext.getWrappedMessage();
Service.Mode mode = (Service.Mode) msgContext.getWrappedMessage().getContextualProperty(Service.Mode.class.getName());
SOAPMessage m = message.getContent(SOAPMessage.class);
if (m != null && !MessageUtils.isOutbound(message)) {
try {
SAAJUtils.getBody(m).removeContents();
W3CDOMStreamWriter writer = new W3CDOMStreamWriter(SAAJUtils.getBody(m));
StaxUtils.copy(s, writer);
writer.flush();
writer.close();
if (mode == Service.Mode.MESSAGE) {
s = new DOMSource(m.getSOAPPart());
} else {
s = new DOMSource(SAAJUtils.getBody(m).getFirstChild());
}
W3CDOMStreamReader r = new W3CDOMStreamReader(DOMUtils.getFirstElement(SAAJUtils.getBody(m)));
message.setContent(XMLStreamReader.class, r);
} catch (Exception e) {
throw new Fault(e);
}
} else if (mode != null) {
if (message instanceof SoapMessage) {
if (mode == Service.Mode.MESSAGE) {
try {
// REVISIT: should try to use the original SOAPMessage
// instead of creating a new empty one.
SOAPMessage msg = initSOAPMessage(null);
write(s, SAAJUtils.getBody(msg));
s = new DOMSource(msg.getSOAPPart());
} catch (Exception e) {
throw new Fault(e);
}
}
} else if (message instanceof XMLMessage && message.getContent(DataSource.class) != null) {
throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
}
} else {
XMLStreamReader reader = StaxUtils.createXMLStreamReader(s);
msgContext.getWrappedMessage().setContent(XMLStreamReader.class, reader);
}
msgContext.getWrappedMessage().setContent(Source.class, s);
}
use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.
the class ClientFaultConverter method processFaultDetail.
protected void processFaultDetail(Fault fault, Message msg) {
Element exDetail = (Element) DOMUtils.getChild(fault.getDetail(), Node.ELEMENT_NODE);
if (exDetail == null) {
return;
}
QName qname = new QName(exDetail.getNamespaceURI(), exDetail.getLocalName());
FaultInfo faultWanted = null;
MessagePartInfo part = null;
BindingOperationInfo boi = msg.getExchange().getBindingOperationInfo();
if (boi == null) {
return;
}
if (boi.isUnwrapped()) {
boi = boi.getWrappedOperation();
}
for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
if (qname.equals(mpi.getConcreteName())) {
faultWanted = faultInfo;
part = mpi;
break;
}
}
if (faultWanted != null) {
break;
}
}
if (faultWanted == null) {
// did not find it using the proper qualified names, we'll try again with just the localpart
for (FaultInfo faultInfo : boi.getOperationInfo().getFaults()) {
for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
if (qname.getLocalPart().equals(mpi.getConcreteName().getLocalPart())) {
faultWanted = faultInfo;
part = mpi;
break;
}
}
if (faultWanted != null) {
break;
}
}
}
if (faultWanted == null) {
return;
}
Service s = msg.getExchange().getService();
DataBinding dataBinding = s.getDataBinding();
Object e = null;
if (isDOMSupported(dataBinding)) {
DataReader<Node> reader = this.getNodeDataReader(msg);
reader.setProperty(DataReader.FAULT, fault);
e = reader.read(part, exDetail);
} else {
DataReader<XMLStreamReader> reader = this.getDataReader(msg);
XMLStreamReader xsr = new W3CDOMStreamReader(exDetail);
try {
xsr.nextTag();
} catch (XMLStreamException e1) {
throw new Fault(e1);
}
reader.setProperty(DataReader.FAULT, fault);
e = reader.read(part, xsr);
}
if (!(e instanceof Exception)) {
try {
Class<?> exClass = faultWanted.getProperty(Class.class.getName(), Class.class);
if (exClass == null) {
return;
}
if (e == null) {
Constructor<?> constructor = exClass.getConstructor(String.class);
e = constructor.newInstance(fault.getMessage());
} else {
try {
Constructor<?> constructor = getConstructor(exClass, e);
e = constructor.newInstance(fault.getMessage(), e);
} catch (NoSuchMethodException e1) {
// Use reflection to convert fault bean to exception
e = convertFaultBean(exClass, e, fault);
}
}
msg.setContent(Exception.class, e);
} catch (Exception e1) {
LogUtils.log(LOG, Level.INFO, "EXCEPTION_WHILE_CREATING_EXCEPTION", e1, e1.getMessage());
}
} else {
if (fault.getMessage() != null) {
Field f;
try {
f = Throwable.class.getDeclaredField("detailMessage");
ReflectionUtil.setAccessible(f);
f.set(e, fault.getMessage());
} catch (Exception e1) {
// ignore
}
}
msg.setContent(Exception.class, e);
}
}
use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.
the class ProviderImpl method convertToInternal.
/**
* Convert from EndpointReference to CXF internal 2005/08 EndpointReferenceType
*
* @param external the javax.xml.ws.EndpointReference
* @return CXF internal 2005/08 EndpointReferenceType
*/
public static EndpointReferenceType convertToInternal(EndpointReference external) {
if (external instanceof W3CEndpointReference) {
Unmarshaller um = null;
try {
DocumentFragment frag = DOMUtils.getEmptyDocument().createDocumentFragment();
DOMResult result = new DOMResult(frag);
external.writeTo(result);
W3CDOMStreamReader reader = new W3CDOMStreamReader(frag);
// CXF internal 2005/08 EndpointReferenceType should be
// compatible with W3CEndpointReference
// jaxContext = ContextUtils.getJAXBContext();
JAXBContext context = JAXBContext.newInstance(new Class[] { org.apache.cxf.ws.addressing.ObjectFactory.class });
um = context.createUnmarshaller();
return um.unmarshal(reader, EndpointReferenceType.class).getValue();
} catch (JAXBException e) {
throw new IllegalArgumentException("Could not unmarshal EndpointReference", e);
} finally {
JAXBUtils.closeUnmarshaller(um);
}
}
return null;
}
use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.
the class AegisElementDataReader method read.
/**
* Convert a DOM element to a type.
* @param input
* @return
*/
public Object read(Element input) throws Exception {
W3CDOMStreamReader sreader = new W3CDOMStreamReader(input);
// advance into the first tag
sreader.nextTag();
return reader.read(sreader);
}
use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.
the class Soap12FaultInInterceptor method unmarshalFault.
public static SoapFault unmarshalFault(SoapMessage message, XMLStreamReader reader) {
String exMessage = null;
QName faultCode = null;
List<QName> subCodes = null;
String role = null;
String node = null;
Element detail = null;
String lang = null;
Map<String, String> ns = new HashMap<>();
ns.put("s", Soap12.SOAP_NAMESPACE);
XPathUtils xu = new XPathUtils(ns);
try {
Node mainNode = message.getContent(Node.class);
Node fault = null;
if (reader instanceof W3CDOMStreamReader) {
W3CDOMStreamReader dr = (W3CDOMStreamReader) reader;
fault = dr.getCurrentElement();
dr.consumeFrame();
} else if (mainNode != null) {
Node bodyNode = (Node) xu.getValue("//s:Body", mainNode, XPathConstants.NODE);
StaxUtils.readDocElements(bodyNode.getOwnerDocument(), bodyNode, new FragmentStreamReader(reader), false, false);
fault = (Element) xu.getValue("//s:Fault", bodyNode, XPathConstants.NODE);
} else {
fault = StaxUtils.read(new FragmentStreamReader(reader));
}
fault = DOMUtils.getDomElement(fault);
Element el = (Element) xu.getValue("//s:Fault/s:Code/s:Value", fault, XPathConstants.NODE);
if (el != null) {
faultCode = DOMUtils.createQName(el.getTextContent(), el);
}
el = (Element) xu.getValue("//s:Fault/s:Code/s:Subcode", fault, XPathConstants.NODE);
if (el != null) {
subCodes = new LinkedList<QName>();
NodeList vlist = el.getElementsByTagNameNS(Soap12.SOAP_NAMESPACE, "Value");
for (int i = 0; i < vlist.getLength(); i++) {
Node v = vlist.item(i);
subCodes.add(DOMUtils.createQName(v.getTextContent(), v));
}
}
exMessage = (String) xu.getValue("//s:Fault/s:Reason/s:Text/text()", fault, XPathConstants.STRING);
lang = (String) xu.getValue("//s:Fault/s:Reason/s:Text/@xml:lang", fault, XPathConstants.STRING);
Node detailNode = (Node) xu.getValue("//s:Fault/s:Detail", fault, XPathConstants.NODE);
if (detailNode != null) {
detail = (Element) detailNode;
}
role = (String) xu.getValue("//s:Fault/s:Role/text()", fault, XPathConstants.STRING);
node = (String) xu.getValue("//s:Fault/s:Node/text()", fault, XPathConstants.STRING);
} catch (XMLStreamException e) {
throw new SoapFault("Could not parse message.", e, message.getVersion().getSender());
}
// if the fault's content is invalid and fautlCode is not found, blame the receiver
if (faultCode == null) {
faultCode = Soap12.getInstance().getReceiver();
exMessage = new Message("INVALID_FAULT", LOG).toString();
}
SoapFault fault = new SoapFault(exMessage, faultCode);
fault.setSubCodes(subCodes);
fault.setDetail(detail);
fault.setRole(role);
fault.setNode(node);
fault.setLang(lang);
return fault;
}
Aggregations