use of org.apache.camel.component.bean.BeanInvocation 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.component.bean.BeanInvocation in project camel by apache.
the class SoapJaxbDataFormat method marshal.
/**
* Marshal inputObjects to SOAP xml. If the exchange or message has an
* EXCEPTION_CAUGTH property or header then instead of the object the
* exception is marshaled.
*
* To determine the name of the top level xml elements the elementNameStrategy
* is used.
* @throws IOException,SAXException
*/
public void marshal(Exchange exchange, Object inputObject, OutputStream stream) throws IOException, SAXException {
checkElementNameStrategy(exchange);
String soapAction = getSoapActionFromExchange(exchange);
if (soapAction == null && inputObject instanceof BeanInvocation) {
BeanInvocation beanInvocation = (BeanInvocation) inputObject;
WebMethod webMethod = beanInvocation.getMethod().getAnnotation(WebMethod.class);
if (webMethod != null && webMethod.action() != null) {
soapAction = webMethod.action();
}
}
Object envelope = adapter.doMarshal(exchange, inputObject, stream, soapAction);
// and continue in super
super.marshal(exchange, envelope, stream);
}
use of org.apache.camel.component.bean.BeanInvocation in project camel by apache.
the class MultiPartClientMarshalTest method testSendPayload.
@Test
public void testSendPayload() throws Exception {
Exchange exchange = template.send("direct:start", new Processor() {
public void process(Exchange exchange) throws Exception {
BeanInvocation beanInvocation = new BeanInvocation();
GetCustomersByName getCustomersByName = new GetCustomersByName();
getCustomersByName.setName("Dr. Multipart");
beanInvocation.setMethod(MultiPartCustomerService.class.getMethod("getCustomersByName", GetCustomersByName.class, com.example.customerservice.multipart.Product.class));
Product product = new Product();
product.setName("Multiuse Product");
product.setDescription("Useful for lots of things.");
Object[] args = new Object[] { getCustomersByName, product };
beanInvocation.setArgs(args);
exchange.getIn().setBody(beanInvocation);
}
});
if (exchange.getException() != null) {
throw exchange.getException();
}
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsMap.put("example", "http://multipart.customerservice.example.com/");
XQueryBuilder builder = XQueryBuilder.xquery("//soap:Envelope/soap:Header/example:product/name");
builder.setNamespaces(nsMap);
String result = builder.evaluateAsString(exchange);
assertTrue(result.equals("Multiuse Product"));
builder = XQueryBuilder.xquery("//soap:Envelope/soap:Body/example:getCustomersByName/name");
builder.setNamespaces(nsMap);
result = builder.evaluateAsString(exchange);
assertTrue(result.equals("Dr. Multipart"));
}
use of org.apache.camel.component.bean.BeanInvocation in project camel by apache.
the class MultiPartCxfServerTest method testSendRequestWithInAndInOutParts.
@Test
public void testSendRequestWithInAndInOutParts() throws Exception {
Exchange exchange = producerTemplate.send("direct:start", new Processor() {
public void process(Exchange exchange) throws Exception {
BeanInvocation beanInvocation = new BeanInvocation();
beanInvocation.setMethod(MultiPartCustomerService.class.getMethod("saveCustomer", SaveCustomer.class, Product.class, Holder.class));
Customer customer = new Customer();
customer.setName("TestCustomer");
customer.setRevenue(50000);
SaveCustomer saveCustomer = new SaveCustomer();
saveCustomer.setCustomer(customer);
Product product = new Product();
product.setName("Multiuse Product");
product.setDescription("Useful for lots of things.");
Holder<Company> holder = new Holder<Company>();
Object[] args = new Object[] { saveCustomer, product, holder };
beanInvocation.setArgs(args);
exchange.getIn().setBody(beanInvocation);
}
});
if (exchange.getException() != null) {
throw exchange.getException();
}
@SuppressWarnings("unchecked") List<Object> headers = (List<Object>) exchange.getOut().getHeader(SoapJaxbDataFormat.SOAP_UNMARSHALLED_HEADER_LIST);
assertTrue(headers.size() == 1);
Object companyHeaderObj = headers.get(0);
assertTrue(companyHeaderObj instanceof Company);
assertTrue(((Company) companyHeaderObj).getName().equals("MultipartSoft"));
}
use of org.apache.camel.component.bean.BeanInvocation in project camel by apache.
the class MultiPartCxfServerTest method testSendRequestWithReusedInAndInOutParts.
/**
* This test validates the end-to-end behavior of the service interface mapping when a parameter type
* is defined with a different QName in two different Web method. It also tests the case where a
* QName and type are directly reused across methods.
*/
@Test
public void testSendRequestWithReusedInAndInOutParts() throws Exception {
Exchange exchange = producerTemplate.send("direct:start", new Processor() {
public void process(Exchange exchange) throws Exception {
BeanInvocation beanInvocation = new BeanInvocation();
beanInvocation.setMethod(MultiPartCustomerService.class.getMethod("saveCustomerToo", SaveCustomer.class, Product.class, Holder.class));
Customer customer = new Customer();
customer.setName("TestCustomerToo");
customer.setRevenue(50000);
SaveCustomer saveCustomer = new SaveCustomer();
saveCustomer.setCustomer(customer);
Product product = new Product();
product.setName("Multiuse Product");
product.setDescription("Useful for lots of things.");
Holder<Company> holder = new Holder<Company>();
Object[] args = new Object[] { saveCustomer, product, holder };
beanInvocation.setArgs(args);
exchange.getIn().setBody(beanInvocation);
}
});
if (exchange.getException() != null) {
throw exchange.getException();
}
@SuppressWarnings("unchecked") List<Object> headers = (List<Object>) exchange.getOut().getHeader(SoapJaxbDataFormat.SOAP_UNMARSHALLED_HEADER_LIST);
assertTrue(headers.size() == 1);
Object companyHeaderObj = headers.get(0);
assertTrue(companyHeaderObj instanceof Company);
assertTrue(((Company) companyHeaderObj).getName().equals("MultipartSoft"));
}
Aggregations