use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class JAXRSInInterceptor method handleMessage.
public void handleMessage(Message message) {
final Exchange exchange = message.getExchange();
exchange.put(Message.REST_MESSAGE, Boolean.TRUE);
Response response = exchange.get(Response.class);
if (response == null) {
try {
processRequest(message, exchange);
if (exchange.isOneWay()) {
ServerProviderFactory.getInstance(message).clearThreadLocalProxies();
}
} catch (Fault ex) {
convertExceptionToResponseIfPossible(ex.getCause(), message);
} catch (RuntimeException ex) {
convertExceptionToResponseIfPossible(ex, message);
} catch (IOException ex) {
convertExceptionToResponseIfPossible(ex, message);
}
}
response = exchange.get(Response.class);
if (response != null) {
createOutMessage(message, response);
message.getInterceptorChain().doInterceptStartingAt(message, OutgoingChainInterceptor.class.getName());
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class JAXRSDefaultFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
if (PropertyUtils.isTrue(message.getExchange().get(JAXRSUtils.SECOND_JAXRS_EXCEPTION))) {
return;
}
final Fault f = (Fault) message.getContent(Exception.class);
Response r = JAXRSUtils.convertFaultToResponse(f.getCause(), message);
if (r != null) {
JAXRSUtils.setMessageContentType(message, r);
message.setContent(List.class, new MessageContentsList(r));
if (message.getExchange().getOutMessage() == null && message.getExchange().getOutFaultMessage() != null) {
message.getExchange().setOutMessage(message.getExchange().getOutFaultMessage());
}
new JAXRSOutInterceptor().handleMessage(message);
return;
}
ServerProviderFactory.releaseRequestState(message);
if (mustPropogateException(message)) {
throw f;
}
new StaxOutInterceptor().handleMessage(message);
message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
NSStack nsStack = new NSStack();
nsStack.push();
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
try {
nsStack.add("http://cxf.apache.org/bindings/xformat");
String prefix = nsStack.getPrefix("http://cxf.apache.org/bindings/xformat");
StaxUtils.writeStartElement(writer, prefix, "XMLFault", "http://cxf.apache.org/bindings/xformat");
StaxUtils.writeStartElement(writer, prefix, "faultstring", "http://cxf.apache.org/bindings/xformat");
Throwable t = f.getCause();
writer.writeCharacters(t == null ? f.getMessage() : t.toString());
// fault string
writer.writeEndElement();
if (f.getDetail() != null) {
StaxUtils.writeStartElement(writer, prefix, "detail", "http://cxf.apache.org/bindings/xformat");
StaxUtils.writeNode(DOMUtils.getChild(f.getDetail(), Node.ELEMENT_NODE), writer, false);
writer.writeEndElement();
}
// fault root
writer.writeEndElement();
writer.flush();
} catch (XMLStreamException xe) {
throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class XMLFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
if (mustPropogateException(message)) {
throw (Fault) message.getContent(Exception.class);
}
Fault f = (Fault) message.getContent(Exception.class);
message.put(org.apache.cxf.message.Message.RESPONSE_CODE, f.getStatusCode());
NSStack nsStack = new NSStack();
nsStack.push();
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
XMLFault xmlFault = XMLFault.createFault(f);
try {
nsStack.add(XMLConstants.NS_XML_FORMAT);
String prefix = nsStack.getPrefix(XMLConstants.NS_XML_FORMAT);
StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_ROOT, XMLConstants.NS_XML_FORMAT);
StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_STRING, XMLConstants.NS_XML_FORMAT);
Throwable t = xmlFault.getCause();
writer.writeCharacters(t == null ? xmlFault.getMessage() : t.toString());
// fault string
writer.writeEndElement();
if (xmlFault.getDetail() != null) {
Element detail = xmlFault.getDetail();
StaxUtils.writeStartElement(writer, prefix, XMLFault.XML_FAULT_DETAIL, XMLConstants.NS_XML_FORMAT);
Node node = detail.getFirstChild();
while (node != null) {
StaxUtils.writeNode(node, writer, false);
node = node.getNextSibling();
}
writer.writeEndElement();
}
// fault root
writer.writeEndElement();
writer.flush();
} catch (XMLStreamException xe) {
throw new Fault(new org.apache.cxf.common.i18n.Message("XML_WRITE_EXC", BUNDLE), xe);
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class XMLMessageOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
BindingOperationInfo boi = message.getExchange().getBindingOperationInfo();
MessageInfo mi;
BindingMessageInfo bmi;
if (isRequestor(message)) {
mi = boi.getOperationInfo().getInput();
bmi = boi.getInput();
} else {
mi = boi.getOperationInfo().getOutput();
bmi = boi.getOutput();
}
XMLBindingMessageFormat xmf = bmi.getExtensor(XMLBindingMessageFormat.class);
QName rootInModel = null;
if (xmf != null) {
rootInModel = xmf.getRootNode();
}
final int mpn = mi.getMessagePartsNumber();
if (boi.isUnwrapped() || mpn == 1) {
// wrapper out interceptor created the wrapper
// or if bare-one-param
new BareOutInterceptor().handleMessage(message);
} else {
if (rootInModel == null) {
rootInModel = boi.getName();
}
if (mpn == 0 && !boi.isUnwrapped()) {
// write empty operation qname
writeMessage(message, rootInModel, false);
} else {
// multi param, bare mode, needs write root node
writeMessage(message, rootInModel, true);
}
}
// in the end we do flush ;)
XMLStreamWriter writer = message.getContent(XMLStreamWriter.class);
try {
writer.flush();
} catch (XMLStreamException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("STAX_WRITE_EXC", BUNDLE, e));
}
}
use of org.apache.cxf.interceptor.Fault in project cxf by apache.
the class JAXBSchemaInitializer method createBridgeXsElement.
private void createBridgeXsElement(MessagePartInfo part, QName qn, QName typeName) {
SchemaInfo schemaInfo = serviceInfo.getSchema(qn.getNamespaceURI());
if (schemaInfo != null) {
XmlSchemaElement el = schemaInfo.getElementByQName(qn);
if (el == null) {
createXsElement(schemaInfo.getSchema(), part, typeName, schemaInfo);
} else if (!typeName.equals(el.getSchemaTypeName())) {
throw new Fault(new Message("CANNOT_CREATE_ELEMENT", LOG, qn, typeName, el.getSchemaTypeName()));
}
return;
}
XmlSchema schema = schemas.newXmlSchemaInCollection(qn.getNamespaceURI());
if (qualifiedSchemas) {
schema.setElementFormDefault(XmlSchemaForm.QUALIFIED);
}
schemaInfo = new SchemaInfo(qn.getNamespaceURI(), qualifiedSchemas, false);
schemaInfo.setSchema(schema);
createXsElement(schema, part, typeName, schemaInfo);
NamespaceMap nsMap = new NamespaceMap();
nsMap.add(WSDLConstants.CONVENTIONAL_TNS_PREFIX, schema.getTargetNamespace());
nsMap.add(WSDLConstants.NP_SCHEMA_XSD, WSDLConstants.NS_SCHEMA_XSD);
schema.setNamespaceContext(nsMap);
serviceInfo.addSchema(schemaInfo);
}
Aggregations