use of org.apache.camel.Processor in project camel by apache.
the class CxfJavaMtomProducerPayloadTest method testInvokingService.
@SuppressWarnings("unchecked")
@Test
public void testInvokingService() throws Exception {
if (MtomTestHelper.isAwtHeadless(null, log)) {
return;
}
final Holder<byte[]> photo = new Holder<byte[]>("RequestFromCXF".getBytes("UTF-8"));
final Holder<Image> image = new Holder<Image>(getImage("/java.jpg"));
Exchange exchange = context.createProducerTemplate().send(MTOM_ENDPOINT_URI_MTOM_ENABLE, new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(new Object[] { photo, image });
}
});
// Make sure we don't put the attachement into out message
assertEquals("The attachement size should be 0 ", 0, exchange.getOut().getAttachments().size());
Object[] result = exchange.getOut().getBody(Object[].class);
Holder<byte[]> photo1 = (Holder<byte[]>) result[1];
Holder<Image> image1 = (Holder<Image>) result[2];
assertEquals("ResponseFromCamel", new String(photo1.value, "UTF-8"));
assertNotNull(image1.value);
}
use of org.apache.camel.Processor in project camel by apache.
the class CxfMtomDisabledProducerPayloadModeTest method testProducer.
@Override
public void testProducer() throws Exception {
if (MtomTestHelper.isAwtHeadless(logger, null)) {
return;
}
Exchange exchange = context.createProducerTemplate().send("direct:testEndpoint", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
List<Source> elements = new ArrayList<Source>();
elements.add(new DOMSource(StaxUtils.read(new StringReader(MtomTestHelper.MTOM_DISABLED_REQ_MESSAGE)).getDocumentElement()));
CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null);
exchange.getIn().setBody(body);
exchange.getIn().addAttachment(MtomTestHelper.REQ_PHOTO_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.REQ_PHOTO_DATA, "application/octet-stream")));
exchange.getIn().addAttachment(MtomTestHelper.REQ_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.requestJpeg, "image/jpeg")));
}
});
// process response - verify response attachments
CxfPayload<?> out = exchange.getOut().getBody(CxfPayload.class);
Assert.assertEquals(1, out.getBody().size());
DataHandler dr = exchange.getOut().getAttachment(MtomTestHelper.RESP_PHOTO_CID);
Assert.assertEquals("application/octet-stream", dr.getContentType());
MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream()));
dr = exchange.getOut().getAttachment(MtomTestHelper.RESP_IMAGE_CID);
Assert.assertEquals("image/jpeg", dr.getContentType());
BufferedImage image = ImageIO.read(dr.getInputStream());
Assert.assertEquals(560, image.getWidth());
Assert.assertEquals(300, image.getHeight());
}
use of org.apache.camel.Processor in project camel by apache.
the class CxfRsProducerClientFactoryCache2Test method doRunTest.
private void doRunTest(ProducerTemplate template, final int clientPort) {
// use request as we want InOut
Exchange exchange = template.request("direct://http", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE);
inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/123");
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
inMessage.setHeader("clientPort", clientPort);
inMessage.setBody(null);
}
});
// get the response message
Customer response = (Customer) exchange.getOut().getBody();
assertNotNull("The response should not be null ", response);
assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123");
assertEquals("Get a wrong customer name", response.getName(), "John");
assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
use of org.apache.camel.Processor in project camel by apache.
the class CxfRsProducerTest method testProducerWithQueryParameters.
@Test
public void testProducerWithQueryParameters() {
Exchange exchange = template.send("cxfrs://http://localhost:" + getPort2() + "/" + getClass().getSimpleName() + "/testQuery?httpClientAPI=true&q1=12&q2=13&synchronous=true", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
// set the Http method
inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, InputStream.class);
inMessage.setBody(null);
}
});
// get the response message
String response = exchange.getOut().getBody(String.class);
assertNotNull("The response should not be null ", response);
assertEquals("The response value is wrong", "q1=12&q2=13", response);
}
use of org.apache.camel.Processor in project camel by apache.
the class CxfRsProducerTest method testGetCustomerWithClientProxyAPI.
@Test
public void testGetCustomerWithClientProxyAPI() {
// START SNIPPET: ProxyExample
Exchange exchange = template.send("direct://proxy", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
setupDestinationURL(inMessage);
// set the operation name
inMessage.setHeader(CxfConstants.OPERATION_NAME, "getCustomer");
// using the proxy client API
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
// set a customer header
inMessage.setHeader("key", "value");
// setup the accept content type
inMessage.setHeader(Exchange.ACCEPT_CONTENT_TYPE, "application/json");
// set the parameters , if you just have one parameter
// camel will put this object into an Object[] itself
inMessage.setBody("123");
}
});
// get the response message
Customer response = (Customer) exchange.getOut().getBody();
assertNotNull("The response should not be null ", response);
assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123");
assertEquals("Get a wrong customer name", response.getName(), "John");
assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
assertEquals("Get a wrong header value", "value", exchange.getOut().getHeader("key"));
// END SNIPPET: ProxyExample
}
Aggregations