Java Code Examples for javax.xml.soap.SOAPHeader#addHeaderElement()

The following examples show how to use javax.xml.soap.SOAPHeader#addHeaderElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JPlagClientAccessHandler.java    From jplag with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: SoapPayloadIOTest.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for {@link io.apiman.gateway.engine.io.SoapPayloadIO#marshall(org.w3c.dom.Document)}.
 */
@Test
public void testMarshall_Simple() throws Exception {
    MessageFactory msgFactory = MessageFactory.newInstance();
    SOAPMessage message = msgFactory.createMessage();
    SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();

    SOAPHeader header = envelope.getHeader();
    SOAPHeaderElement cheader = header.addHeaderElement(new QName("urn:ns1", "CustomHeader"));
    cheader.setTextContent("CVALUE");

    SoapPayloadIO io = new SoapPayloadIO();
    byte[] data = io.marshall(envelope);
    String actual = new String(data);

    String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header><CustomHeader xmlns=\"urn:ns1\">CVALUE</CustomHeader></SOAP-ENV:Header><SOAP-ENV:Body/></SOAP-ENV:Envelope>";
    Assert.assertEquals(expected, actual);
}
 
Example 3
Source File: RelatesToHeader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if (header == null)
        header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(name);

    if (type != null)
        she.addAttribute(typeAttributeName, type);
    she.addTextNode(value);
}
 
Example 4
Source File: SecureSoapMessages.java    From spring-ws-security-soap-example with MIT License 5 votes vote down vote up
private static final SOAPMessage getMessageToSign(final String pathBase)
        throws SOAPException, IOException {
    final SOAPMessage soapMessage;
    final SOAPPart soapPart;
    final SOAPEnvelope soapEnvelope;
    final SOAPHeader soapHeader;
    final SOAPHeaderElement secElement;
    final SOAPElement binaryTokenElement;

    soapMessage = SoapMessageUtils.getMessage(pathBase);
    soapPart = soapMessage.getSOAPPart();
    soapEnvelope = soapPart.getEnvelope();
    soapHeader = soapEnvelope.getHeader();

    secElement = soapHeader.addHeaderElement(soapEnvelope.createName(
            "Security", "wsse",
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"));
    binaryTokenElement = secElement.addChildElement(soapEnvelope.createName(
            "BinarySecurityToken", "wsse",
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"));
    binaryTokenElement.setAttribute("EncodingType",
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
    binaryTokenElement.setAttribute("ValueType",
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3");

    return soapMessage;
}
 
Example 5
Source File: VersionHandler.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * The method is invoked for normal processing of outbound messages.
 * 
 * @param context
 *            the message context.
 * @return An indication of whether handler processing should continue for
 *         the current message. Return <code>true</code> to continue
 *         processing.
 * 
 * @throws Exception
 *             Causes the JAX-WS runtime to cease fault message processing.
 **/
@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean request_p = (Boolean) context
            .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (request_p.booleanValue()) {
        try {
            SOAPMessage msg = context.getMessage();
            SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
            SOAPHeader hdr = env.getHeader();
            if (hdr == null) {
                hdr = env.addHeader();
            }
            QName qname_user = new QName("http://com/auth/", "auth");
            SOAPHeaderElement helem_user = hdr.addHeaderElement(qname_user);
            helem_user.setActor(VERSION);
            if (version == null || version.trim().length() == 0) {
                helem_user.addTextNode(apiVersionInfo.getProperty(VERSION));
            } else {
                helem_user.addTextNode(version);
            }

            msg.saveChanges();
            message = soapMessage2String(msg);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}
 
Example 6
Source File: StringHeader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if(header == null)
        header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(name);
    if(mustUnderstand) {
        she.setMustUnderstand(true);
    }
    she.addTextNode(value);
}
 
Example 7
Source File: FaultDetailHeader.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if (header == null)
            header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(av.faultDetailTag);
    she = header.addHeaderElement(new QName(av.nsUri, wrapper));
    she.addTextNode(problemValue);
}
 
Example 8
Source File: ProblemActionHeader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if(header == null)
        header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(new QName(getNamespaceURI(), getLocalPart()));
    she.addChildElement(actionLocalName);
    she.addTextNode(action);
    if (soapAction != null) {
        she.addChildElement(soapActionLocalName);
        she.addTextNode(soapAction);
    }
}
 
Example 9
Source File: FaultDetailHeader.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if (header == null)
            header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(av.faultDetailTag);
    she = header.addHeaderElement(new QName(av.nsUri, wrapper));
    she.addTextNode(problemValue);
}
 
Example 10
Source File: FaultDetailHeader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if (header == null)
            header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(av.faultDetailTag);
    she = header.addHeaderElement(new QName(av.nsUri, wrapper));
    she.addTextNode(problemValue);
}
 
Example 11
Source File: ProblemActionHeader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if(header == null)
        header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(new QName(getNamespaceURI(), getLocalPart()));
    she.addChildElement(actionLocalName);
    she.addTextNode(action);
    if (soapAction != null) {
        she.addChildElement(soapActionLocalName);
        she.addTextNode(soapAction);
    }
}
 
Example 12
Source File: FaultDetailHeader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if (header == null)
            header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(av.faultDetailTag);
    she = header.addHeaderElement(new QName(av.nsUri, wrapper));
    she.addTextNode(problemValue);
}
 
