com.microsoft.bingads.internal.HeaderHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of microsoft.bingads Show documentation
Show all versions of microsoft.bingads Show documentation
The Bing Ads Java SDK is a library improving developer experience when working with the Bing Ads services by providing high-level access to features such as Bulk API, OAuth Authorization and SOAP API.
package com.microsoft.bingads.internal;
import javax.xml.namespace.QName;
import jakarta.xml.soap.SOAPHeader;
import jakarta.xml.soap.SOAPMessage;
import jakarta.xml.ws.handler.MessageContext;
import jakarta.xml.ws.handler.soap.SOAPHandler;
import jakarta.xml.ws.handler.soap.SOAPMessageContext;
import com.microsoft.bingads.InternalException;
import java.util.Map;
import java.util.Set;
/**
* Created by Esti Fisher on 11/10/2015.
*/
public class HeaderHandler implements SOAPHandler {
private static HeaderHandler instance = new HeaderHandler();
public static HeaderHandler getInstance() {
return instance;
}
@Override
public boolean handleMessage(SOAPMessageContext context) {
try {
Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outbound) {
SOAPMessage message = context.getMessage();
SOAPHeader tempHeader = message.getSOAPHeader();
if (tempHeader == null) {
tempHeader = message.getSOAPPart().getEnvelope().addHeader();
}
final SOAPHeader header = tempHeader;
Map headers = (Map)context.get(ServiceUtils.REQUEST_HEADERS_KEY);
String namespaceURI = ((QName)context.get(MessageContext.WSDL_INTERFACE)).getNamespaceURI();
for (Map.Entry entry : headers.entrySet()) {
header.addHeaderElement(new QName(namespaceURI, entry.getKey())).addTextNode(entry.getValue());
}
} else {
String headerValue = getSpecificHeaderValue(context.getMessage().getSOAPHeader(), ServiceUtils.TRACKING_HEADER_NAME);
if (headerValue != null) {
context.put(ServiceUtils.TRACKING_KEY, headerValue);
context.setScope(ServiceUtils.TRACKING_KEY, MessageContext.Scope.APPLICATION);
}
}
} catch (Exception e) {
throw new InternalException(e);
}
return true;
}
private String getSpecificHeaderValue(SOAPHeader header, String localName) {
return (header != null && header.getFirstChild() != null && header.getFirstChild().getLocalName() != null && header.getFirstChild().getLocalName().equals(localName)) ?
header.getFirstChild().getTextContent() : null;
}
@Override
public Set getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext context) {
return true;
}
@Override
public void close(MessageContext context) {
}
}