Java Code Examples for javax.xml.soap.SOAPEnvelope#addNamespaceDeclaration()

The following examples show how to use javax.xml.soap.SOAPEnvelope#addNamespaceDeclaration() . 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: SoapProtocol.java    From jolie with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void setOutputEncodingStyle( SOAPEnvelope soapEnvelope, String operationName )
	throws IOException, SOAPException {
	Port port = getWSDLPort();
	if( port != null ) {
		BindingOperation bindingOperation = port.getBinding().getBindingOperation( operationName, null, null );
		if( bindingOperation == null ) {
			return;
		}
		BindingOutput output = bindingOperation.getBindingOutput();
		if( output == null ) {
			return;
		}
		for( ExtensibilityElement element : (List< ExtensibilityElement >) output.getExtensibilityElements() ) {
			if( element instanceof javax.wsdl.extensions.soap.SOAPBody ) {
				List< String > list = ((javax.wsdl.extensions.soap.SOAPBody) element).getEncodingStyles();
				if( list != null && list.isEmpty() == false ) {
					soapEnvelope.setEncodingStyle( list.get( 0 ) );
					soapEnvelope.addNamespaceDeclaration( "enc", list.get( 0 ) );
				}
			}
		}
	}
}
 
Example 2
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 3
Source File: Adsv5XTeeServiceImpl.java    From j-road with Apache License 2.0 6 votes vote down vote up
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
  callback.doWithMessage(message);
  try {
    SaajSoapMessage saajMessage = (SaajSoapMessage) message;
    SOAPMessage soapmess = saajMessage.getSaajMessage();
    SOAPEnvelope env = soapmess.getSOAPPart().getEnvelope();
    env.addNamespaceDeclaration("xro", "http://x-road.ee/xsd/x-road.xsd");
    Iterator headers = env.getHeader().getChildElements();
    while (headers.hasNext()) {
      SOAPElement header = (SOAPElement) headers.next();
      if (header.getNamespaceURI().equalsIgnoreCase("http://x-rd.net/xsd/xroad.xsd")) {
        String localHeaderName = header.getLocalName();
        QName qName = new QName("http://x-road.ee/xsd/x-road.xsd", localHeaderName, "xro");
        header.setElementQName(qName);
      }
    }
  } catch (SOAPException e) {
    throw new RuntimeException(e);
  }

}
 
Example 4
Source File: TorXTeeServiceImpl.java    From j-road with Apache License 2.0 6 votes vote down vote up
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
  callback.doWithMessage(message);
  try {
    SaajSoapMessage saajMessage = (SaajSoapMessage) message;
    SOAPMessage soapmess = saajMessage.getSaajMessage();
    SOAPEnvelope env = soapmess.getSOAPPart().getEnvelope();
    env.addNamespaceDeclaration("xro", "http://x-road.ee/xsd/x-road.xsd");
    Iterator headers = env.getHeader().getChildElements();
    while (headers.hasNext()) {
      SOAPElement header = (SOAPElement) headers.next();
      if (header.getNamespaceURI().equalsIgnoreCase("http://x-rd.net/xsd/xroad.xsd")) {
        String localHeaderName = header.getLocalName();
        QName qName = new QName("http://x-road.ee/xsd/x-road.xsd", localHeaderName, "xro");
        header.setElementQName(qName);
      }
    }
  } catch (SOAPException e) {
    throw new RuntimeException(e);
  }

}
 
Example 5
Source File: SignCode.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private static SOAPBody populateEnvelope(SOAPMessage message, String namespace)
        throws SOAPException {
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration(
            "soapenv","http://schemas.xmlsoap.org/soap/envelope/");
    envelope.addNamespaceDeclaration(
            namespace,"http://api.ws.symantec.com/webtrust/codesigningservice");
    return envelope.getBody();
}
 
Example 6
Source File: Soap11Encoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private void addSchemaLocationForExceptionToSOAPMessage(SOAPMessage soapResponseMessage) throws SOAPException {
    SOAPEnvelope envelope = soapResponseMessage.getSOAPPart().getEnvelope();
    envelope.addNamespaceDeclaration(W3CConstants.NS_XSI_PREFIX, W3CConstants.NS_XSI);
    StringBuilder schemaLocation = new StringBuilder();
    schemaLocation.append(envelope.getNamespaceURI());
    schemaLocation.append(" ");
    schemaLocation.append(envelope.getNamespaceURI());
    schemaLocation.append(" ");
    schemaLocation.append(N52XmlHelper.getSchemaLocationForOWS110Exception());
    envelope.addAttribute(N52XmlHelper.getSchemaLocationQNameWithPrefix(), schemaLocation.toString());
}
 