Example 13
Source File: StringHeader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if(header == null)
        header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(name);
    if(mustUnderstand) {
        she.setMustUnderstand(true);
    }
    she.addTextNode(value);
}
 
Example 14
Source File: TestMustUnderstandHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
public boolean handleMessage(SOAPMessageContext ctx) {

        boolean continueProcessing = true;

        try {
            Object b = ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
            boolean outbound = (Boolean)b;
            SOAPMessage msg = ctx.getMessage();
            if (isServerSideHandler()) {
                if (outbound) {
                    QName qname = new QName("http://cxf.apache.org/mu", "MU");
                    SOAPPart soapPart = msg.getSOAPPart();
                    SOAPEnvelope envelope = soapPart.getEnvelope();
                    SOAPHeader header = envelope.getHeader();
                    if (header == null) {
                        header = envelope.addHeader();
                    }


                    SOAPHeaderElement headerElement
                        = header.addHeaderElement(envelope.createName("MU", "ns1", qname.getNamespaceURI()));

                    // QName soapMustUnderstand = new QName("http://schemas.xmlsoap.org/soap/envelope/",
                    // "mustUnderstand");
                    Name name = SOAPFactory.newInstance()
                        .createName("mustUnderstand", "soap", "http://schemas.xmlsoap.org/soap/envelope/");
                    headerElement.addAttribute(name, "1");
                } else {
                    getHandlerInfoList(ctx).add(getHandlerId());
                }
            }
        } catch (SOAPException e) {
            e.printStackTrace();
        }
        return continueProcessing;
    }
 
Example 15
Source File: VersionHandlerCtmg.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * The method is invoked for normal processing of outbound messages.
 * 
 * @param context
 *            the message context.
 * @return An indication of whether handler processing should continue for
 *         the current message. Return <code>true</code> to continue
 *         processing.
 * 
 * @throws Exception
 *             Causes the JAX-WS runtime to cease fault message processing.
 **/
