private void serializeClient(final SOAPHeader header, final AbstractMessage message, final SOAPEnvelope envelope) throws SOAPException { LOGGER.debug("Generate \"Client\" element."); ObjectType clientObjectType = MessageHelper.getObjectType(message.getConsumer()); SOAPElement clientHeader = header.addChildElement(Constants.NS_XRD_ELEM_CLIENT, Constants.NS_XRD_PREFIX); clientHeader.addAttribute(envelope.createQName(Constants.NS_ID_ATTR_OBJECT_TYPE, Constants.NS_ID_PREFIX), clientObjectType.toString()); SOAPElement xRoadInstance = clientHeader.addChildElement(Constants.NS_ID_ELEM_XROAD_INSTANCE, Constants.NS_ID_PREFIX); xRoadInstance.addTextNode(message.getConsumer().getXRoadInstance()); SOAPElement memberClass = clientHeader.addChildElement(Constants.NS_ID_ELEM_MEMBER_CLASS, Constants.NS_ID_PREFIX); memberClass.addTextNode(message.getConsumer().getMemberClass()); SOAPElement memberCode = clientHeader.addChildElement(Constants.NS_ID_ELEM_MEMBER_CODE, Constants.NS_ID_PREFIX); memberCode.addTextNode(message.getConsumer().getMemberCode()); if (clientObjectType == ObjectType.SUBSYSTEM) { SOAPElement subsystem = clientHeader.addChildElement(Constants.NS_ID_ELEM_SUBSYSTEM_CODE, Constants.NS_ID_PREFIX); subsystem.addTextNode(message.getConsumer().getSubsystemCode()); } LOGGER.debug("\"Client\" element was succesfully generated."); }
@Override public boolean handleMessage(SOAPMessageContext context) { Boolean request = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (!request) { try { SOAPMessage msg = context.getMessage(); SOAPEnvelope env = msg.getSOAPPart().getEnvelope(); SOAPHeader header = env.getHeader(); if (header == null) { header = env.addHeader(); return false; } Node node = (Node) header.getElementsByTagName("token").item(0); String token = node.getChildNodes().item(0).getNodeValue(); if (token != null && token.equals("Kalango Lab")) { System.out.println("Token válido"); return true; } } catch (Exception e) { e.printStackTrace(); } return false; } return true; }
@SuppressWarnings("unchecked") private XTeeHeader parseXteeHeader(SOAPMessage paringMessage) throws SOAPException { XTeeHeader pais = new XTeeHeader(); if (paringMessage.getSOAPHeader() == null) { return pais; } SOAPHeader header = paringMessage.getSOAPHeader(); for (Iterator<Node> headerElemendid = header.getChildElements(); headerElemendid.hasNext();) { Node headerElement = headerElemendid.next(); if (!SOAPUtil.isTextNode(headerElement) && headerElement.getFirstChild() != null) { String localName = headerElement.getLocalName(); String value = headerElement.getFirstChild().getNodeValue(); pais.addElement(new QName(headerElement.getNamespaceURI(), localName), value); } } return pais; }
@Test public void parseApiVersion_ctmg_1_8() throws Exception { // given SOAPPart part = mock(SOAPPart.class); SOAPEnvelope envelope = mock(SOAPEnvelope.class); SOAPHeader soapHeader = mock(SOAPHeader.class); List<Node> version = new ArrayList<Node>(); Node node = mock(Node.class); doReturn("testVersion").when(node).getValue(); version.add(node); Iterator<?> it = version.iterator(); doReturn(it).when(soapHeader).extractHeaderElements( eq("ctmg.service.version")); doReturn(soapHeader).when(envelope).getHeader(); doReturn(envelope).when(part).getEnvelope(); doReturn(part).when(message).getSOAPPart(); // when String result = SoapRequestParser.parseApiVersion(context); // then assertEquals("testVersion", result); }
public void writeTo(SOAPMessage saaj) throws SOAPException { try { // TODO what about in-scope namespaces // Not very efficient consider implementing a stream buffer // processor that produces a DOM node from the buffer. TransformerFactory tf = XmlUtil.newTransformerFactory(); Transformer t = tf.newTransformer(); XMLStreamBufferSource source = new XMLStreamBufferSource(_mark); DOMResult result = new DOMResult(); t.transform(source, result); Node d = result.getNode(); if(d.getNodeType() == Node.DOCUMENT_NODE) d = d.getFirstChild(); SOAPHeader header = saaj.getSOAPHeader(); if(header == null) header = saaj.getSOAPPart().getEnvelope().addHeader(); Node node = header.getOwnerDocument().importNode(d, true); header.appendChild(node); } catch (Exception e) { throw new SOAPException(e); } }
/** Set the initial understood/not understood state of the headers in this * object */ private void initHeaderUnderstanding() { SOAPHeader soapHeader = ensureSOAPHeader(); if (soapHeader == null) { return; } Iterator allHeaders = soapHeader.examineAllHeaderElements(); while(allHeaders.hasNext()) { SOAPHeaderElement nextHdrElem = (SOAPHeaderElement) allHeaders.next(); if (nextHdrElem == null) { continue; } if (nextHdrElem.getMustUnderstand()) { notUnderstood(nextHdrElem.getElementQName()); } //only headers explicitly marked as understood should be //in the understoodHeaders set, so don't add anything to //that set at the beginning } }
public void writeTo(SOAPMessage saaj) throws SOAPException { try { // TODO what about in-scope namespaces // Not very efficient consider implementing a stream buffer // processor that produces a DOM node from the buffer. Transformer t = XmlUtil.newTransformer(); SOAPHeader header = saaj.getSOAPHeader(); if (header == null) header = saaj.getSOAPPart().getEnvelope().addHeader(); // TODO workaround for oracle xdk bug 16555545, when this bug is fixed the line below can be // uncommented and all lines below, except the catch block, can be removed. // t.transform(epr.asSource(localName), new DOMResult(header)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter w = XMLOutputFactory.newFactory().createXMLStreamWriter(baos); epr.writeTo(localName, w); w.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance(); fac.setNamespaceAware(true); Node eprNode = fac.newDocumentBuilder().parse(bais).getDocumentElement(); Node eprNodeToAdd = header.getOwnerDocument().importNode(eprNode, true); header.appendChild(eprNodeToAdd); } catch (Exception e) { throw new SOAPException(e); } }
@Override public void writeTo(SOAPMessage saaj) throws SOAPException { //TODO: SAAJ returns null instead of throwing SOAPException, // when there is no SOAPHeader in the message, // which leads to NPE. try { SOAPHeader header = saaj.getSOAPHeader(); if (header == null) { header = saaj.getSOAPPart().getEnvelope().addHeader(); } Element node = (Element)infoset.writeTo(header); node.setAttributeNS(AddressingVersion.W3C.nsUri,AddressingVersion.W3C.getPrefix()+":"+IS_REFERENCE_PARAMETER,TRUE_VALUE); } catch (XMLStreamBufferException e) { throw new SOAPException(e); } }
/** * Deserializes the client element of the SOAP header to a ConsumerMember * object. * * @param header SOAP header to be deserialized * @return ConsumerMember object * @throws XRd4JException if there's a XRd4J error * @throws XRd4JMissingMemberException if ConsumerMember is missing */ protected final ConsumerMember deserializeConsumer(final SOAPHeader header) throws XRd4JException, XRd4JMissingMemberException { LOGGER.debug("Deserialize \"{}\".", Constants.NS_XRD_ELEM_CLIENT); // Client headers Map<String, String> client; // Client object type ObjectType clientObjectType; NodeList list = header.getElementsByTagNameNS(Constants.NS_XRD_URL, Constants.NS_XRD_ELEM_CLIENT); if (list.getLength() == 1) { clientObjectType = this.deserializeObjectType((Node) list.item(0)); client = SOAPHelper.nodesToMap(list.item(0).getChildNodes()); LOGGER.trace(ELEMENT_FOUND_LOG_PATTERN, Constants.NS_XRD_ELEM_CLIENT); return this.getConsumerMember(client, clientObjectType); } LOGGER.warn("\"{}\" element missing from SOAP header.", Constants.NS_XRD_ELEM_CLIENT); throw new XRd4JMissingMemberException("Client element is missing from SOAP header."); }
/** * Deserializes the service element of the SOAP header to a ProducerMember * object. * * @param header SOAP header to be deserialized * @return ProducerMember object * @throws XRd4JException if there's a XRd4J error * @throws XRd4JMissingMemberException if ProducerMember is missing */ protected final ProducerMember deserializeProducer(final SOAPHeader header) throws XRd4JException, XRd4JMissingMemberException { LOGGER.debug(DESERIALIZE_LOG_PATTERN, Constants.NS_XRD_ELEM_SERVICE); // Service headers Map<String, String> service; // Service object type ObjectType serviceObjectType; NodeList list = header.getElementsByTagNameNS(Constants.NS_XRD_URL, Constants.NS_XRD_ELEM_SERVICE); if (list.getLength() == 1) { serviceObjectType = this.deserializeObjectType((Node) list.item(0)); service = SOAPHelper.nodesToMap(list.item(0).getChildNodes()); LOGGER.trace(ELEMENT_FOUND_LOG_PATTERN, Constants.NS_XRD_ELEM_SERVICE); return this.getProducerMember(service, serviceObjectType); } LOGGER.warn("\"{}\" element missing from SOAP header.", Constants.NS_XRD_ELEM_SERVICE); throw new XRd4JMissingMemberException("Service element is missing from SOAP header."); }
private void serializeService(final SOAPHeader header, final AbstractMessage message, final SOAPEnvelope envelope) throws SOAPException { LOGGER.debug("Generate \"Service\" element."); ObjectType serviceObjectType = MessageHelper.getObjectType(message.getProducer()); SOAPElement serviceHeader = header.addChildElement(Constants.NS_XRD_ELEM_SERVICE, Constants.NS_XRD_PREFIX); serviceHeader.addAttribute(envelope.createQName(Constants.NS_ID_ATTR_OBJECT_TYPE, Constants.NS_ID_PREFIX), serviceObjectType.toString()); SOAPElement xRoadInstance = serviceHeader.addChildElement(Constants.NS_ID_ELEM_XROAD_INSTANCE, Constants.NS_ID_PREFIX); xRoadInstance.addTextNode(message.getProducer().getXRoadInstance()); if (serviceObjectType == ObjectType.SERVICE) { SOAPElement memberClass = serviceHeader.addChildElement(Constants.NS_ID_ELEM_MEMBER_CLASS, Constants.NS_ID_PREFIX); memberClass.addTextNode(message.getProducer().getMemberClass()); SOAPElement memberCode = serviceHeader.addChildElement(Constants.NS_ID_ELEM_MEMBER_CODE, Constants.NS_ID_PREFIX); memberCode.addTextNode(message.getProducer().getMemberCode()); } if (message.getProducer().getSubsystemCode() != null && !message.getProducer().getSubsystemCode().isEmpty()) { SOAPElement subsystem = serviceHeader.addChildElement(Constants.NS_ID_ELEM_SUBSYSTEM_CODE, Constants.NS_ID_PREFIX); subsystem.addTextNode(message.getProducer().getSubsystemCode()); } SOAPElement serviceCode = serviceHeader.addChildElement(Constants.NS_ID_ELEM_SERVICE_CODE, Constants.NS_ID_PREFIX); serviceCode.addTextNode(message.getProducer().getServiceCode()); if (message.getProducer().getServiceVersion() != null && !message.getProducer().getServiceVersion().isEmpty()) { SOAPElement serviceVersion = serviceHeader.addChildElement(Constants.NS_ID_ELEM_SERVICE_VERSION, Constants.NS_ID_PREFIX); serviceVersion.addTextNode(message.getProducer().getServiceVersion()); } LOGGER.debug("\"Service\" element was succesfully generated."); }
public void writeTo(SOAPMessage saaj) throws SOAPException { try { // TODO what about in-scope namespaces // Not very efficient consider implementing a stream buffer // processor that produces a DOM node from the buffer. TransformerFactory tf = XmlUtil.newTransformerFactory(true); Transformer t = tf.newTransformer(); XMLStreamBufferSource source = new XMLStreamBufferSource(_mark); DOMResult result = new DOMResult(); t.transform(source, result); Node d = result.getNode(); if(d.getNodeType() == Node.DOCUMENT_NODE) d = d.getFirstChild(); SOAPHeader header = saaj.getSOAPHeader(); if(header == null) header = saaj.getSOAPPart().getEnvelope().addHeader(); Node node = header.getOwnerDocument().importNode(d, true); header.appendChild(node); } catch (Exception e) { throw new SOAPException(e); } }
public void writeTo(SOAPMessage saaj) throws SOAPException { try { // TODO what about in-scope namespaces // Not very efficient consider implementing a stream buffer // processor that produces a DOM node from the buffer. Transformer t = XmlUtil.newTransformer(); SOAPHeader header = saaj.getSOAPHeader(); if (header == null) header = saaj.getSOAPPart().getEnvelope().addHeader(); // TODO workaround for oracle xdk bug 16555545, when this bug is fixed the line below can be // uncommented and all lines below, except the catch block, can be removed. // t.transform(epr.asSource(localName), new DOMResult(header)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLStreamWriter w = XMLOutputFactory.newFactory().createXMLStreamWriter(baos); epr.writeTo(localName, w); w.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); DocumentBuilderFactory fac = XmlUtil.newDocumentBuilderFactory(false); fac.setNamespaceAware(true); Node eprNode = fac.newDocumentBuilder().parse(bais).getDocumentElement(); Node eprNodeToAdd = header.getOwnerDocument().importNode(eprNode, true); header.appendChild(eprNodeToAdd); } catch (Exception e) { throw new SOAPException(e); } }
protected Map<String,String> addSoapMetaInfo(Map<String, String> metaInfo, SOAPMessage soapMessage) throws SOAPException { SOAPHeader soapHeader = soapMessage.getSOAPHeader(); if (soapHeader == null) { return metaInfo; } else { Map<String, String> newMetaInfo = new HashMap<String, String>(); newMetaInfo.putAll(metaInfo); Iterator<?> iter = soapHeader.examineAllHeaderElements(); while (iter.hasNext()) { SOAPHeaderElement headerElem = (SOAPHeaderElement) iter.next(); if (!Listener.AUTHENTICATED_USER_HEADER.equals(headerElem.getNodeName())) newMetaInfo.put(headerElem.getNodeName(), headerElem.getTextContent()); } return newMetaInfo; } }
@Test public void parseApiVersion_cm_1_8() throws Exception { // given SOAPPart part = mock(SOAPPart.class); SOAPEnvelope envelope = mock(SOAPEnvelope.class); SOAPHeader soapHeader = mock(SOAPHeader.class); List<Node> version = new ArrayList<Node>(); Node node = mock(Node.class); doReturn("testVersion").when(node).getValue(); version.add(node); Iterator<?> it = version.iterator(); doReturn(it).when(soapHeader).extractHeaderElements( eq("cm.service.version")); doReturn(soapHeader).when(envelope).getHeader(); doReturn(envelope).when(part).getEnvelope(); doReturn(part).when(message).getSOAPPart(); // when String result = SoapRequestParser.parseApiVersion(context); // then assertEquals("testVersion", result); }
public SOAPMessage invoke(SOAPMessage request) { try { SOAPHeader headers = request.getSOAPHeader(); if (headers != null) { NodeList nl = headers.getElementsByTagNameNS("http://org.jboss.ws/foo", "returnNullResponse"); if (nl != null && nl.getLength() > 0) { return null; } } } catch (Exception e) { throw new RuntimeException(e); } return request; }
@Test @RunAsClient public void testProviderDispatch() throws Exception { Dispatch<SOAPMessage> dispatch = createDispatch("ProviderEndpoint"); SOAPMessage reqMsg = getRequestMessage(); SOAPMessage resMsg = dispatch.invoke(reqMsg); SOAPEnvelope resEnv = resMsg.getSOAPPart().getEnvelope(); SOAPHeader soapHeader = resEnv.getHeader(); if (soapHeader != null) soapHeader.detachNode(); assertEquals(DOMUtils.parse(msgString), resEnv); }
@Test @RunAsClient public void testProviderMessage() throws Exception { SOAPMessage reqMsg = getRequestMessage(); SOAPEnvelope reqEnv = reqMsg.getSOAPPart().getEnvelope(); URL epURL = baseURL; SOAPConnection con = SOAPConnectionFactory.newInstance().createConnection(); SOAPMessage resMsg = con.call(reqMsg, epURL); SOAPEnvelope resEnv = resMsg.getSOAPPart().getEnvelope(); SOAPHeader soapHeader = resEnv.getHeader(); if (soapHeader != null) soapHeader.detachNode(); assertEquals(reqEnv, resEnv); }
/** * Adds an "Access" element to the SOAP header */ public boolean handleRequest(MessageContext msgct) { if (msgct instanceof SOAPMessageContext) { SOAPMessageContext smsgct = (SOAPMessageContext) msgct; try { SOAPMessage msg = smsgct.getMessage(); SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope(); SOAPHeader header = msg.getSOAPHeader(); if (header == null) header = envelope.addHeader(); // add an header if non exists SOAPHeaderElement accessElement = header.addHeaderElement(envelope.createName("Access", "ns0", JPLAG_TYPES_NS)); SOAPElement usernameelem = accessElement.addChildElement("username"); usernameelem.addTextNode(username); SOAPElement passwordelem = accessElement.addChildElement("password"); passwordelem.addTextNode(password); SOAPElement compatelem = accessElement.addChildElement("compatLevel"); compatelem.addTextNode(compatibilityLevel + ""); } catch (SOAPException x) { System.out.println("Unable to create access SOAP header!"); x.printStackTrace(); } } return true; }
private String getOperationFromSOAPHdr(SOAPHeader soapHeader) { @SuppressWarnings("unchecked") Iterator<SOAPHeaderElement> it = soapHeader.examineAllHeaderElements(); SOAPHeaderElement opHdr = null; while (it.hasNext()) { opHdr = it.next(); if ("operation".equals(opHdr.getLocalName())) { System.out.println("Found operation in SOAP header"); return opHdr.getTextContent(); } } return null; }
private SOAPHeaderElement getSecurityHeader(SOAPEnvelope envelope) throws SOAPException { if (envelope != null) { SOAPHeader header = envelope.getHeader(); if (header != null) { Iterator<?> iter = header.getChildElements(WSSE_QNAME); if (iter.hasNext()) { return (SOAPHeaderElement)iter.next(); } iter = header.getChildElements(WSSE_2_QNAME); if (iter.hasNext()) { return (SOAPHeaderElement)iter.next(); } iter = header.getChildElements(WSSE_11_QNAME); if (iter.hasNext()) { return (SOAPHeaderElement)iter.next(); } } } return null; }
private void copyToSOAPHeader(SOAPHeader soapHeader, Property property) throws IOException, SOAPException { if ((property != null) && (matches(property.getName(), getIncludeRegexes(), new ArrayList<Pattern>()))) { String v = property.getValue().toString(); QName qname = new QName(HEADER_NAMESPACE_PROPAGATION, property.getName()); if (SOAPHeadersType.XML.equals(_soapHeadersType)) { try { Element xmlElement = new ElementPuller().pull(new StringReader(v)); Node xmlNode = soapHeader.getOwnerDocument().importNode(xmlElement, true); soapHeader.appendChild(xmlNode); } catch (Throwable t) { soapHeader.addChildElement(qname).setValue(v); } } else { soapHeader.addChildElement(qname).setValue(v); } } }