Example 7
Source File: SignCodeMojo.java    From sling-whiteboard with Apache License 2.0 5 votes vote down vote up
private static SOAPBody populateEnvelope(SOAPMessage message, String namespace)
        throws SOAPException {
    SOAPPart soapPart = message.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration(
            "soapenv","http://schemas.xmlsoap.org/soap/envelope/");
    envelope.addNamespaceDeclaration(
            namespace,"http://api.ws.symantec.com/webtrust/codesigningservice");
    return envelope.getBody();
}
 
Example 8
Source File: TarnXTeeServiceImpl.java    From j-road with Apache License 2.0 5 votes vote down vote up
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
  try {
    SaajSoapMessage saajMessage = (SaajSoapMessage) message;
    SOAPMessage soapmess = saajMessage.getSaajMessage();
    SOAPEnvelope env = soapmess.getSOAPPart().getEnvelope();
    env.addNamespaceDeclaration("eto", "http://producers.etoimik.xtee.riik.ee/producer/etoimik");
  } catch (SOAPException e) {
    throw new RuntimeException(e);
  }
  callback.doWithMessage(message);
}
 
Example 9
Source File: XRoadProtocolNamespaceStrategyV4.java    From j-road with Apache License 2.0 5 votes vote down vote up
@Override
public void addNamespaces(SOAPEnvelope env) throws SOAPException {
  env.addNamespaceDeclaration("xsd", "http://www.w3.org/2001/XMLSchema");
  env.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
  env.addNamespaceDeclaration(protocol.getNamespacePrefix(), protocol.getNamespaceUri());
  env.addNamespaceDeclaration("id", "http://x-road.eu/xsd/identifiers");
}
 
Example 10
Source File: SOAPGenerator.java    From cougar with Apache License 2.0 5 votes vote down vote up
private static SOAPMessage generateSOAPMessageShell(HttpCallBean httpCallBean) {
	SOAPMessage message = null;
	String secNameSpace = "http://www.betfair.com/security/";
	httpCallBean.setServiceName("Baseline");
	String nameSpace = httpCallBean.getNameSpace();
	
	if(nameSpace==null || nameSpace.equals(""))
	{
		throw new RuntimeException("Namespace error invalid :" + nameSpace);
	}
	
	MessageFactory mf;

	try {
		mf = MessageFactory.newInstance();

		message = mf.createMessage();

		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();

		envelope.addNamespaceDeclaration("soapenv",
				"http://schemas.xmlsoap.org/soap/envelope/");
		envelope.addNamespaceDeclaration(BAS, nameSpace);

		envelope.addNamespaceDeclaration("sec", secNameSpace);

		// header
		envelope.getHeader().detachNode();
		SOAPHeader soapHeader = envelope.addHeader();

		Name header = envelope.createName("Header", "soapenv",
				"http://schemas.xmlsoap.org/soap/envelope/");

		soapHeader.addHeaderElement(header);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return message;
}
 
Example 11
Source File: Tr064Comm.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Sets all required namespaces and prepares the SOAP message to send.
 * Creates skeleton + body data.
 *
 * @param bodyData
 *            is attached to skeleton to form entire SOAP message
 * @return ready to send SOAP message
 */
private SOAPMessage constructTr064Msg(SOAPBodyElement bodyData) {
    SOAPMessage soapMsg = null;

    try {
        MessageFactory msgFac;
        msgFac = MessageFactory.newInstance();
        soapMsg = msgFac.createMessage();
        soapMsg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
        soapMsg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "UTF-8");
        SOAPPart part = soapMsg.getSOAPPart();

        // valid for entire SOAP msg
        String namespace = "s";

        // create suitable fbox envelope
        SOAPEnvelope envelope = part.getEnvelope();
        envelope.setPrefix(namespace);
        envelope.removeNamespaceDeclaration("SOAP-ENV"); // delete standard namespace which was already set
        envelope.addNamespaceDeclaration(namespace, "http://schemas.xmlsoap.org/soap/envelope/");
        Name nEncoding = envelope.createName("encodingStyle", namespace,
                "http://schemas.xmlsoap.org/soap/encoding/");
        envelope.addAttribute(nEncoding, "http://schemas.xmlsoap.org/soap/encoding/");

        // create empty header
        SOAPHeader header = envelope.getHeader();
        header.setPrefix(namespace);

        // create body with command based on parameter
        SOAPBody body = envelope.getBody();
        body.setPrefix(namespace);
        body.addChildElement(bodyData); // bodyData already prepared. Needs only be added

    } catch (Exception e) {
        logger.error("Error creating SOAP message for fbox request with data {}", bodyData);
        e.printStackTrace();
    }

    return soapMsg;
}
 
