use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class Soap11DataFormatAdapter method createExceptionFromFault.
/**
* Creates an exception and eventually an embedded bean that contains the
* fault detail. The exception class is determined by using the
* elementNameStrategy. The qName of the fault detail should match the
* WebFault annotation of the Exception class. If no fault detail is set a
* SOAPFaultException is created.
*
* @param fault Soap fault
* @return created Exception
*/
private Exception createExceptionFromFault(Fault fault) {
String message = fault.getFaultstring();
Detail faultDetail = fault.getDetail();
if (faultDetail == null || faultDetail.getAny().size() == 0) {
try {
return new SOAPFaultException(SOAPFactory.newInstance().createFault(message, fault.getFaultcode()));
} catch (SOAPException e) {
throw new RuntimeCamelException(e);
}
}
JAXBElement<?> detailEl = (JAXBElement<?>) faultDetail.getAny().get(0);
Class<? extends Exception> exceptionClass = getDataFormat().getElementNameStrategy().findExceptionForFaultName(detailEl.getName());
Constructor<? extends Exception> messageConstructor;
Constructor<? extends Exception> constructor;
try {
messageConstructor = exceptionClass.getConstructor(String.class);
Object detail = JAXBIntrospector.getValue(detailEl);
try {
constructor = exceptionClass.getConstructor(String.class, detail.getClass());
return constructor.newInstance(message, detail);
} catch (NoSuchMethodException e) {
return messageConstructor.newInstance(message);
}
} catch (Exception e) {
throw new RuntimeCamelException(e);
}
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class Soap12DataFormatAdapter method doUnmarshal.
@Override
public Object doUnmarshal(Exchange exchange, InputStream stream, Object rootObject) throws IOException {
if (rootObject.getClass() != Envelope.class) {
throw new RuntimeCamelException("Expected Soap Envelope but got " + rootObject.getClass());
}
Envelope envelope = (Envelope) rootObject;
Header header = envelope.getHeader();
if (header != null) {
List<Object> returnHeaders;
List<Object> anyHeaderElements = envelope.getHeader().getAny();
if (null != anyHeaderElements && !(getDataFormat().isIgnoreUnmarshalledHeaders())) {
if (getDataFormat().isIgnoreJAXBElement()) {
returnHeaders = new ArrayList<Object>();
for (Object headerEl : anyHeaderElements) {
returnHeaders.add(JAXBIntrospector.getValue(headerEl));
}
} else {
returnHeaders = anyHeaderElements;
}
exchange.getOut().setHeader(SoapJaxbDataFormat.SOAP_UNMARSHALLED_HEADER_LIST, returnHeaders);
}
}
List<Object> anyElement = envelope.getBody().getAny();
if (anyElement.size() == 0) {
// No parameter so return null
return null;
}
Object payloadEl = anyElement.get(0);
Object payload = JAXBIntrospector.getValue(payloadEl);
if (payload instanceof Fault) {
Exception exception = createExceptionFromFault((Fault) payload);
exchange.setException(exception);
return null;
} else {
return getDataFormat().isIgnoreJAXBElement() ? payload : payloadEl;
}
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class Soap12DataFormatAdapter method createExceptionFromFault.
/**
* Creates an exception and eventually an embedded bean that contains the
* fault detail. The exception class is determined by using the
* elementNameStrategy. The qName of the fault detail should match the
* WebFault annotation of the Exception class. If no fault detail is set
* a {@link javax.xml.ws.soap.SOAPFaultException} is created.
*
* @param fault Soap fault
* @return created Exception
*/
private Exception createExceptionFromFault(Fault fault) {
StringBuilder sb = new StringBuilder();
for (Reasontext text : fault.getReason().getText()) {
sb.append(text.getValue());
}
String message = sb.toString();
Detail faultDetail = fault.getDetail();
if (faultDetail == null || faultDetail.getAny().size() == 0) {
try {
return new SOAPFaultException(SOAPFactory.newInstance().createFault(message, fault.getCode().getValue()));
} catch (SOAPException e) {
throw new RuntimeCamelException(e);
}
}
JAXBElement<?> detailEl = (JAXBElement<?>) faultDetail.getAny().get(0);
Class<? extends Exception> exceptionClass = getDataFormat().getElementNameStrategy().findExceptionForFaultName(detailEl.getName());
Constructor<? extends Exception> messageConstructor;
Constructor<? extends Exception> constructor;
try {
messageConstructor = exceptionClass.getConstructor(String.class);
Object detail = JAXBIntrospector.getValue(detailEl);
try {
constructor = exceptionClass.getConstructor(String.class, detail.getClass());
return constructor.newInstance(message, detail);
} catch (NoSuchMethodException e) {
return messageConstructor.newInstance(message);
}
} catch (Exception e) {
throw new RuntimeCamelException(e);
}
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class SoapJaxbDataFormat method createContentFromObject.
/**
* Create body content from a non Exception object. If the inputObject is a
* BeanInvocation the following should be considered: The first parameter
* will be used for the SOAP body. BeanInvocations with more than one
* parameter are not supported. So the interface should be in doc lit bare
* style.
*
* @param inputObject
* object to be put into the SOAP body
* @param soapAction
* for name resolution
* @param headerElements
* in/out parameter used to capture header content if present
*
* @return JAXBElement for the body content
*/
protected List<Object> createContentFromObject(final Object inputObject, String soapAction, List<Object> headerElements) {
List<Object> bodyParts = new ArrayList<Object>();
List<Object> headerParts = new ArrayList<Object>();
if (inputObject instanceof BeanInvocation) {
BeanInvocation bi = (BeanInvocation) inputObject;
Annotation[][] annotations = bi.getMethod().getParameterAnnotations();
List<WebParam> webParams = new ArrayList<WebParam>();
for (Annotation[] singleParameterAnnotations : annotations) {
for (Annotation annotation : singleParameterAnnotations) {
if (annotation instanceof WebParam) {
webParams.add((WebParam) annotation);
}
}
}
if (webParams.size() > 0) {
if (webParams.size() == bi.getArgs().length) {
int index = -1;
for (Object o : bi.getArgs()) {
if (webParams.get(++index).header()) {
headerParts.add(o);
} else {
bodyParts.add(o);
}
}
} else {
throw new RuntimeCamelException("The number of bean invocation parameters does not " + "match the number of parameters annotated with @WebParam for the method [ " + bi.getMethod().getName() + "].");
}
} else {
// try to map all objects for the body
for (Object o : bi.getArgs()) {
bodyParts.add(o);
}
}
} else {
bodyParts.add(inputObject);
}
List<Object> bodyElements = new ArrayList<Object>();
for (Object bodyObj : bodyParts) {
QName name = elementNameStrategy.findQNameForSoapActionOrType(soapAction, bodyObj.getClass());
if (name == null) {
LOG.warn("Could not find QName for class " + bodyObj.getClass().getName());
continue;
} else {
bodyElements.add(getElement(bodyObj, name));
}
}
for (Object headerObj : headerParts) {
QName name = elementNameStrategy.findQNameForSoapActionOrType(soapAction, headerObj.getClass());
if (name == null) {
LOG.warn("Could not find QName for class " + headerObj.getClass().getName());
continue;
} else {
JAXBElement<?> headerElem = getElement(headerObj, name);
if (null != headerElem) {
headerElements.add(headerElem);
}
}
}
return bodyElements;
}
use of org.apache.camel.RuntimeCamelException in project camel by apache.
the class ICalDataFormatAutoConfiguration method configureICalDataFormatFactory.
@Bean(name = "ical-dataformat-factory")
@ConditionalOnClass(CamelContext.class)
@ConditionalOnMissingBean(ICalDataFormat.class)
public DataFormatFactory configureICalDataFormatFactory(final CamelContext camelContext, final ICalDataFormatConfiguration configuration) {
return new DataFormatFactory() {
public DataFormat newInstance() {
ICalDataFormat dataformat = new ICalDataFormat();
if (CamelContextAware.class.isAssignableFrom(ICalDataFormat.class)) {
CamelContextAware contextAware = CamelContextAware.class.cast(dataformat);
if (contextAware != null) {
contextAware.setCamelContext(camelContext);
}
}
try {
Map<String, Object> parameters = new HashMap<>();
IntrospectionSupport.getProperties(configuration, parameters, null, false);
IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), dataformat, parameters);
} catch (Exception e) {
throw new RuntimeCamelException(e);
}
return dataformat;
}
};
}
Aggregations