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;
}
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());
}
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());
}
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;
}
Aggregations