@Test public void testExtractDataWithComment() throws IOException, SOAPException { XRoadMessage message = EXTRACTOR.extractData(new SaajSoapMessage(getMessageFromString("extractor/EarestFail6.xml"))); assertEquals(RESULT_PEM_FORMAT, message.getContent().toString()); }
public XRoadMessage<XmlObject> extractData(WebServiceMessage response) throws IOException { Node kehaNode; try { SaajSoapMessage message = (SaajSoapMessage) response; SOAPMessage mes = message.getSaajMessage(); Element body = mes.getSOAPBody(); NodeList kehaNodes = body.getChildNodes(); kehaNode = body.getChildNodes().item(0); if (kehaNode instanceof javax.xml.soap.Text) { kehaNode = getKehaNode(kehaNodes, 1); } if (kehaNodes.getLength() > 1) { for (int i = 0; i < kehaNodes.getLength(); i++) { if (kehaNodes.item(i).getParentNode().getParentNode().equals(body)) { kehaNode = kehaNodes.item(i); break; } } } } catch (SOAPException e) { throw new RuntimeException(e); } checkForNonTechnicalFault(kehaNode); try { XmlOptions options = new XmlOptions(); SchemaTypeLoader loader = XmlBeans.getContextTypeLoader(); QName responseElement = new QName(metadata.getResponseElementNs(), metadata.getResponseElementName()); SchemaType type = loader.findType(responseElement); XmlObject responseObj; if (StringUtils.isBlank(responseElement.getNamespaceURI()) && !StringUtils.equalsIgnoreCase(kehaNode.getNamespaceURI(), responseElement.getNamespaceURI())) { DOMSource src = new DOMSource(kehaNode); StringResult result = new StringResult(); TransformerFactory.newInstance().newTransformer().transform(src, result); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setNamespaceAware(false); ByteArrayInputStream stream = new ByteArrayInputStream(result.toString().getBytes("UTF-8")); kehaNode = SOAPUtil.getFirstNonTextChild(fac.newDocumentBuilder().parse(stream)); } if (type != null) { options.setDocumentType(type); options.setLoadReplaceDocumentElement(new QName("xml-fragment")); responseObj = XmlObject.Factory.parse(XmlObject.Factory.parse(kehaNode, options).toString(), new XmlOptions().setDocumentType(type)); } else { options.setLoadReplaceDocumentElement(responseElement); responseObj = XmlBeansUtil.getResponseObject(XmlObject.Factory.parse(kehaNode, options)); } Map<String, XRoadAttachment> attachments = new HashMap<String, XRoadAttachment>(); List<XRoadAttachment> undefined = new ArrayList<XRoadAttachment>(); for (Iterator<?> i = SOAPUtil.extractSoapMessage(response).getAttachments(); i.hasNext();) { AttachmentPart part = (AttachmentPart) i.next(); String cid = part.getContentId(); if (cid == null) { undefined.add(new XRoadAttachment(cid, part.getDataHandler())); } else { cid = cid.startsWith("<") && cid.endsWith(">") ? cid.substring(1, cid.length() - 1) : cid; attachments.put(cid, new XRoadAttachment(part.getContentId(), part.getDataHandler())); } } if (!attachments.isEmpty()) { for (XmlObject obj : XmlBeansUtil.getAllObjects(responseObj)) { for (Method method : XmlBeansUtil.getSwaRefSetters(obj)) { String field = XmlBeansUtil.getFieldName(method); String cid = XmlBeansUtil.getCid(obj, field); cid = cid.startsWith("cid:") ? cid.substring(4) : cid; XRoadAttachment attachment = attachments.get(cid); if (attachment != null) { attachments.remove(cid); method.invoke(obj, attachment.getDataHandler()); } } } } undefined.addAll(attachments.values()); return new XmlBeansXRoadMessage<XmlObject>(responseObj, undefined); } catch (Exception e) { throw new RuntimeException(e); } }
StandardXRoadConsumerMessageExtractor implements WebServiceMessageExtractor { public XRoadMessage<XmlObject> extractData(WebServiceMessage response) throws IOException { Node kehaNode; try { SaajSoapMessage message = (SaajSoapMessage) response; SOAPMessage mes = message.getSaajMessage(); Element body = mes.getSOAPBody(); NodeList kehaNodes = body.getChildNodes(); kehaNode = body.getChildNodes().item(0); if (kehaNode instanceof javax.xml.soap.Text) { kehaNode = getKehaNode(kehaNodes, 1); } if (kehaNodes.getLength() > 1) { for (int i = 0; i < kehaNodes.getLength(); i++) { if (kehaNodes.item(i).getParentNode().getParentNode().equals(body)) { kehaNode = kehaNodes.item(i); break; } } } } catch (SOAPException e) { throw new RuntimeException(e); } checkForNonTechnicalFault(kehaNode); try { XmlOptions options = new XmlOptions(); SchemaTypeLoader loader = XmlBeans.getContextTypeLoader(); QName responseElement = new QName(metadata.getResponseElementNs(), metadata.getResponseElementName()); SchemaType type = loader.findType(responseElement); XmlObject responseObj; if (StringUtils.isBlank(responseElement.getNamespaceURI()) && !StringUtils.equalsIgnoreCase(kehaNode.getNamespaceURI(), responseElement.getNamespaceURI())) { DOMSource src = new DOMSource(kehaNode); StringResult result = new StringResult(); TransformerFactory.newInstance().newTransformer().transform(src, result); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setNamespaceAware(false); ByteArrayInputStream stream = new ByteArrayInputStream(result.toString().getBytes("UTF-8")); kehaNode = SOAPUtil.getFirstNonTextChild(fac.newDocumentBuilder().parse(stream)); } if (type != null) { options.setDocumentType(type); options.setLoadReplaceDocumentElement(new QName("xml-fragment")); responseObj = XmlObject.Factory.parse(XmlObject.Factory.parse(kehaNode, options).toString(), new XmlOptions().setDocumentType(type)); } else { options.setLoadReplaceDocumentElement(responseElement); responseObj = XmlBeansUtil.getResponseObject(XmlObject.Factory.parse(kehaNode, options)); } Map<String, XRoadAttachment> attachments = new HashMap<String, XRoadAttachment>(); List<XRoadAttachment> undefined = new ArrayList<XRoadAttachment>(); for (Iterator<?> i = SOAPUtil.extractSoapMessage(response).getAttachments(); i.hasNext();) { AttachmentPart part = (AttachmentPart) i.next(); String cid = part.getContentId(); if (cid == null) { undefined.add(new XRoadAttachment(cid, part.getDataHandler())); } else { cid = cid.startsWith("<") && cid.endsWith(">") ? cid.substring(1, cid.length() - 1) : cid; attachments.put(cid, new XRoadAttachment(part.getContentId(), part.getDataHandler())); } } if (!attachments.isEmpty()) { for (XmlObject obj : XmlBeansUtil.getAllObjects(responseObj)) { for (Method method : XmlBeansUtil.getSwaRefSetters(obj)) { String field = XmlBeansUtil.getFieldName(method); String cid = XmlBeansUtil.getCid(obj, field); cid = cid.startsWith("cid:") ? cid.substring(4) : cid; XRoadAttachment attachment = attachments.get(cid); if (attachment != null) { attachments.remove(cid); method.invoke(obj, attachment.getDataHandler()); } } } } undefined.addAll(attachments.values()); return new XmlBeansXRoadMessage<XmlObject>(responseObj, undefined); } catch (Exception e) { throw new RuntimeException(e); } } }
StandardXRoadConsumerMessageExtractor implements WebServiceMessageExtractor { public XRoadMessage<XmlObject> extractData(WebServiceMessage response) throws IOException { Node kehaNode; try { SaajSoapMessage message = (SaajSoapMessage) response; SOAPMessage mes = message.getSaajMessage(); Element body = mes.getSOAPBody(); NodeList kehaNodes = body.getChildNodes(); kehaNode = body.getChildNodes().item(0); if (kehaNode instanceof javax.xml.soap.Text) { kehaNode = getKehaNode(kehaNodes, 1); } if (kehaNodes.getLength() > 1) { for (int i = 0; i < kehaNodes.getLength(); i++) { if (kehaNodes.item(i).getParentNode().getParentNode().equals(body)) { kehaNode = kehaNodes.item(i); break; } } } } catch (SOAPException e) { throw new RuntimeException(e); } checkForNonTechnicalFault(kehaNode); try { XmlOptions options = new XmlOptions(); SchemaTypeLoader loader = XmlBeans.getContextTypeLoader(); QName responseElement = new QName(metadata.getResponseElementNs(), metadata.getResponseElementName()); SchemaType type = loader.findType(responseElement); XmlObject responseObj; if (StringUtils.isBlank(responseElement.getNamespaceURI()) && !StringUtils.equalsIgnoreCase(kehaNode.getNamespaceURI(), responseElement.getNamespaceURI())) { DOMSource src = new DOMSource(kehaNode); StringResult result = new StringResult(); TransformerFactory.newInstance().newTransformer().transform(src, result); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setNamespaceAware(false); ByteArrayInputStream stream = new ByteArrayInputStream(result.toString().getBytes("UTF-8")); kehaNode = SOAPUtil.getFirstNonTextChild(fac.newDocumentBuilder().parse(stream)); } if (type != null) { options.setDocumentType(type); options.setLoadReplaceDocumentElement(new QName("xml-fragment")); responseObj = XmlObject.Factory.parse(XmlObject.Factory.parse(kehaNode, options).toString(), new XmlOptions().setDocumentType(type)); } else { options.setLoadReplaceDocumentElement(responseElement); responseObj = XmlBeansUtil.getResponseObject(XmlObject.Factory.parse(kehaNode, options)); } Map<String, XRoadAttachment> attachments = new HashMap<String, XRoadAttachment>(); List<XRoadAttachment> undefined = new ArrayList<XRoadAttachment>(); for (Iterator<?> i = SOAPUtil.extractSoapMessage(response).getAttachments(); i.hasNext();) { AttachmentPart part = (AttachmentPart) i.next(); String cid = part.getContentId(); if (cid == null) { undefined.add(new XRoadAttachment(cid, part.getDataHandler())); } else { cid = cid.startsWith("<") && cid.endsWith(">") ? cid.substring(1, cid.length() - 1) : cid; attachments.put(cid, new XRoadAttachment(part.getContentId(), part.getDataHandler())); } } if (!attachments.isEmpty()) { for (XmlObject obj : XmlBeansUtil.getAllObjects(responseObj)) { for (Method method : XmlBeansUtil.getSwaRefSetters(obj)) { String field = XmlBeansUtil.getFieldName(method); String cid = XmlBeansUtil.getCid(obj, field); cid = cid.startsWith("cid:") ? cid.substring(4) : cid; XRoadAttachment attachment = attachments.get(cid); if (attachment != null) { attachments.remove(cid); method.invoke(obj, attachment.getDataHandler()); } } } } undefined.addAll(attachments.values()); return new XmlBeansXRoadMessage<XmlObject>(responseObj, undefined); } catch (Exception e) { throw new RuntimeException(e); } } StandardXRoadConsumerMessageExtractor(XmlBeansXRoadMetadata metadata); }
StandardXRoadConsumerMessageExtractor implements WebServiceMessageExtractor { public XRoadMessage<XmlObject> extractData(WebServiceMessage response) throws IOException { Node kehaNode; try { SaajSoapMessage message = (SaajSoapMessage) response; SOAPMessage mes = message.getSaajMessage(); Element body = mes.getSOAPBody(); NodeList kehaNodes = body.getChildNodes(); kehaNode = body.getChildNodes().item(0); if (kehaNode instanceof javax.xml.soap.Text) { kehaNode = getKehaNode(kehaNodes, 1); } if (kehaNodes.getLength() > 1) { for (int i = 0; i < kehaNodes.getLength(); i++) { if (kehaNodes.item(i).getParentNode().getParentNode().equals(body)) { kehaNode = kehaNodes.item(i); break; } } } } catch (SOAPException e) { throw new RuntimeException(e); } checkForNonTechnicalFault(kehaNode); try { XmlOptions options = new XmlOptions(); SchemaTypeLoader loader = XmlBeans.getContextTypeLoader(); QName responseElement = new QName(metadata.getResponseElementNs(), metadata.getResponseElementName()); SchemaType type = loader.findType(responseElement); XmlObject responseObj; if (StringUtils.isBlank(responseElement.getNamespaceURI()) && !StringUtils.equalsIgnoreCase(kehaNode.getNamespaceURI(), responseElement.getNamespaceURI())) { DOMSource src = new DOMSource(kehaNode); StringResult result = new StringResult(); TransformerFactory.newInstance().newTransformer().transform(src, result); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setNamespaceAware(false); ByteArrayInputStream stream = new ByteArrayInputStream(result.toString().getBytes("UTF-8")); kehaNode = SOAPUtil.getFirstNonTextChild(fac.newDocumentBuilder().parse(stream)); } if (type != null) { options.setDocumentType(type); options.setLoadReplaceDocumentElement(new QName("xml-fragment")); responseObj = XmlObject.Factory.parse(XmlObject.Factory.parse(kehaNode, options).toString(), new XmlOptions().setDocumentType(type)); } else { options.setLoadReplaceDocumentElement(responseElement); responseObj = XmlBeansUtil.getResponseObject(XmlObject.Factory.parse(kehaNode, options)); } Map<String, XRoadAttachment> attachments = new HashMap<String, XRoadAttachment>(); List<XRoadAttachment> undefined = new ArrayList<XRoadAttachment>(); for (Iterator<?> i = SOAPUtil.extractSoapMessage(response).getAttachments(); i.hasNext();) { AttachmentPart part = (AttachmentPart) i.next(); String cid = part.getContentId(); if (cid == null) { undefined.add(new XRoadAttachment(cid, part.getDataHandler())); } else { cid = cid.startsWith("<") && cid.endsWith(">") ? cid.substring(1, cid.length() - 1) : cid; attachments.put(cid, new XRoadAttachment(part.getContentId(), part.getDataHandler())); } } if (!attachments.isEmpty()) { for (XmlObject obj : XmlBeansUtil.getAllObjects(responseObj)) { for (Method method : XmlBeansUtil.getSwaRefSetters(obj)) { String field = XmlBeansUtil.getFieldName(method); String cid = XmlBeansUtil.getCid(obj, field); cid = cid.startsWith("cid:") ? cid.substring(4) : cid; XRoadAttachment attachment = attachments.get(cid); if (attachment != null) { attachments.remove(cid); method.invoke(obj, attachment.getDataHandler()); } } } } undefined.addAll(attachments.values()); return new XmlBeansXRoadMessage<XmlObject>(responseObj, undefined); } catch (Exception e) { throw new RuntimeException(e); } } StandardXRoadConsumerMessageExtractor(XmlBeansXRoadMetadata metadata); XRoadMessage<XmlObject> extractData(WebServiceMessage response); }
StandardXRoadConsumerMessageExtractor implements WebServiceMessageExtractor { public XRoadMessage<XmlObject> extractData(WebServiceMessage response) throws IOException { Node kehaNode; try { SaajSoapMessage message = (SaajSoapMessage) response; SOAPMessage mes = message.getSaajMessage(); Element body = mes.getSOAPBody(); NodeList kehaNodes = body.getChildNodes(); kehaNode = body.getChildNodes().item(0); if (kehaNode instanceof javax.xml.soap.Text) { kehaNode = getKehaNode(kehaNodes, 1); } if (kehaNodes.getLength() > 1) { for (int i = 0; i < kehaNodes.getLength(); i++) { if (kehaNodes.item(i).getParentNode().getParentNode().equals(body)) { kehaNode = kehaNodes.item(i); break; } } } } catch (SOAPException e) { throw new RuntimeException(e); } checkForNonTechnicalFault(kehaNode); try { XmlOptions options = new XmlOptions(); SchemaTypeLoader loader = XmlBeans.getContextTypeLoader(); QName responseElement = new QName(metadata.getResponseElementNs(), metadata.getResponseElementName()); SchemaType type = loader.findType(responseElement); XmlObject responseObj; if (StringUtils.isBlank(responseElement.getNamespaceURI()) && !StringUtils.equalsIgnoreCase(kehaNode.getNamespaceURI(), responseElement.getNamespaceURI())) { DOMSource src = new DOMSource(kehaNode); StringResult result = new StringResult(); TransformerFactory.newInstance().newTransformer().transform(src, result); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setNamespaceAware(false); ByteArrayInputStream stream = new ByteArrayInputStream(result.toString().getBytes("UTF-8")); kehaNode = SOAPUtil.getFirstNonTextChild(fac.newDocumentBuilder().parse(stream)); } if (type != null) { options.setDocumentType(type); options.setLoadReplaceDocumentElement(new QName("xml-fragment")); responseObj = XmlObject.Factory.parse(XmlObject.Factory.parse(kehaNode, options).toString(), new XmlOptions().setDocumentType(type)); } else { options.setLoadReplaceDocumentElement(responseElement); responseObj = XmlBeansUtil.getResponseObject(XmlObject.Factory.parse(kehaNode, options)); } Map<String, XRoadAttachment> attachments = new HashMap<String, XRoadAttachment>(); List<XRoadAttachment> undefined = new ArrayList<XRoadAttachment>(); for (Iterator<?> i = SOAPUtil.extractSoapMessage(response).getAttachments(); i.hasNext();) { AttachmentPart part = (AttachmentPart) i.next(); String cid = part.getContentId(); if (cid == null) { undefined.add(new XRoadAttachment(cid, part.getDataHandler())); } else { cid = cid.startsWith("<") && cid.endsWith(">") ? cid.substring(1, cid.length() - 1) : cid; attachments.put(cid, new XRoadAttachment(part.getContentId(), part.getDataHandler())); } } if (!attachments.isEmpty()) { for (XmlObject obj : XmlBeansUtil.getAllObjects(responseObj)) { for (Method method : XmlBeansUtil.getSwaRefSetters(obj)) { String field = XmlBeansUtil.getFieldName(method); String cid = XmlBeansUtil.getCid(obj, field); cid = cid.startsWith("cid:") ? cid.substring(4) : cid; XRoadAttachment attachment = attachments.get(cid); if (attachment != null) { attachments.remove(cid); method.invoke(obj, attachment.getDataHandler()); } } } } undefined.addAll(attachments.values()); return new XmlBeansXRoadMessage<XmlObject>(responseObj, undefined); } catch (Exception e) { throw new RuntimeException(e); } } StandardXRoadConsumerMessageExtractor(XmlBeansXRoadMetadata metadata); XRoadMessage<XmlObject> extractData(WebServiceMessage response);  }