Java Code Examples for javax.xml.soap.SOAPElement#setTextContent()

The following examples show how to use javax.xml.soap.SOAPElement#setTextContent() . 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: SOAP.java    From onvif-java-lib with Apache License 2.0 6 votes vote down vote up
protected void createSoapHeader(SOAPMessage soapMessage) throws SOAPException {
	onvifDevice.createNonce();
	String encrypedPassword = onvifDevice.getEncryptedPassword();
	if (encrypedPassword != null && onvifDevice.getUsername() != null) {

		SOAPPart sp = soapMessage.getSOAPPart();
		SOAPEnvelope se = sp.getEnvelope();
		SOAPHeader header = soapMessage.getSOAPHeader();
		se.addNamespaceDeclaration("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
		se.addNamespaceDeclaration("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

		SOAPElement securityElem = header.addChildElement("Security", "wsse");
		// securityElem.setAttribute("SOAP-ENV:mustUnderstand", "1");

		SOAPElement usernameTokenElem = securityElem.addChildElement("UsernameToken", "wsse");

		SOAPElement usernameElem = usernameTokenElem.addChildElement("Username", "wsse");
		usernameElem.setTextContent(onvifDevice.getUsername());

		SOAPElement passwordElem = usernameTokenElem.addChildElement("Password", "wsse");
		passwordElem.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
		passwordElem.setTextContent(encrypedPassword);

		SOAPElement nonceElem = usernameTokenElem.addChildElement("Nonce", "wsse");
		nonceElem.setAttribute("EncodingType", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
		nonceElem.setTextContent(onvifDevice.getEncryptedNonce());

		SOAPElement createdElem = usernameTokenElem.addChildElement("Created", "wsu");
		createdElem.setTextContent(onvifDevice.getLastUTCTime());
	}
}
 
Example 2
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private void generateRelateToElement(SOAPHeader soapHeader, WsAddressingRelatesTo relateTo) throws SOAPException {
   SOAPElement relateToElement = soapHeader.addChildElement(RELATESTO);
   if (relateTo.getRelationshipType() != null && !relateTo.getRelationshipType().isEmpty()) {
      relateToElement.addAttribute(RELATIONSHIPTYPE, relateTo.getRelationshipType());
   }

   if (relateTo.getRelationshipType() != null) {
      relateToElement.setTextContent(relateTo.getReleatesTo().toString());
   }

}
 
Example 3
Source File: JaxWsHandler.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a child text node named childName to the existing header named headerName.
 *
 * @param soapClient the binding provider
 * @param headerName the name of the existing header
 * @param childNamespace the namespace of the new child
 * @param childName the name of the new child
 * @param childValue the value of the new child
 *
 * @throws NullPointerException if no header exists named headerName
 */
public void setHeaderChildString(BindingProvider soapClient, final String headerName,
    String childNamespace, String childName, String childValue) {
  // Find the parent header SOAPElement
  SOAPElement parentHeader = (SOAPElement) getHeader(soapClient, headerName);
  Preconditions.checkNotNull(parentHeader, "No header element found with name: %s", headerName);
  // Add a SOAPElement for the child
  try {
    SOAPElement childElement = parentHeader.addChildElement(new QName(childNamespace, childName));
    childElement.setTextContent(childValue);
  } catch (SOAPException e) {
    throw new ServiceException("Failed to set header for child " + childName, e);
  }
}
 
Example 4
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private void generateRelateToElement(SOAPHeader soapHeader, WsAddressingRelatesTo relateTo) throws SOAPException {
   SOAPElement relateToElement = soapHeader.addChildElement(RELATESTO);
   if (relateTo.getRelationshipType() != null && !relateTo.getRelationshipType().isEmpty()) {
      relateToElement.addAttribute(RELATIONSHIPTYPE, relateTo.getRelationshipType());
   }

   if (relateTo.getRelationshipType() != null) {
      relateToElement.setTextContent(relateTo.getReleatesTo().toString());
   }

}
 
Example 5
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private void generateRelateToElement(SOAPHeader soapHeader, WsAddressingRelatesTo relateTo) throws SOAPException {
   SOAPElement relateToElement = soapHeader.addChildElement(RELATESTO);
   if (relateTo.getRelationshipType() != null && !relateTo.getRelationshipType().isEmpty()) {
      relateToElement.addAttribute(RELATIONSHIPTYPE, relateTo.getRelationshipType());
   }

   if (relateTo.getRelationshipType() != null) {
      relateToElement.setTextContent(relateTo.getReleatesTo().toString());
   }

}
 
Example 6
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private void generateRelateToElement(SOAPHeader soapHeader, WsAddressingRelatesTo relateTo) throws SOAPException {
   SOAPElement relateToElement = soapHeader.addChildElement(RELATESTO);
   if (relateTo.getRelationshipType() != null && !relateTo.getRelationshipType().isEmpty()) {
      relateToElement.addAttribute(RELATIONSHIPTYPE, relateTo.getRelationshipType());
   }

   if (relateTo.getRelationshipType() != null) {
      relateToElement.setTextContent(relateTo.getReleatesTo().toString());
   }

}
 
Example 7
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
private void generateRelateToElement(SOAPHeader soapHeader, WsAddressingRelatesTo relateTo) throws SOAPException {
   SOAPElement relateToElement = soapHeader.addChildElement(RELATESTO);
   if (relateTo.getRelationshipType() != null && !relateTo.getRelationshipType().isEmpty()) {
      relateToElement.addAttribute(RELATIONSHIPTYPE, relateTo.getRelationshipType());
   }

   if (relateTo.getRelationshipType() != null) {
      relateToElement.setTextContent(relateTo.getReleatesTo().toString());
   }

}
 
Example 8
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processRequiredElements(WsAddressingHeader header, SOAPHeader soapHeader) throws SOAPException {
   SOAPElement actionElement = soapHeader.addChildElement(ACTION);
   actionElement.addAttribute(MUST_UNDERSTAND, header.getMustUnderstand());
   actionElement.setTextContent(header.getAction().toString());
}
 
Example 9
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processRequiredElements(WsAddressingHeader header, SOAPHeader soapHeader) throws SOAPException {
   SOAPElement actionElement = soapHeader.addChildElement(ACTION);
   actionElement.addAttribute(MUST_UNDERSTAND, header.getMustUnderstand());
   actionElement.setTextContent(header.getAction().toString());
}
 
Example 10
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processRequiredElements(WsAddressingHeader header, SOAPHeader soapHeader) throws SOAPException {
   SOAPElement actionElement = soapHeader.addChildElement(ACTION);
   actionElement.addAttribute(MUST_UNDERSTAND, header.getMustUnderstand());
   actionElement.setTextContent(header.getAction().toString());
}
 
Example 11
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processRequiredElements(WsAddressingHeader header, SOAPHeader soapHeader) throws SOAPException {
   SOAPElement actionElement = soapHeader.addChildElement(ACTION);
   actionElement.addAttribute(MUST_UNDERSTAND, header.getMustUnderstand());
   actionElement.setTextContent(header.getAction().toString());
}
 
Example 12
Source File: AbstractSoapEncoder.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a SOAPDetail element from SOS exception document.
 *
 * @param detail
 *            SOAPDetail
 * @param exception
 *            SOS Exception document
 *
 * @throws SOAPException
 *             if an error occurs.
 *
 * @deprecated javax.xml.soap.* is no longer supported from 8.0 because it
 *             was removed from Java
 */
@Deprecated
private void createSOAPFaultDetail(Detail detail, CodedException exception) throws SOAPException {
    SOAPElement exRep = detail.addChildElement(OWSConstants.QN_EXCEPTION);
    exRep.addNamespaceDeclaration(OWSConstants.NS_OWS_PREFIX, OWSConstants.NS_OWS);
    String code = exception.getCode()
            .toString();
    String locator = exception.getLocator();
    StringBuilder exceptionText = new StringBuilder();
    exceptionText.append(exception.getMessage());
    exceptionText.append('\n');
    if (exception.getCause() != null) {
        exceptionText.append('\n')
                .append("[EXCEPTION]: ")
                .append('\n');
        if (exception.getCause()
                .getLocalizedMessage() != null
                && !exception.getCause()
                        .getLocalizedMessage()
                        .isEmpty()) {
            exceptionText.append(exception.getCause()
                    .getLocalizedMessage());
            exceptionText.append('\n');
        }
        if (exception.getCause()
                .getMessage() != null
                && !exception.getCause()
                        .getMessage()
                        .isEmpty()) {
            exceptionText.append(exception.getCause()
                    .getMessage());
            exceptionText.append('\n');
        }
    }
    exRep.addAttribute(new QName(OWSConstants.EN_EXCEPTION_CODE), code);
    if (locator != null && !locator.isEmpty()) {
        exRep.addAttribute(new QName(OWSConstants.EN_LOCATOR), locator);
    }
    if (exceptionText.length() != 0) {
        SOAPElement execText = exRep.addChildElement(OWSConstants.QN_EXCEPTION_TEXT);
        execText.setTextContent(exceptionText.toString());
    }
}
 
Example 13
Source File: SimpleSecurityHandler.java    From onvif with Apache License 2.0 4 votes vote down vote up
@Override
public boolean handleMessage(final SOAPMessageContext msgCtx) {
  // System.out.println("SimpleSecurityHandler");

  // Indicator telling us which direction this message is going in
  final Boolean outInd = (Boolean) msgCtx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

  // Handler must only add security headers to outbound messages
  if (outInd.booleanValue()) {
    try {
      // Create the xml
      SOAPMessage soapMessage = msgCtx.getMessage();
      SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();
      SOAPHeader header = envelope.getHeader();
      if (header == null) header = envelope.addHeader();

      SOAPPart sp = soapMessage.getSOAPPart();
      SOAPEnvelope se = sp.getEnvelope();
      se.addNamespaceDeclaration(WSSE_PREFIX, WSSE_NS);
      se.addNamespaceDeclaration(WSU_PREFIX, WSU_NS);

      SOAPElement securityElem = header.addChildElement(WSSE_LN, WSSE_PREFIX);
      // securityElem.setAttribute("SOAP-ENV:mustUnderstand", "1");

      SOAPElement usernameTokenElem =
          securityElem.addChildElement(USERNAME_TOKEN_LN, WSSE_PREFIX);

      SOAPElement usernameElem = usernameTokenElem.addChildElement(USERNAME_LN, WSSE_PREFIX);
      usernameElem.setTextContent(username);

      SOAPElement passwordElem = usernameTokenElem.addChildElement(PASSWORD_LN, WSSE_PREFIX);
      passwordElem.setAttribute(PASSWORD_TYPE_ATTR, PASSWORD_DIGEST);
      passwordElem.setTextContent(encryptPassword(password));

      SOAPElement nonceElem = usernameTokenElem.addChildElement(NONCE_LN, WSSE_PREFIX);
      nonceElem.setAttribute("EncodingType", BASE64_ENCODING);
      nonceElem.setTextContent(Base64.encodeBase64String(nonce.getBytes()));

      SOAPElement createdElem = usernameTokenElem.addChildElement(CREATED_LN, WSU_PREFIX);
      createdElem.setTextContent(getLastUTCTime());
    } catch (final Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  return true;
}
 
Example 14
Source File: HWSoapMessageDocProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
public SOAPMessage invoke(SOAPMessage request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    SOAPMessage response = null;
    SOAPBody body = null;
    try {
        body = SAAJUtils.getBody(request);
    } catch (SOAPException e) {
        return null;
    }
    Node n = body.getFirstChild();

    while (n.getNodeType() != Node.ELEMENT_NODE) {
        n = n.getNextSibling();
    }
    if (n.getLocalName().equals(sayHi.getLocalPart())) {
        response = sayHiResponse;
    } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
        Element el = DOMUtils.getFirstElement(n);
        String v = DOMUtils.getContent(el);
        if (v.contains("Return sayHi")) {
            response = sayHiResponse;
        } else if (v.contains("exceed maxLength")) {
            response = greetMeResponseExceedMaxLengthRestriction;
        } else if (v.contains("throwFault")) {
            try {
                SOAPFactory f = SOAPFactory.newInstance();
                SOAPFault soapFault = f.createFault();

                soapFault.setFaultString("Test Fault String ****");

                Detail detail = soapFault.addDetail();
                detail = soapFault.getDetail();

                QName qName = new QName("http://www.Hello.org/greeter", "TestFault", "ns");
                DetailEntry de = detail.addDetailEntry(qName);

                qName = new QName("http://www.Hello.org/greeter", "ErrorCode", "ns");
                SOAPElement errorElement = de.addChildElement(qName);
                errorElement.setTextContent("errorcode");
                throw new SOAPFaultException(soapFault);
            } catch (SOAPException ex) {
                //ignore
            }

        } else {
            response = greetMeResponse;
        }
    }
    return response;
}
 
Example 15
Source File: HWDOMSourcePayloadProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
public DOMSource invoke(DOMSource request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    DOMSource response = new DOMSource();

    Node n = request.getNode();
    if (n instanceof Document) {
        n = ((Document)n).getDocumentElement();
    }
    if (n.getLocalName().equals(sayHi.getLocalPart())) {
        response.setNode(sayHiResponse);
    } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
        Element el = DOMUtils.getFirstElement(n);
        String s = DOMUtils.getContent(el);
        if ("throwFault".equals(s.trim())) {
            try {
                SOAPFactory f = SOAPFactory.newInstance();
                SOAPFault soapFault = f.createFault();

                soapFault.setFaultString("Test Fault String ****");

                Detail detail = soapFault.addDetail();
                detail = soapFault.getDetail();

                QName qName = new QName("http://www.Hello.org/greeter", "TestFault", "ns");
                DetailEntry de = detail.addDetailEntry(qName);

                qName = new QName("http://www.Hello.org/greeter", "ErrorCode", "ns");
                SOAPElement errorElement = de.addChildElement(qName);
                errorElement.setTextContent("errorcode");

                throw new SOAPFaultException(soapFault);
            } catch (SOAPException e) {
                e.printStackTrace();
            }
        }

        response.setNode(greetMeResponse);
    }
    return response;
}
 
Example 16
Source File: TestMtomProviderImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public SOAPMessage invoke(final SOAPMessage request) {
    try {
        System.out.println("=== Received client request ===");

        // create the SOAPMessage
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPPart part = message.getSOAPPart();
        SOAPEnvelope envelope = part.getEnvelope();
        SOAPBody body = envelope.getBody();


        SOAPBodyElement testResponse = body
            .addBodyElement(envelope.createName("testXopResponse", null, "http://cxf.apache.org/mime/types"));
        SOAPElement name = testResponse.addChildElement("name", null, "http://cxf.apache.org/mime/types");
        name.setTextContent("return detail + call detail");
        SOAPElement attachinfo = testResponse.addChildElement(
                                     "attachinfo", null, "http://cxf.apache.org/mime/types");
        SOAPElement include = attachinfo.addChildElement("Include", "xop",
                                                            "http://www.w3.org/2004/08/xop/include");

        int fileSize = 0;
        try (InputStream pre = this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl")) {
            for (int i = pre.read(); i != -1; i = pre.read()) {
                fileSize++;
            }
        }

        int count = 50;
        byte[] data = new byte[fileSize *  count];
        for (int x = 0; x < count; x++) {
            this.getClass().getResourceAsStream("/wsdl/mtom_xop.wsdl").read(data,
                                                                            fileSize * x,
                                                                            fileSize);
        }


        DataHandler dh = new DataHandler(new ByteArrayDataSource(data, "application/octet-stream"));

        // create the image attachment
        AttachmentPart attachment = message.createAttachmentPart(dh);
        attachment.setContentId("mtom_xop.wsdl");
        message.addAttachmentPart(attachment);
        System.out
            .println("Adding attachment: " + attachment.getContentId() + ":" + attachment.getSize());

        // add the reference to the image attachment
        include.addAttribute(envelope.createName("href"), "cid:" + attachment.getContentId());

        return message;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 17
Source File: WsAddressingHandlerV200508.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private void processRequiredElements(WsAddressingHeader header, SOAPHeader soapHeader) throws SOAPException {
   SOAPElement actionElement = soapHeader.addChildElement(ACTION);
   actionElement.addAttribute(MUST_UNDERSTAND, header.getMustUnderstand());
   actionElement.setTextContent(header.getAction().toString());
}