@Override
public boolean handleMessage(SOAPMessageContext context) {
    Boolean request_p = (Boolean) context
            .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (request_p.booleanValue()) {
        try {
            SOAPMessage msg = context.getMessage();
            SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
            SOAPHeader hdr = env.getHeader();
            if (hdr == null) {
                hdr = env.addHeader();
            }
            QName qname_user = new QName("http://com/auth/", "auth");
            SOAPHeaderElement helem_user = hdr.addHeaderElement(qname_user);
            helem_user.setActor(VERSION);
            if (version != null && version.trim().length() != 0) {
                helem_user.addTextNode(version);
            }

            msg.saveChanges();
            message = soapMessage2String(msg);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return true;
}
 
Example 16
Source File: FaultDetailHeader.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if (header == null)
            header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(av.faultDetailTag);
    she = header.addHeaderElement(new QName(av.nsUri, wrapper));
    she.addTextNode(problemValue);
}
 
Example 17
Source File: StringHeader.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if(header == null)
        header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(name);
    if(mustUnderstand) {
        she.setMustUnderstand(true);
    }
    she.addTextNode(value);
}
 
Example 18
Source File: FaultDetailHeader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void writeTo(SOAPMessage saaj) throws SOAPException {
    SOAPHeader header = saaj.getSOAPHeader();
    if (header == null)
            header = saaj.getSOAPPart().getEnvelope().addHeader();
    SOAPHeaderElement she = header.addHeaderElement(av.faultDetailTag);
    she = header.addHeaderElement(new QName(av.nsUri, wrapper));
    she.addTextNode(problemValue);
}
 
Example 19
Source File: JPlagClientAccessHandler.java    From jplag with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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",
							"http://www.ipd.uni-karlsruhe.de/jplag/types"));
			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;
}
 
Example 20
Source File: SAAJTestServlet.java    From appengine-java-vm-runtime with Apache License 2.0 4 votes vote down vote up
/**
 * The main goal of this test is to exercise the SAAJ classes and make sure
 * that no exceptions are thrown. We build a SOAP message, we send it to a
 * mock SOAP server, which simply echoes the message back to us, and then we
 * check to make sure that the returned message is the same as the sent
 * message. We perform that check for equality in a very shallow way because
 * we are not seriously concerned about the possibility that the SOAP message
 * might be garbled. The check for equality should be thought of as only a
 * sanity check.
 */
private void testSAAJ(String protocol) throws Exception {
  // Create the message
  MessageFactory factory = MessageFactory.newInstance(protocol);
  SOAPMessage requestMessage = factory.createMessage();

  // Add a header
  SOAPHeader header = requestMessage.getSOAPHeader();
  QName headerName = new QName("http://ws-i.org/schemas/conformanceClaim/", "Claim", "wsi");
  SOAPHeaderElement headerElement = header.addHeaderElement(headerName);
  headerElement.addAttribute(new QName("conformsTo"), "http://ws-i.org/profiles/basic/1.1/");

  // Add a body
  QName bodyName = new QName("http://wombat.ztrade.com", "GetLastTradePrice", "m");
  SOAPBody body = requestMessage.getSOAPBody();
  SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
  QName name = new QName("symbol");
  SOAPElement symbol = bodyElement.addChildElement(name);
  symbol.addTextNode("SUNW");

  // Add an attachment
  AttachmentPart attachment = requestMessage.createAttachmentPart();
  String stringContent =
      "Update address for Sunny Skies " + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";
  attachment.setContent(stringContent, "text/plain");
  attachment.setContentId("update_address");
  requestMessage.addAttachmentPart(attachment);

  // Add another attachment
  URL url = new URL("http://greatproducts.com/gizmos/img.jpg");
  // URL url = new URL("file:///etc/passwords");
  DataHandler dataHandler = new DataHandler(url);
  AttachmentPart attachment2 = requestMessage.createAttachmentPart(dataHandler);
  attachment2.setContentId("attached_image");
  requestMessage.addAttachmentPart(attachment2);

  // Send the message
  SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
  SOAPConnection connection = soapConnectionFactory.createConnection();
  URL endpoint = new URL("http://wombat.ztrade.com/quotes");

  // Get the response. Our mock url-fetch handler will echo back the request
  SOAPMessage responseMessage = connection.call(requestMessage, endpoint);
  connection.close();

  assertEquals(requestMessage, responseMessage);
}