use of javax.xml.ws.WebFault in project camel by apache.
the class Soap11DataFormatAdapter method createFaultFromException.
/**
* Creates a SOAP fault from the exception and populates the message as well
* as the detail. The detail object is read from the method getFaultInfo of
* the throwable if present
*
* @param exception the cause exception
* @return SOAP fault from given Throwable
*/
@SuppressWarnings("unchecked")
private JAXBElement<Fault> createFaultFromException(final Throwable exception) {
WebFault webFault = exception.getClass().getAnnotation(WebFault.class);
if (webFault == null || webFault.targetNamespace() == null) {
throw new RuntimeException("The exception " + exception.getClass().getName() + " needs to have an WebFault annotation with name and targetNamespace", exception);
}
QName name = new QName(webFault.targetNamespace(), webFault.name());
Object faultObject;
try {
Method method = exception.getClass().getMethod("getFaultInfo");
faultObject = method.invoke(exception);
} catch (Exception e) {
throw new RuntimeCamelException("Exception while trying to get fault details", e);
}
Fault fault = new Fault();
fault.setFaultcode(FAULT_CODE_SERVER);
fault.setFaultstring(exception.getMessage());
Detail detailEl = new ObjectFactory().createDetail();
@SuppressWarnings("rawtypes") JAXBElement<?> faultDetailContent = new JAXBElement(name, faultObject.getClass(), faultObject);
detailEl.getAny().add(faultDetailContent);
fault.setDetail(detailEl);
return new ObjectFactory().createFault(fault);
}
use of javax.xml.ws.WebFault in project camel by apache.
the class ServiceInterfaceStrategy method addExceptions.
@SuppressWarnings("unchecked")
private void addExceptions(Method method) {
Class<?>[] exTypes = method.getExceptionTypes();
for (Class<?> exType : exTypes) {
WebFault webFault = exType.getAnnotation(WebFault.class);
if (webFault != null) {
QName faultName = new QName(webFault.targetNamespace(), webFault.name());
faultNameToException.put(faultName, (Class<? extends Exception>) exType);
}
}
}
use of javax.xml.ws.WebFault in project tomee by apache.
the class EjbMethodInvoker method preEjbInvoke.
private Object preEjbInvoke(Exchange exchange, Method method, List<Object> params) {
EjbMessageContext ctx = new EjbMessageContext(exchange.getInMessage(), Scope.APPLICATION);
WebServiceContextImpl.setMessageContext(ctx);
Map<String, Object> handlerProperties = removeHandlerProperties(ctx);
exchange.put(HANDLER_PROPERTIES, handlerProperties);
try {
EjbInterceptor interceptor = new EjbInterceptor(params, method, this.bus, exchange);
Object[] arguments = { ctx, interceptor };
RpcContainer container = (RpcContainer) this.beanContext.getContainer();
Class callInterface = this.beanContext.getServiceEndpointInterface();
method = getMostSpecificMethod(beanContext, method, callInterface);
Object res = container.invoke(this.beanContext.getDeploymentID(), InterfaceType.SERVICE_ENDPOINT, callInterface, method, arguments, null);
if (exchange.isOneWay()) {
return null;
}
return new MessageContentsList(res);
} catch (ApplicationException e) {
// when no handler is defined, EjbInterceptor will directly delegate
// to #directEjbInvoke. So if an application exception is thrown by
// the end user, when must consider the ApplicationException as a
// web fault if it contains the @WebFault exception
Throwable t = e.getCause();
if (t != null) {
if (RuntimeException.class.isAssignableFrom(t.getClass()) && t.getClass().isAnnotationPresent(javax.ejb.ApplicationException.class)) {
// it's not a checked exception so it can not be a WebFault
throw (RuntimeException) t;
} else if (!t.getClass().isAnnotationPresent(WebFault.class)) {
// not a web fault even if it's an EJB ApplicationException
exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
throw createFault(t, method, params, false);
}
} else {
// may not occurs ...
t = e;
}
// TODO may be we can change to FaultMode.CHECKED_APPLICATION_FAULT
exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
throw createFault(t, method, params, false);
} catch (Exception e) {
exchange.getInMessage().put(FaultMode.class, FaultMode.UNCHECKED_APPLICATION_FAULT);
throw createFault(e, method, params, false);
} finally {
WebServiceContextImpl.clear();
}
}
use of javax.xml.ws.WebFault in project cxf by apache.
the class WebFaultInInterceptor method getFaultName.
private QName getFaultName(Exception webFault) {
QName faultName = null;
WebFault wf = webFault.getClass().getAnnotation(WebFault.class);
if (wf != null) {
faultName = new QName(wf.targetNamespace(), wf.name());
}
return faultName;
}
use of javax.xml.ws.WebFault in project cxf by apache.
the class WebFaultOutInterceptor method handleMessage.
public void handleMessage(Message message) throws Fault {
Fault f = (Fault) message.getContent(Exception.class);
if (f == null) {
return;
}
try {
Throwable thr = f.getCause();
SOAPFaultException sf = null;
if (thr instanceof SOAPFaultException) {
sf = (SOAPFaultException) thr;
} else if (thr.getCause() instanceof SOAPFaultException) {
sf = (SOAPFaultException) thr.getCause();
}
if (sf != null) {
SoapVersion soapVersion = (SoapVersion) message.get(SoapVersion.class.getName());
if (soapVersion != null && soapVersion.getVersion() != 1.1) {
if (f instanceof SoapFault) {
for (Iterator<QName> it = CastUtils.cast(sf.getFault().getFaultSubcodes()); it.hasNext(); ) {
((SoapFault) f).addSubCode(it.next());
}
}
if (sf.getFault().getFaultReasonLocales().hasNext()) {
Locale lang = (Locale) sf.getFault().getFaultReasonLocales().next();
String convertedLang = lang.getLanguage();
String country = lang.getCountry();
if (country.length() > 0) {
convertedLang = convertedLang + '-' + country;
}
f.setLang(convertedLang);
}
}
message.setContent(Exception.class, f);
}
} catch (Exception e) {
// do nothing;
}
Throwable cause = f.getCause();
WebFault fault = null;
if (cause != null) {
fault = getWebFaultAnnotation(cause.getClass());
if (fault == null && cause.getCause() != null) {
fault = getWebFaultAnnotation(cause.getCause().getClass());
if (fault != null || cause instanceof RuntimeException) {
cause = cause.getCause();
}
}
}
if (cause instanceof Exception && fault != null) {
Exception ex = (Exception) cause;
Object faultInfo;
try {
Method method = cause.getClass().getMethod("getFaultInfo", new Class[0]);
faultInfo = method.invoke(cause, new Object[0]);
} catch (NoSuchMethodException e) {
faultInfo = createFaultInfoBean(fault, cause);
} catch (InvocationTargetException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("INVOCATION_TARGET_EXC", BUNDLE), e);
} catch (IllegalAccessException | IllegalArgumentException e) {
throw new Fault(new org.apache.cxf.common.i18n.Message("COULD_NOT_INVOKE", BUNDLE), e);
}
Service service = message.getExchange().getService();
try {
DataWriter<XMLStreamWriter> writer = service.getDataBinding().createWriter(XMLStreamWriter.class);
if (ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.OUT, message)) {
Schema schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
writer.setSchema(schema);
}
OperationInfo op = null;
// Prevent a NPE if we can't match the operation
if (message.getExchange().getBindingOperationInfo() != null) {
op = message.getExchange().getBindingOperationInfo().getOperationInfo();
}
QName faultName = getFaultName(fault, cause.getClass(), op);
MessagePartInfo part = op != null ? getFaultMessagePart(faultName, op) : null;
if (f.hasDetails()) {
writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getDetail()));
} else {
writer.write(faultInfo, part, new W3CDOMStreamWriter(f.getOrCreateDetail()));
if (!f.getDetail().hasChildNodes()) {
f.setDetail(null);
}
}
f.setMessage(ex.getMessage());
} catch (Fault f2) {
message.setContent(Exception.class, f2);
super.handleMessage(message);
} catch (Exception nex) {
// if exception occurs while writing a fault, we'll just let things continue
// and let the rest of the chain try handling it as is.
LOG.log(Level.WARNING, "EXCEPTION_WHILE_WRITING_FAULT", nex);
}
} else if (cause instanceof SOAPFaultException && ((SOAPFaultException) cause).getFault().hasDetail()) {
return;
} else if (f instanceof SoapFault && f.getCause() instanceof SOAPFaultException && ((SOAPFaultException) f.getCause()).getFault().hasDetail()) {
return;
} else {
FaultMode mode = message.get(FaultMode.class);
if (mode == FaultMode.CHECKED_APPLICATION_FAULT) {
// only convert checked exceptions with this
// otherwise delegate down to the normal protocol specific stuff
super.handleMessage(message);
}
}
}
Aggregations