Search in sources :

Example 46 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project googleads-java-lib by googleads.

the class RequestInfoXPathSet method parseMessage.

public RequestInfo.Builder parseMessage(RequestInfo.Builder builder, SOAPMessage soapMessage) {
    Preconditions.checkNotNull(builder, "Null builder");
    Transformer transformer = transformerSupplier.get();
    if (soapMessage == null || soapMessage.getSOAPPart() == null || transformer == null) {
        return builder;
    }
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        // Some SOAP frameworks don't include SOAP headers when calling SOAPMessage.writeTo.
        // Use an XML transformer to write the XML content instead.
        transformer.transform(soapMessage.getSOAPPart().getContent(), new StreamResult(outputStream));
        builder.withPayload(outputStream.toString(StandardCharsets.UTF_8.name()));
    } catch (TransformerException | SOAPException | IOException e) {
        builder.withPayload("Unable to read request content due to exception: " + e);
        libLogger.warn("Unable to read request content due to exception.", e);
    }
    try {
        SOAPHeader soapHeader = soapMessage.getSOAPHeader();
        builder.withContext(contextName, nodeExtractor.extractNodeValue(soapHeader, contextXPath));
    } catch (SOAPException e) {
        builder.withContext(contextName, "Unable to extract " + contextName + " from request due to exception: " + e);
    }
    return builder;
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) SOAPException(javax.xml.soap.SOAPException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 47 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project googleads-java-lib by googleads.

the class ResponseInfoXPathSetTest method testParseActualMessage.

@Test
public void testParseActualMessage() throws SOAPException, IOException {
    Builder builder = new Builder();
    SOAPMessage message = Mockito.mock(SOAPMessage.class);
    SOAPHeader header = Mockito.mock(SOAPHeader.class);
    when(message.getSOAPHeader()).thenReturn(header);
    final String payload = "<foo><bar>MyBar</bar></foo>";
    doAnswer(invocation -> {
        OutputStream outputStream = (OutputStream) invocation.getArguments()[0];
        outputStream.write(payload.getBytes(StandardCharsets.UTF_8));
        return null;
    }).when(message).writeTo(org.mockito.ArgumentMatchers.any(OutputStream.class));
    assertSame("parseMessage should return the same builder passed in", builder, xPathSet.parseMessage(builder, message));
    ResponseInfo responseInfo = builder.build();
    assertEquals("Payload doesn't match", payload, responseInfo.getPayload());
}
Also used : ResponseInfo(com.google.api.ads.common.lib.client.ResponseInfo) Builder(com.google.api.ads.common.lib.client.ResponseInfo.Builder) OutputStream(java.io.OutputStream) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader) Test(org.junit.Test)

Example 48 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project googleads-java-lib by googleads.

the class RequestInfoXPathSetTest method testParseActualMessage.

@Test
public void testParseActualMessage() throws SOAPException, IOException, TransformerException {
    Builder builder = new Builder();
    SOAPMessage message = Mockito.mock(SOAPMessage.class);
    SOAPHeader header = Mockito.mock(SOAPHeader.class);
    SOAPPart soapPart = Mockito.mock(SOAPPart.class);
    when(message.getSOAPHeader()).thenReturn(header);
    when(message.getSOAPPart()).thenReturn(soapPart);
    final String payload = "<foo><bar>MyBar</bar></foo>";
    when(transformerSupplier.get()).thenReturn(transformer);
    doAnswer(invocation -> {
        StreamResult streamResult = (StreamResult) invocation.getArguments()[1];
        streamResult.getOutputStream().write(payload.getBytes(StandardCharsets.UTF_8));
        return null;
    }).when(transformer).transform(any(), any());
    assertSame("parseMessage should return the same builder passed in", builder, xPathSet.parseMessage(builder, message));
    RequestInfo requestInfo = builder.build();
    assertEquals("Payload doesn't match", payload, requestInfo.getPayload());
    assertEquals("Context name doesn't match", "bar", requestInfo.getContextName());
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) Builder(com.google.api.ads.common.lib.client.RequestInfo.Builder) SOAPPart(javax.xml.soap.SOAPPart) RequestInfo(com.google.api.ads.common.lib.client.RequestInfo) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPHeader(javax.xml.soap.SOAPHeader) Test(org.junit.Test)

Example 49 with SOAPHeader

use of javax.xml.soap.SOAPHeader in project csb-sdk by aliyun.

the class SOAPHeaderHandler method handleMessage.

@Override
public boolean handleMessage(SOAPMessageContext context) {
    try {
        Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (outboundProperty.booleanValue()) {
            SOAPEnvelope envelope = context.getMessage().getSOAPPart().getEnvelope();
            SOAPHeader header = envelope.getHeader();
            if (header == null)
                header = envelope.addHeader();
            if (wsParams.getAk() != null) {
                Map<String, String> headers = SignUtil.newParamsMap(null, wsParams.getApi(), wsParams.getVersion(), wsParams.getAk(), wsParams.getSk(), wsParams.isTimestamp(), wsParams.isNonce(), WSClientSDK.genExtHeader(wsParams.getFingerPrinter()), null, wsParams.getSignImpl(), wsParams.getVerifySignImpl(), wsParams.getSignAlgothrim());
                for (Entry<String, String> kv : headers.entrySet()) {
                    header.addHeaderElement(new QName(HEADER_NS, kv.getKey())).setTextContent(kv.getValue());
                    dumpHeaders(kv.getKey(), kv.getValue());
                }
            }
            if (wsParams.isMockRequest()) {
                header.addHeaderElement(new QName(HEADER_NS, HEADER_MOCK));
                dumpHeaders(HEADER_MOCK, "");
            }
        } else {
        // remove the unnecessary response headers
        // SOAPEnvelope envelope =
        // context.getMessage().getSOAPPart().getEnvelope();
        }
    } catch (Exception e) {
        throw new WSClientException("failed to add soap header", e);
    }
    return true;
}
Also used : QName(javax.xml.namespace.QName) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) WSClientException(com.alibaba.csb.ws.sdk.WSClientException) SOAPHeader(javax.xml.soap.SOAPHeader) WSClientException(com.alibaba.csb.ws.sdk.WSClientException)

Aggregations

SOAPHeader (javax.xml.soap.SOAPHeader)49 SOAPException (javax.xml.soap.SOAPException)30 SOAPMessage (javax.xml.soap.SOAPMessage)29 SOAPHeaderElement (javax.xml.soap.SOAPHeaderElement)22 SOAPElement (javax.xml.soap.SOAPElement)18 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)17 QName (javax.xml.namespace.QName)13 SOAPBody (javax.xml.soap.SOAPBody)13 Name (javax.xml.soap.Name)10 WebServiceException (javax.xml.ws.WebServiceException)9 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)8 ArrayList (java.util.ArrayList)7 SOAPPart (javax.xml.soap.SOAPPart)7 Test (org.junit.Test)7 Element (org.w3c.dom.Element)7 IOException (java.io.IOException)6 SOAPFactory (javax.xml.soap.SOAPFactory)6 NodeList (org.w3c.dom.NodeList)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4