@SuppressWarnings("unchecked") @Test public void testInvokingServiceFromCxfProducer() throws Exception { if (MtomTestHelper.isAwtHeadless(logger, null)) { return; } final Holder<byte[]> photo = new Holder<byte[]>(MtomTestHelper.REQ_PHOTO_DATA); final Holder<Image> image = new Holder<Image>(getImage("/java.jpg")); Exchange exchange = context .createProducerTemplate() .send( "direct://testEndpoint", new Processor() { @Override public void process(Exchange exchange) throws Exception { exchange.getIn().setBody(new Object[] {photo, image}); } }); assertEquals("The attachement size should be 2 ", 2, exchange.getOut().getAttachments().size()); Object[] result = exchange.getOut().getBody(Object[].class); Holder<byte[]> photo1 = (Holder<byte[]>) result[1]; MtomTestHelper.assertEquals(MtomTestHelper.RESP_PHOTO_DATA, photo1.value); Holder<Image> image1 = (Holder<Image>) result[2]; Assert.assertNotNull(image1.value); if (image.value instanceof BufferedImage) { Assert.assertEquals(560, ((BufferedImage) image1.value).getWidth()); Assert.assertEquals(300, ((BufferedImage) image1.value).getHeight()); } }
@SuppressWarnings("unchecked") public void process(Exchange exchange) throws Exception { CxfPayload<SoapHeader> in = exchange.getIn().getBody(CxfPayload.class); // verify request assertEquals(1, in.getBody().size()); Map<String, String> ns = new HashMap<String, String>(); ns.put("ns", MtomTestHelper.SERVICE_TYPES_NS); ns.put("xop", MtomTestHelper.XOP_NS); XPathUtils xu = new XPathUtils(ns); Element body = new XmlConverter().toDOMElement(in.getBody().get(0)); Element ele = (Element) xu.getValue("//ns:Detail/ns:photo/xop:Include", body, XPathConstants.NODE); String photoId = ele.getAttribute("href").substring(4); // skip "cid:" assertEquals(MtomTestHelper.REQ_PHOTO_CID, photoId); ele = (Element) xu.getValue("//ns:Detail/ns:image/xop:Include", body, XPathConstants.NODE); String imageId = ele.getAttribute("href").substring(4); // skip "cid:" assertEquals(MtomTestHelper.REQ_IMAGE_CID, imageId); DataHandler dr = exchange.getIn().getAttachment(photoId); assertEquals("application/octet-stream", dr.getContentType()); MtomTestHelper.assertEquals( MtomTestHelper.REQ_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream())); dr = exchange.getIn().getAttachment(imageId); assertEquals("image/jpeg", dr.getContentType()); MtomTestHelper.assertEquals( MtomTestHelper.requestJpeg, IOUtils.readBytesFromStream(dr.getInputStream())); // create response List<Source> elements = new ArrayList<Source>(); elements.add( new DOMSource( DOMUtils.readXml(new StringReader(MtomTestHelper.RESP_MESSAGE)) .getDocumentElement())); CxfPayload<SoapHeader> sbody = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null); exchange.getOut().setBody(sbody); exchange .getOut() .addAttachment( MtomTestHelper.RESP_PHOTO_CID, new DataHandler( new ByteArrayDataSource( MtomTestHelper.RESP_PHOTO_DATA, "application/octet-stream"))); exchange .getOut() .addAttachment( MtomTestHelper.RESP_IMAGE_CID, new DataHandler(new ByteArrayDataSource(MtomTestHelper.responseJpeg, "image/jpeg"))); }
@Test public void testConsumer() throws Exception { if (MtomTestHelper.isAwtHeadless(logger, null)) { return; } context .createProducerTemplate() .send( "cxf:bean:consumerEndpoint", new Processor() { public void process(Exchange exchange) throws Exception { exchange.setPattern(ExchangePattern.InOut); assertEquals( "Get a wrong Content-Type header", "application/xop+xml", exchange.getIn().getHeader("Content-Type")); List<Source> elements = new ArrayList<Source>(); elements.add( new DOMSource( DOMUtils.readXml(new StringReader(getRequestMessage())) .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"))); } }); }