Ejemplo n.º 1
0
  @Validated
  @Test
  public void testAppendChild() throws Exception {
    MessageFactory fact = MessageFactory.newInstance();
    SOAPMessage message = fact.createMessage();
    SOAPHeader soapHeader = message.getSOAPHeader();

    assertEquals(0, soapHeader.getChildNodes().getLength());
    assertFalse(soapHeader.getChildElements().hasNext());

    Document doc = soapHeader.getOwnerDocument();
    String namespace = "http://example.com";
    String localName = "GetLastTradePrice";
    Element getLastTradePrice = doc.createElementNS(namespace, localName);
    Element symbol = doc.createElement("symbol");
    symbol.setAttribute("foo", "bar");
    getLastTradePrice.appendChild(symbol);
    org.w3c.dom.Text def = doc.createTextNode("DEF");
    symbol.appendChild(def);

    soapHeader.appendChild(getLastTradePrice);

    assertEquals(1, soapHeader.getChildNodes().getLength());
    Iterator iter = soapHeader.getChildElements();
    assertTrue(iter.hasNext());
    Object obj = iter.next();
    // must be SOAPHeaderElement
    assertTrue(obj instanceof SOAPHeaderElement);
    SOAPElement soapElement = (SOAPElement) obj;
    assertEquals(namespace, soapElement.getNamespaceURI());
    assertEquals(localName, soapElement.getLocalName());

    iter = soapElement.getChildElements();
    assertTrue(iter.hasNext());
    obj = iter.next();
    assertTrue(obj instanceof SOAPElement);
    soapElement = (SOAPElement) obj;
    assertEquals(null, soapElement.getNamespaceURI());
    assertEquals("symbol", soapElement.getLocalName());
    assertFalse(iter.hasNext());

    iter = soapElement.getChildElements();
    assertTrue(iter.hasNext());
    obj = iter.next();
    assertTrue(obj instanceof Text);
    Text text = (Text) obj;
    assertEquals("DEF", text.getData());
    assertFalse(iter.hasNext());
  }
  private Map toHeadersMap(SOAPMessage doc) {
    SOAPHeader header = getSOAPHeader(doc);
    if (header == null) {
      return new HashMap(0);
    }

    Map<String, String> headersMap = new HashMap();
    NodeList nodes = header.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
      Node item = nodes.item(i);
      if (item.hasChildNodes()) {
        headersMap.put(item.getLocalName(), item.getFirstChild().getNodeValue());
      }
    }
    return headersMap;
  }
Ejemplo n.º 3
0
  /*
   * ???
   *
   * @param response
   * @param responseBean
   * @throws DOMException
   * @throws SOAPException
   */
  private void extractHeaderDataSOAP(SOAPMessage response, HttpResponseBean responseBean)
      throws SOAPException {
    // extract MimeHeaders
    MimeHeaders mime = response.getMimeHeaders();
    Iterator<MimeHeader> iter = mime.getAllHeaders();

    while (iter.hasNext()) {
      MimeHeader mimeH = iter.next();
      responseBean.addEntryToResponseHeaders(mimeH.getName(), mimeH.getValue());
    }

    // extract SOAPHeaders from the envelope and a them to the mimeHeaders
    if (response.getSOAPHeader() != null) {
      javax.xml.soap.SOAPHeader header = response.getSOAPHeader();

      NodeList nodes = header.getChildNodes();

      for (int x = 0; x < nodes.getLength(); x++) {
        // if the header entry contains child nodes - write them with the node names
        if (nodes.item(x).hasChildNodes()) {
          NodeList childnodes = nodes.item(x).getChildNodes();
          StringBuilder buff = new StringBuilder();
          for (int y = 0; y < childnodes.getLength(); y++) {
            if (!"".equals(buff.toString())) {
              buff.append(" ");
            }
            buff.append(childnodes.item(y).getLocalName()).append(":");
            buff.append(childnodes.item(y).getTextContent());
          }
          responseBean.addEntryToResponseHeaders(nodes.item(x).getLocalName(), buff.toString());
        } else {
          responseBean.addEntryToResponseHeaders(
              nodes.item(x).getLocalName(), nodes.item(x).getTextContent());
        }
      }
    }
  }
Ejemplo n.º 4
0
  /** Returns the security header element. */
  private Element getSecurityHeader(SOAPMessage soapMessage) throws SOAPBindingException {
    try {
      SOAPHeader header = soapMessage.getSOAPPart().getEnvelope().getHeader();
      NodeList headerChildNodes = header.getChildNodes();
      if ((headerChildNodes == null) || (headerChildNodes.getLength() == 0)) {
        throw new SOAPBindingException(WSSUtils.bundle.getString("noSecurityHeader"));
      }
      for (int i = 0; i < headerChildNodes.getLength(); i++) {

        Node currentNode = headerChildNodes.item(i);
        if (currentNode.getNodeType() != Node.ELEMENT_NODE) {
          continue;
        }
        if ((WSSEConstants.TAG_SECURITYT.equals(currentNode.getLocalName()))
            && (WSSEConstants.NS_WSSE_WSF11.equals(currentNode.getNamespaceURI()))) {
          return (Element) currentNode;
        }
      }
      return null;
    } catch (SOAPException se) {
      WSSUtils.debug.error("MessageProcess.getSecurityHeader:: " + "SOAPException", se);
      throw new SOAPBindingException(WSSUtils.bundle.getString("noSecurityHeader"));
    }
  }