Beispiel #1
0
  @Validated
  @Test
  public void testExamineHeader() {
    SOAPHeaderElement she = null;

    try {
      she1 = hdr.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI"));
      she1.setActor("actor-URI");
      Iterator iterator = hdr.examineAllHeaderElements();
      int cnt = 0;
      while (iterator.hasNext()) {
        cnt++;
        she = (SOAPHeaderElement) iterator.next();
        if (!she.equals(she1)) {
          fail("SOAP Header Elements do not match");
        }
      }

      if (cnt != 1) {
        fail("SOAPHeaderElement count mismatch: expected 1, received " + cnt);
      }

      iterator = hdr.examineAllHeaderElements();
      if (!iterator.hasNext()) {
        fail("no elements in iterator - unexpected");
      }

    } catch (Exception e) {
      fail("Unexpected Exception: " + e);
    }
  }
  @Override
  public boolean handleMessage(SOAPMessageContext context) {

    System.out.println("Client : handleMessage()......");

    Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    // if this is a request, true for outbound messages, false for inbound
    if (isRequest) {

      try {
        SOAPMessage soapMsg = context.getMessage();
        SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
        SOAPHeader soapHeader = soapEnv.getHeader();

        // if no header, add one
        if (soapHeader == null) {
          soapHeader = soapEnv.addHeader();
        }

        // add a soap header, name as "mac address"
        QName qname = new QName("http://service.coupon.ronx.com/", "couponList");
        SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);

        soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
        //                soapHeaderElement.addTextNode(mac);
        soapMsg.saveChanges();

        // tracking
        soapMsg.writeTo(System.out);

      } catch (SOAPException | IOException e) {
        System.err.println(e);
      }
    }

    // continue other handler chain
    return true;
  }
Beispiel #3
0
  @Validated
  @Test
  public void testHeaders() {
    try {
      // Create message factory and SOAP factory
      MessageFactory messageFactory = MessageFactory.newInstance();
      SOAPFactory soapFactory = SOAPFactory.newInstance();

      // Create a message
      SOAPMessage message = messageFactory.createMessage();

      // Get the SOAP header from the message and
      //  add headers to it
      SOAPHeader header = message.getSOAPHeader();

      String nameSpace = "ns";
      String nameSpaceURI = "http://gizmos.com/NSURI";

      Name order = soapFactory.createName("orderDesk", nameSpace, nameSpaceURI);
      SOAPHeaderElement orderHeader = header.addHeaderElement(order);
      orderHeader.setActor("http://gizmos.com/orders");

      Name shipping = soapFactory.createName("shippingDesk", nameSpace, nameSpaceURI);
      SOAPHeaderElement shippingHeader = header.addHeaderElement(shipping);
      shippingHeader.setActor("http://gizmos.com/shipping");

      Name confirmation = soapFactory.createName("confirmationDesk", nameSpace, nameSpaceURI);
      SOAPHeaderElement confirmationHeader = header.addHeaderElement(confirmation);
      confirmationHeader.setActor("http://gizmos.com/confirmations");

      Name billing = soapFactory.createName("billingDesk", nameSpace, nameSpaceURI);
      SOAPHeaderElement billingHeader = header.addHeaderElement(billing);
      billingHeader.setActor("http://gizmos.com/billing");

      // Add header with mustUnderstand attribute
      Name tName = soapFactory.createName("Transaction", "t", "http://gizmos.com/orders");

      SOAPHeaderElement transaction = header.addHeaderElement(tName);
      transaction.setMustUnderstand(true);
      transaction.addTextNode("5");

      // Get the SOAP body from the message but leave
      // it empty
      SOAPBody body = message.getSOAPBody();

      message.saveChanges();

      // Display the message that would be sent
      // System.out.println("\n----- Request Message ----\n");
      // message.writeTo(System.out);

      // Look at the headers
      Iterator allHeaders = header.examineAllHeaderElements();

      while (allHeaders.hasNext()) {
        SOAPHeaderElement headerElement = (SOAPHeaderElement) allHeaders.next();
        Name headerName = headerElement.getElementName();
        // System.out.println("\nHeader name is " +
        //                   headerName.getQualifiedName());
        // System.out.println("Actor is " + headerElement.getActor());
        // System.out.println("mustUnderstand is " +
        //                   headerElement.getMustUnderstand());
      }
    } catch (Exception e) {
      fail("Enexpected Exception " + e);
    }
  }