From f2b166a153ad4ef295c2f2277f4fa1e893ff6782 Mon Sep 17 00:00:00 2001 From: Emil Ivov Date: Mon, 28 Sep 2009 10:03:33 +0000 Subject: [PATCH] Centralizes use of Contact header generation. --- .../sip/ProtocolProviderServiceSipImpl.java | 49 ------------------- .../impl/protocol/sip/SipMessageFactory.java | 46 ++++++++++++++++- 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/src/net/java/sip/communicator/impl/protocol/sip/ProtocolProviderServiceSipImpl.java b/src/net/java/sip/communicator/impl/protocol/sip/ProtocolProviderServiceSipImpl.java index bdd7dcbfe..4ce186ae4 100644 --- a/src/net/java/sip/communicator/impl/protocol/sip/ProtocolProviderServiceSipImpl.java +++ b/src/net/java/sip/communicator/impl/protocol/sip/ProtocolProviderServiceSipImpl.java @@ -1001,55 +1001,6 @@ public MaxForwardsHeader getMaxForwardsHeader() throws return maxForwardsHeader; } - /** - * Returns a Contact header containing a sip URI based on the local-host - * address that seemed most suitable for use according what we - * guessed while looking at request. - * - * @param request the request that we are preparing to answer with the - * response that we are building a Contact header for. - * - * @return a Contact header based upon a local inet address. - */ - public ContactHeader getContactHeaderForResponse(Request request) - { - //emil: so here's the thing. ideally we should use the address and - //port that this request arrived at. However there's no way for us - //to know this. First because jain-sip does not provide such a way - //and second (more important :) ) because the JRE itself does not, - //provide away of retrieving the specific local address for a - //datagram was received on a 0.0.0.0/::0 bound sock. - - //therefore, since jain-sip will (normally be sending our response to - //the address in the topmost Via .. we use it to determine a local - //address. - ViaHeader via = (ViaHeader)request.getHeader(ViaHeader.NAME); - SipURI intendedDestinationURI; - - String transport = via.getTransport(); - - String host = via.getHost(); - int port = via.getPort(); - - try - { - intendedDestinationURI = addressFactory.createSipURI(null, host); - intendedDestinationURI.setPort(port); - - if(transport != null) - intendedDestinationURI.setTransportParam(transport); - } - catch (ParseException e) - { - logger.debug(via + " does not seem to be a valid header."); - - FromHeader from = (FromHeader)request.getHeader(From.NAME); - intendedDestinationURI = (SipURI)from.getAddress().getURI(); - } - - return getContactHeader(intendedDestinationURI); - } - /** * Returns a Contact header containing a sip URI based on a localhost * address and therefore usable in REGISTER requests only. diff --git a/src/net/java/sip/communicator/impl/protocol/sip/SipMessageFactory.java b/src/net/java/sip/communicator/impl/protocol/sip/SipMessageFactory.java index d08a4ef85..9f304ee3d 100644 --- a/src/net/java/sip/communicator/impl/protocol/sip/SipMessageFactory.java +++ b/src/net/java/sip/communicator/impl/protocol/sip/SipMessageFactory.java @@ -306,7 +306,51 @@ private Message attachScSpecifics(Message message) */ private Message attachContactHeader(Message message) { - //creates a Contact header + if(message instanceof Request) + { + Request request = (Request)message; + + SipURI requestURI = (SipURI)request.getRequestURI(); + + request.setHeader(protocolProvider.getContactHeader(requestURI)); + return request; + } + else + { + Response response = (Response)message; + + ViaHeader via = (ViaHeader)response.getHeader(ViaHeader.NAME); + SipURI intendedDestinationURI; + + String transport = via.getTransport(); + + String host = via.getHost(); + int port = via.getPort(); + + try + { + intendedDestinationURI = protocolProvider.getAddressFactory() + .createSipURI(null, host); + intendedDestinationURI.setPort(port); + + if(transport != null) + intendedDestinationURI.setTransportParam(transport); + } + catch (ParseException e) + { + logger.debug(via + " does not seem to be a valid header."); + + FromHeader from = (FromHeader)response.getHeader(From.NAME); + intendedDestinationURI = (SipURI)from.getAddress().getURI(); + } + + ContactHeader contactHeader + = protocolProvider.getContactHeader(intendedDestinationURI); + + response.setHeader(contactHeader); + + return response; + } } /**