String extraDomain = doc.attributeValue("name");
String allowMultiple = doc.attributeValue("allowMultiple");
if (extraDomain == null || "".equals(extraDomain)) {
// No new bind domain was specified so return a bad_request error
Element reply = doc.createCopy();
reply.add(new PacketError(PacketError.Condition.bad_request).getElement());
connection.deliverRawText(reply.asXML());
}
else if (extraDomain.equals(initialDomain)) {
// Component is binding initial domain that is already registered
// Send confirmation that the new domain has been registered
connection.deliverRawText("<bind/>");
}
else if (extraDomain.endsWith(initialDomain)) {
// Only accept subdomains under the initial registered domain
if (allowMultiple != null && component.getSubdomains().contains(extraDomain)) {
// Domain already in use so return a conflict error
Element reply = doc.createCopy();
reply.add(new PacketError(PacketError.Condition.conflict).getElement());
connection.deliverRawText(reply.asXML());
}
else {
try {
// Get the requested subdomain
String subdomain = extraDomain;
int index = extraDomain.indexOf(serverName);
if (index > -1) {
subdomain = extraDomain.substring(0, index -1);
}
InternalComponentManager.getInstance().addComponent(subdomain, component);
// Send confirmation that the new domain has been registered
connection.deliverRawText("<bind/>");
}
catch (ComponentException e) {
Log.error("Error binding extra domain: " + extraDomain + " to component: " +
component, e);
// Return internal server error
Element reply = doc.createCopy();
reply.add(new PacketError(
PacketError.Condition.internal_server_error).getElement());
connection.deliverRawText(reply.asXML());
}
}
}
else {
// Return forbidden error since we only allow subdomains of the intial domain
// to be used by the same external component
Element reply = doc.createCopy();
reply.add(new PacketError(PacketError.Condition.forbidden).getElement());
connection.deliverRawText(reply.asXML());
}
return true;
}
return false;