當前位置: 首頁>>代碼示例>>Java>>正文


Java SOAPBody.addNamespaceDeclaration方法代碼示例

本文整理匯總了Java中javax.xml.soap.SOAPBody.addNamespaceDeclaration方法的典型用法代碼示例。如果您正苦於以下問題:Java SOAPBody.addNamespaceDeclaration方法的具體用法?Java SOAPBody.addNamespaceDeclaration怎麽用?Java SOAPBody.addNamespaceDeclaration使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.xml.soap.SOAPBody的用法示例。


在下文中一共展示了SOAPBody.addNamespaceDeclaration方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testSOAPMessage

import javax.xml.soap.SOAPBody; //導入方法依賴的package包/類
/**
 * Example of soap messaging in RestAssured.
 * @throws Exception
 */
@Test
public void testSOAPMessage() throws Exception {
    /*
    Construct SOAP Request Message instead of hardcoding as a string which is error prone:
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Header/>
        <SOAP-ENV:Body xmlns:ns2="http://namespace2.com">
            <n2:GetVendor>
                <n2:id>1290</n2:id>
            </n2:GetVendor>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
     */
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    SOAPEnvelope envelope = soapPart.getEnvelope();
    SOAPBody soapBody = envelope.getBody();

    // Add namespace declarations
    soapBody.addNamespaceDeclaration("ns2", "http://namespace2.com");

    SOAPElement vendorElement = soapBody.addChildElement("Vendor", "ns2");
    vendorElement.addChildElement("id", "ns2").addTextNode("1290");
    soapMessage.saveChanges();

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    soapMessage.writeTo(out);
    String soapMessageInText = new String(out.toByteArray());

    /* Response stub returned from WireMock
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
        <SOAP-ENV:Header/>
        <SOAP-ENV:Body xmlns:ns2="http://namespace2.com">
            <ns2:Vendor>
                <ns2:id>1290</ns2:id>
                <ns2:name>John Smith</ns2:name>
                <ns2:phone>650-222-3333</ns2:phone>
            </ns2:Vendor>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
     */

    //@formatter:off
    response = given().
            config(RestAssuredConfig.newConfig().
                    xmlConfig(XmlConfig.xmlConfig().
                            namespaceAware(true).
                            declareNamespace("ns2", "http://namespace2.com"))).
            spec(requestSpec).
            header("SOAPAction", "http://localhost/getVendor").
            contentType("application/soap+xml; charset=UTF-8;").
            body(soapMessageInText).
    when().
            post("/v1/vendors").
    then().assertThat().
            contentType(ContentType.XML).
            statusCode(200).
            extract().response();
    //@formatter:on

    // One way to assert
    String responseString = response.asString();
    String vendorName = new XmlPath(responseString).getString("Envelope.Body.Vendor.name");
    assertThat(vendorName, is("John Smith"));

    // Another way to assert
    vendorName = XmlPath.with(responseString).get("Envelope.Body.Vendor.name");
    assertThat(vendorName, is("John Smith"));
}
 
開發者ID:eing,項目名稱:restassured_cli,代碼行數:75,代碼來源:SampleComponentTest.java

示例2: testAddChildElement

import javax.xml.soap.SOAPBody; //導入方法依賴的package包/類
@Validated @Test
public void testAddChildElement() throws Exception {
    String s = "MyName1";
    String p = "MyPrefix1";
    String u = "myURI";
    SOAPBody body = MessageFactory.newInstance().createMessage().getSOAPBody();
    SOAPElement myse = body.addNamespaceDeclaration(p, u);
    SOAPElement se = body.addChildElement(s, p);
    if (se == null) {
        fail("SOAPElement was null");
    } else {
        Iterator i = body.getChildElements();
        int count = getIteratorCount(i);
        i = body.getChildElements();
        if (count != 1) {
            fail("Wrong iterator count returned of " + count + ", expected 1");
        } else {
            SOAPElement se2 = (SOAPElement)i.next();
            if (!se.equals(se2)) {
                fail("Elements not equal");
            }
        }
        String name = se.getElementName().getLocalName();
        Name n = se.getElementName();
        String prefix = se.getElementName().getPrefix();
        if (!name.equals(s) || !prefix.equals(p)) {
            fail("addChildElement() did not return correct local name and prefix");
        }
    }
}
 
開發者ID:wso2,項目名稱:wso2-axis2,代碼行數:31,代碼來源:SOAPElementTest.java

示例3: testRemoveNamespaceDeclaration

