public class TenantDomainHandler extends AbstractHandler {
public InvocationResponse invoke(MessageContext msgContext) throws AxisFault {
CarbonContextHolder carbonContext = CarbonContextHolder.getThreadLocalCarbonContextHolder();
String tenantDomain = null;
EndpointReference epr = msgContext.getTo();
if (epr != null) {
String toAddress = epr.getAddress();
if (toAddress != null) {
int tenantDelimiterIndex = toAddress.indexOf("/a/");
if (tenantDelimiterIndex != -1) {
String temp = toAddress.substring(tenantDelimiterIndex + 3);
tenantDomain = temp.substring(0, temp.indexOf("/"));
if (tenantDomain != null) {
carbonContext.setTenantDomain(tenantDomain);
}
//remove the domain details so that it can be dispatched
String newAddress = toAddress.replace("/a/" + tenantDomain, "");
EndpointReference newEpr = new EndpointReference(newAddress);
msgContext.setTo(newEpr);
msgContext.getOptions().setTo(newEpr);
}
}
}
if (tenantDomain == null) {
// try to dispatch from the soap header.
SOAPHeader soapHeader = msgContext.getEnvelope().getHeader();
if (soapHeader != null) {
OMElement tenantDomainElement =
soapHeader.getFirstChildWithName(
new QName(MultitenantConstants.TENANT_DOMAIN_HEADER_NAMESPACE,
MultitenantConstants.TENANT_DOMAIN_HEADER_NAME));
if (tenantDomainElement != null) {
tenantDomain = tenantDomainElement.getText();
}
}
}
if (tenantDomain == null) {
Map<String, String> transportHeaders =
(Map<String, String>) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS);
if (transportHeaders != null) {
tenantDomain = transportHeaders.get(MultitenantConstants.TENANT_DOMAIN_HEADER_NAME);
}
}
if (tenantDomain != null) {
carbonContext.setTenantDomain(tenantDomain);
}
return InvocationResponse.CONTINUE;
}