@SuppressWarnings({ "unchecked", "rawtypes" }) protected void handleResponse(Exchange exchange, Object response) throws Exception { if ("org.apache.camel.component.cxf.CxfPayload".equals(exchange.getIn().getBody().getClass().getName())) { if ("org.apache.camel.component.cxf.CxfPayload".equals(response.getClass().getName())) { exchange.getOut().setBody(response, CxfPayload.class); } else { List<Element> outElements = new ArrayList<Element>(); if (response instanceof Element) { outElements.add((Element)response); } else if (response instanceof String) { outElements.add(DomHelper.toDomDocument((String)response).getDocumentElement()); } CxfPayload responsePayload = new CxfPayload(null, outElements); exchange.getOut().setBody(responsePayload); } } else { if (response instanceof String) { exchange.getOut().setBody(response, String.class); } } }
public Object notify(String eventId, Long docId, Message request, int delay) { try { String requestStr = null; if ("org.apache.camel.component.cxf.CxfPayload".equals(request.getBody().getClass().getName())) { // special handling to extract XML @SuppressWarnings("rawtypes") CxfPayload cxfPayload = (CxfPayload) request.getBody(); requestStr = DomHelper.toXml((Node)cxfPayload.getBody().get(0)); } else { requestStr = request.getBody(String.class); } notifyProcesses(eventId, docId, requestStr, delay); return getResponse(0, "Success"); } catch (Exception ex) { logger.severeException(ex.getMessage(), ex); return getResponse(1, ex.toString()); } }
public Object invoke(Long processId, Long owningDocId, String masterRequestId, Message request, Map<String,Object> parameters) throws Exception { String requestStr = null; if ("org.apache.camel.component.cxf.CxfPayload".equals(request.getBody().getClass().getName())) { // special handling to extract XML @SuppressWarnings("rawtypes") CxfPayload cxfPayload = (CxfPayload) request.getBody(); requestStr = DomHelper.toXml((Node)cxfPayload.getBody().get(0)); } else { requestStr = request.getBody(String.class); } Process processVO = ProcessCache.getProcess(processId); if (processVO.getProcessType().equals(ProcessVisibilityConstant.SERVICE)) { String responseVarName = getResponseVariable(); return invokeServiceProcess(processId, owningDocId, masterRequestId, requestStr, parameters, responseVarName); } else { Long instanceId = launchProcess(processId, owningDocId, masterRequestId, parameters); return getResponse(0, "Process '" + processVO.getLabel() + "' instance ID " + instanceId + " launched."); } }
@Override @Before public void setUp() throws Exception { super.setUp(); File file = new File("src/test/resources/org/apache/camel/component/cxf/converter/test.xml"); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.parse(file); document.getDocumentElement().normalize(); List<Source> body = new ArrayList<Source>(); body.add(new DOMSource(document.getDocumentElement())); payload = new CxfPayload<String[]>(new ArrayList<String[]>(), body, null); emptyPayload = new CxfPayload<String[]>(new ArrayList<String[]>(), new ArrayList<Source>(), null); inputStream = new FileInputStream(file); }
@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(StaxUtils.read(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"))); } }); }
@SuppressWarnings("unchecked") public void process(Exchange exchange) throws Exception { CxfPayload<SoapHeader> in = exchange.getIn().getBody(CxfPayload.class); // verify request Assert.assertEquals(1, in.getBody().size()); DataHandler dr = exchange.getIn().getAttachment(MtomTestHelper.REQ_PHOTO_CID); Assert.assertEquals("application/octet-stream", dr.getContentType()); MtomTestHelper.assertEquals(MtomTestHelper.REQ_PHOTO_DATA, IOUtils.readBytesFromStream(dr.getInputStream())); dr = exchange.getIn().getAttachment(MtomTestHelper.REQ_IMAGE_CID); Assert.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(StaxUtils.read(new StringReader(MtomTestHelper.MTOM_DISABLED_RESP_MESSAGE)).getDocumentElement())); CxfPayload<SoapHeader> body = new CxfPayload<SoapHeader>(new ArrayList<SoapHeader>(), elements, null); exchange.getOut().setBody(body); 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"))); }
/** * Assumes a 'MasterRequestID' SOAP header element. Override for something different. */ @SuppressWarnings("unchecked") @Override public String getMasterRequestId(Message request) { List<SoapHeader> headers = (List<SoapHeader>) ((CxfPayload<?>)request.getBody()).getHeaders(); for (SoapHeader header : headers) { if (header.getName().getLocalPart().equals("MasterRequestID")) { Node headerNode = (Node) header.getObject(); return headerNode.getTextContent(); } } return null; }
@SuppressWarnings("rawtypes") public String realToString(Object obj) throws TranslationException { try { CxfPayload cxfPayload = (CxfPayload)obj; return DomHelper.toXml((Node)cxfPayload.getBody().get(0)); } catch (Exception ex) { throw new TranslationException(ex.getMessage(), ex); } }
@SuppressWarnings({"rawtypes","unchecked"}) public Object realToObject(String str) throws TranslationException { try { Document doc = DomHelper.toDomDocument(str); List<Element> body = new ArrayList<Element>(); body.add((Element)doc); return new CxfPayload(null, body); } catch (Exception ex) { throw new TranslationException(ex.getMessage(), ex); } }
@Converter public static <T> CxfPayload<T> elementToCxfPayload(Element element, Exchange exchange) { List<T> headers = new ArrayList<T>(); List<Element> body = new ArrayList<Element>(); body.add(element); return new CxfPayload<T>(headers, body); }
@Converter public static <T> CxfPayload<T> nodeListToCxfPayload(NodeList nodeList, Exchange exchange) { List<T> headers = new ArrayList<T>(); List<Element> body = new ArrayList<Element>(); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); // add all nodes to the body that are elements if (Element.class.isAssignableFrom(node.getClass())) { body.add((Element) node); } } return new CxfPayload<T>(headers, body); }
@Converter public static <T> CxfPayload<T> sourceToCxfPayload(Source src, Exchange exchange) { List<T> headers = new ArrayList<T>(); List<Source> body = new ArrayList<Source>(); body.add(src); return new CxfPayload<T>(headers, body, null); }
@Converter public static <T> Node cxfPayLoadToNode(CxfPayload<T> payload, Exchange exchange) { List<Element> payloadBodyElements = payload.getBody(); if (payloadBodyElements.size() > 0) { return payloadBodyElements.get(0); } return null; }
@Converter public static <T> Source cxfPayLoadToSource(CxfPayload<T> payload, Exchange exchange) { List<Source> payloadBody = payload.getBodySources(); if (payloadBody.size() > 0) { return payloadBody.get(0); } return null; }
private static <T, V> CxfPayload<T> convertVia(Class<V> via, Exchange exchange, Object value, TypeConverterRegistry registry) { TypeConverter tc = registry.lookup(via, value.getClass()); if (tc != null) { TypeConverter tc1 = registry.lookup(Document.class, via); if (tc1 != null) { V is = tc.convertTo(via, exchange, value); Document document = tc1.convertTo(Document.class, exchange, is); return documentToCxfPayload(document, exchange); } } return null; }
@Test public void testNodeListToCxfPayload() { NodeList nodeList = document.getChildNodes(); CxfPayload<String[]> payload = CxfPayloadConverter.nodeListToCxfPayload(nodeList, exchange); assertNotNull(payload); assertEquals("Get a wrong size of body", 1, payload.getBody().size()); }
@Test public void testToCxfPayload() { // use default type converter exchange.getIn().setBody(inputStream); CxfPayload<?> payload = exchange.getIn().getBody(CxfPayload.class); assertTrue(payload instanceof CxfPayload); assertEquals("Get a wrong size of body", 1, payload.getBodySources().size()); assertEquals("Get a wrong size of body", 1, payload.getBody().size()); assertEquals("expects stream source", "streamsource", payload.getBodySources().get(0).getClass().getSimpleName().toLowerCase()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void doTest(Object source, String payload) throws IOException { CxfPayload<?> originalPayload = context.getTypeConverter().convertTo(CxfPayload.class, source); CachedCxfPayload<?> cache = new CachedCxfPayload(originalPayload, exchange, new XmlConverter()); assertTrue(cache.inMemory()); ByteArrayOutputStream bos = new ByteArrayOutputStream(); cache.writeTo(bos); String s = context.getTypeConverter().convertTo(String.class, bos); assertEquals(payload, s); cache.reset(); CachedCxfPayload clone = (CachedCxfPayload) cache.copy(exchange); bos = new ByteArrayOutputStream(); clone.writeTo(bos); s = context.getTypeConverter().convertTo(String.class, bos); assertEquals(payload, s); cache.reset(); clone.reset(); s = context.getTypeConverter().convertTo(String.class, cache); assertEquals(payload, s); s = context.getTypeConverter().convertTo(String.class, clone); assertEquals(payload, s); }
@Converter public static String cxfPayloadToString(final CxfPayload<?> payload) { XmlConverter converter = new XmlConverter(); StringBuilder buf = new StringBuilder(); for (Object element : payload.getBody()) { String elementString = ""; try { elementString = converter.toString((Element)element, null); } catch (TransformerException e) { elementString = element.toString(); } buf.append(elementString); } return buf.toString(); }
protected CxfPayload<SoapHeader> initResult(WebServiceInvocationResult invocationResult) throws JAXBException, ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(invocationResult, doc); List<Element> outElements = new ArrayList<Element>(); outElements.add(doc.getDocumentElement()); CxfPayload<SoapHeader> responsePayload = new CxfPayload<SoapHeader>(null, outElements); System.out.println("Done creating CXF payload result..."); return responsePayload; }
@Override public String getRequestDocumentType(Message request) throws MdwCamelException { return CxfPayload.class.getName(); }
@Override public Object initializeRequestDocument(Message request) throws MdwCamelException { return (CxfPayload<?>)request.getBody(); }
@SuppressWarnings("rawtypes") public Document toDomDocument(Object obj) throws TranslationException { return (Document)((CxfPayload)obj).getBody().get(0); }
@SuppressWarnings({"rawtypes","unchecked"}) public Object fromDomNode(Node domNode) throws TranslationException { List nodeList = new ArrayList(); nodeList.add(domNode); return new CxfPayload(null, nodeList); }
@Converter public static <T> CxfPayload<T> documentToCxfPayload(Document doc, Exchange exchange) { return elementToCxfPayload(doc.getDocumentElement(), exchange); }
@Converter public static <T> NodeList cxfPayloadToNodeList(CxfPayload<T> payload, Exchange exchange) { return new NodeListWrapper(payload.getBody()); }
@Converter public static <T> StreamCache cxfPayLoadToStreamCache(CxfPayload<T> payload, Exchange exchange) { return new CachedCxfPayload<T>(payload, exchange, xml); }
@Test public void testDocumentToCxfPayload() { CxfPayload<String[]> payload = CxfPayloadConverter.documentToCxfPayload(document, exchange); assertNotNull(payload); assertEquals("Get a wrong size of body", 1, payload.getBody().size()); }
@Test public void testCxfPayloadToStreamCache() { StreamCache streamCache = CxfPayloadConverter.cxfPayLoadToStreamCache(payload, exchange); assertNotNull(streamCache); assertTrue(streamCache instanceof CxfPayload); }
@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(StaxUtils.read(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"))); }