Example 12
Source File: SendMTConverterUtils.java    From opencps-v2 with GNU Affero General Public License v3.0 4 votes vote down vote up
private static SOAPMessage convertSendMTToXML(SendMT sendMT) {
	try {
		JAXBContext jaxbContext = JAXBContext.newInstance(SendMT.class);
		MessageFactory mf = MessageFactory.newInstance();
		SOAPMessage message = mf.createMessage();

		SOAPBody body = message.getSOAPBody();

		SOAPHeader soapheader = message.getSOAPHeader();
		soapheader.detachNode();

		Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
		// output pretty printed
		jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		//process marshaller
		jaxbMarshaller.marshal(sendMT, body);
		//jaxbMarshaller.marshal(sendMT, System.out);
		message.saveChanges();

		// Process convert SOAP-ENV to soapenv
		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();
		// SOAPHeader header = message.getSOAPHeader();
		SOAPBody bodyConvert = message.getSOAPBody();
		SOAPFault fault = bodyConvert.getFault();
		envelope.removeNamespaceDeclaration(envelope.getPrefix());
		envelope.addNamespaceDeclaration(Constants.PREFERRED_PREFIX, Constants.SOAP_ENV_NAMESPACE);
		envelope.addNamespaceDeclaration(Constants.PREFERRED_PREFIX_TEM, Constants.SOAP_ENV_NAMESPACE_TEM);
		envelope.setPrefix(Constants.PREFERRED_PREFIX);
		bodyConvert.setPrefix(Constants.PREFERRED_PREFIX);
		if (fault != null) {
			fault.setPrefix(Constants.PREFERRED_PREFIX);
		}
		message.saveChanges();

		return message;
	} catch (Exception e) {
		_log.error(e);
	}

	return null;
}
 
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: SOAPRequestBuilder.java    From cougar with Apache License 2.0 4 votes vote down vote up
public SOAPMessage buildSOAPRequest(Document document,
		HttpCallBean httpCallBean) {

	nameSpace = httpCallBean.getNameSpace();

	if (nameSpace == null || nameSpace.equals("")) {
		throw new RuntimeException(
				"Cannot create SOAP message using the following name space : "
						+ nameSpace);
	}

	// Construct SOAPMessage
	MessageFactory mf;
	try {
		mf = MessageFactory.newInstance();

		SOAPMessage message = mf.createMessage();
		
		//add headers here
		
		MimeHeaders hd = message.getMimeHeaders();
		hd.addHeader("X-Forwarded-For", httpCallBean.getIpAddress());
		
		//SCR: 103 Trace Me testing
		//hd.addHeader("X-Trace-Me", "true");
		
		//
		if(httpCallBean.getHeaderParams()!=null)
		{
		for (String key: httpCallBean.getHeaderParams().keySet())
		{
			hd.addHeader(key, httpCallBean.getHeaderParams().get(key));
		}
		}

		SOAPPart soapPart = message.getSOAPPart();
		SOAPEnvelope envelope = soapPart.getEnvelope();

		envelope.addNamespaceDeclaration("bas", nameSpace);

		// generate header
		generateSoapHeader(envelope, httpCallBean);

		// generate body
		generateSOAPBody(envelope, document);

		message.saveChanges();

		// TODO write this to the logs
		
		//Uncomment for debug

		/*try
		{
		System.out.println("\n Soap Request:\n");
		message.writeTo(System.out);
		System.out.println();
		}
		catch (IOException e) {
			throw new UtilityException(e);
		}*/
		

		return message;

	} catch (SOAPException e) {
		throw new RuntimeException(e);
	} 
}