@Override
public void processMessage(SIPMessage message) throws Exception {
if(message instanceof Request) {
Request request = (Request) message;
if(request.getMethod().equals(Request.REGISTER)) {
ContactHeader contact = (ContactHeader) request.getHeader(ContactHeader.NAME);
URI uri = contact.getAddress().getURI();
if(uri.isSipURI()) {
SipURI sipUri = (SipURI) uri;
String host = sipUri.getHost();
NioTcpMessageProcessor processor = (NioTcpMessageProcessor) this.messageProcessor;
HostPort hostPort = new HostPort();
hostPort.setHost(new Host(host));
hostPort.setPort(5060);
processor.assignChannelToDestination(hostPort, this);
}
}
ContactHeader contact = (ContactHeader)message.getHeader(ContactHeader.NAME);
RecordRouteHeader rr = (RecordRouteHeader)message.getHeader(RecordRouteHeader.NAME);
ViaHeader via = message.getTopmostViaHeader();
if(rr == null) {
if(contact != null) {
rewriteUri((SipURI) contact.getAddress().getURI());
}
} else {
rewriteUri((SipURI) rr.getAddress().getURI()); // not needed but just in case some clients does it
}
String viaHost = via.getHost();
if(viaHost.endsWith(".invalid")) {
via.setHost(getPeerAddress());
via.setPort(getPeerPort());
}
} else {
ContactHeader contact = (ContactHeader)message.getHeader(ContactHeader.NAME);
if(contact != null) {
rewriteUri((SipURI) contact.getAddress().getURI());
}
}
super.processMessage(message);
}