import javax.xml.soap.SOAPBody; //導入方法依賴的package包/類
@Validated @Test
public void testRemoveNamespaceDeclaration() throws Exception {
    String prefix = "myPrefix";
    String uri = "myURI";
    SOAPMessage msg = MessageFactory.newInstance().createMessage();
    SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
    SOAPBody body = envelope.getBody();
    body.addNamespaceDeclaration(prefix, uri);
    boolean b = body.removeNamespaceDeclaration(prefix);
    assertTrue("removeNamespaceDeclaration() did not return true", b);
    b = body.removeNamespaceDeclaration(prefix);
    assertFalse("removeNamespaceDeclaration() did not return false", b);
    assertNull(body.getNamespaceURI(prefix));
}
 
開發者ID:wso2,項目名稱:wso2-axis2,代碼行數:15,代碼來源:SOAPElementTest.java

示例4: launchMobyAsyncService

import javax.xml.soap.SOAPBody; //導入方法依賴的package包/類
/**
 * This method is used to launch an asynchronous MOBY job.
 *
 * @param endpoint
 *            The endpoint of the service.
 * @param msName
 *            The MOBY service name.
 * @param mobyXML
 *            The MOBY payload to be sent to the service.
 * @return The EndpointReference object which helds the details of the MOBY
 *         asynchronous job.
 * @throws MobyException
 */
private static EndpointReference launchMobyAsyncService(String endpoint,
		String msName, String mobyXML) throws MobyException {
	try {
		Service service = Service.create(new QName(
				MobyService.BIOMOBY_SERVICE_URI, msName + "Service"));
		QName mQName = new QName(MobyService.BIOMOBY_SERVICE_URI, msName
				+ "Port");

		service.addPort(mQName, SOAPBinding.SOAP11HTTP_BINDING, endpoint);
		Dispatch<SOAPMessage> dispatch = service.createDispatch(mQName,
				SOAPMessage.class, Service.Mode.MESSAGE);
		Map<String, Object> rc = dispatch.getRequestContext();
		rc.put(BindingProvider.SOAPACTION_USE_PROPERTY, new Boolean(true));
		rc.put(BindingProvider.SOAPACTION_URI_PROPERTY,
				MobyService.BIOMOBY_SERVICE_URI + "#" + msName + "_submit");

		MessageFactory mf = MessageFactory.newInstance();
		SOAPMessage request = mf.createMessage();
		SOAPPart part = request.getSOAPPart();

		String mobyPrefix = "mobyws";
		String xsiPrefix = "xsi";

		// Obtain the SOAPEnvelope and header and body elements.
		SOAPEnvelope env = part.getEnvelope();
		SOAPBody body = env.getBody();

		// Now the SOAP body
		body.addNamespaceDeclaration(mobyPrefix,
				MobyService.BIOMOBY_SERVICE_URI);
		SOAPElement rootMessage = body.addChildElement(msName + "_submit",
				mobyPrefix, MobyService.BIOMOBY_SERVICE_URI);
		SOAPElement data = rootMessage.addChildElement("data", mobyPrefix,
				MobyService.BIOMOBY_SERVICE_URI);
		data.addNamespaceDeclaration(xsiPrefix,
				MobyPrefixResolver.XSI_NAMESPACE2001);
		data.addNamespaceDeclaration("xsd", XSD_NS);
		data.addAttribute(env.createName("type", xsiPrefix,
				MobyPrefixResolver.XSI_NAMESPACE2001), "xsd:string");
		data.addTextNode(mobyXML);

		request.saveChanges();
		SOAPMessage outputMessage = dispatch.invoke(request);
		DOMSource output = new DOMSource(outputMessage.getSOAPPart()
				.getEnvelope());

		StringWriter sw = new StringWriter();
		Transformer tr = TransformerFactory.newInstance().newTransformer();
		tr.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
		tr.transform(output, new StreamResult(sw));

		String response = sw.toString();
		return EndpointReference.createFromXML(response);
	} catch (SOAPException pce) {
		throw new MobyException(
				"Unable to create SOAP document builder for MOBY asynchronous call submission",
				pce);
	} catch (TransformerConfigurationException tce) {
		throw new MobyException(
				"Unable to create transformer factory for MOBY asynchronous call response",
				tce);
	} catch (TransformerException te) {
		throw new MobyException(
				"Unable to create transformer for MOBY asynchronous call response",
				te);
	}
}
 
開發者ID:apache,項目名稱:incubator-taverna-plugin-bioinformatics,代碼行數:81,代碼來源:ExecuteAsyncMobyService.java


注:本文中的javax.xml.soap.SOAPBody.addNamespaceDeclaration方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。