use of javax.sip.header.ViaHeader in project Openfire by igniterealtime.
the class SimpleSession method prepareRequest.
/**
* @param requestType Type of request
* @param destUri The SipURI for the destination. Leave <code>null</code> if a loopback request (e.g. REGISTER) is being made.
* @param toTag The tag for to header. Can leave null.
* @param requestUri The Request URI to set in the message. Leave null if the default destination SipURI should be used.
* @param callId ID of call
* @param seqNum Sequence number
* @return Prepared request
*/
private Request prepareRequest(RequestType requestType, SipURI destUri, String toTag, SipURI requestUri, String callId, long seqNum) {
Request request = null;
String myXMPPUsername = this.jid.getNode();
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing request packet of type '" + requestType + "'");
try {
// Prepare request packet first
request = messageFactory.createRequest(null);
request.setMethod(requestType.toString());
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing request.", e);
}
// Prepare "From" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"From\" header...");
String mySipUsername = registration.getUsername();
try {
SipURI fromUri = addressFactory.createSipURI(mySipUsername, sipHost);
Address fromNameAddress = addressFactory.createAddress(fromUri);
fromNameAddress.setDisplayName(mySipUsername);
FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, getTag());
// Use "set" because this header is mandatory.
request.setHeader(fromHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing FromHeader.", e);
return null;
}
// Prepare "To" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"To\" header...");
try {
if (destUri == null)
destUri = addressFactory.createSipURI(mySipUsername, sipHost);
Address toNameAddress = addressFactory.createAddress(destUri);
ToHeader toHeader = headerFactory.createToHeader(toNameAddress, toTag);
// Use "set" because this header is mandatory.
request.setHeader(toHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing ToHeader.", e);
return null;
}
// Prepare "Via" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"Via\" header...");
try {
ViaHeader viaHeader = headerFactory.createViaHeader(InetAddress.getLocalHost().getHostAddress(), sipPort, ListeningPoint.UDP, null);
// Use "set" because this header is mandatory.
request.setHeader(viaHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing ViaHeader.", e);
return null;
}
// Prepare "CallId" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"CallId\" header...");
CallIdHeader callIdHeader;
try {
if (callId != null)
callIdHeader = headerFactory.createCallIdHeader(callId);
else
callIdHeader = udpSipProvider.getNewCallId();
// Use "set" because this header is mandatory.
request.setHeader(callIdHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing CallIdHeader.", e);
return null;
}
// Prepare "CSeq" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"CSeq\" header...");
try {
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(seqNum, requestType.toString());
// Use "set" because this header is mandatory.
request.setHeader(cSeqHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing CSeqHeader.", e);
return null;
}
// Prepare "MaxForwards" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"MaxForwards\" header...");
try {
MaxForwardsHeader maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);
// Use "set" because this header is mandatory.
request.setHeader(maxForwardsHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when preparing MaxForwardsHeader.", e);
return null;
}
// Setting Request URI
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: setting request URI...");
try {
if (requestUri == null) {
requestUri = (SipURI) destUri.clone();
requestUri.setTransportParam(ListeningPoint.UDP);
}
request.setRequestURI(requestUri);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when setting request URI.", e);
return null;
}
// Add "Contact" header
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Preparing \"Contact\" header...");
try {
SipURI contactURI = addressFactory.createSipURI(mySipUsername, InetAddress.getLocalHost().getHostAddress());
contactURI.setPort(sipPort);
Address contactAddress = addressFactory.createAddress(contactURI);
contactAddress.setDisplayName(mySipUsername);
ContactHeader contactHeader = headerFactory.createContactHeader(contactAddress);
request.setHeader(contactHeader);
} catch (Exception e) {
Log.debug("SimpleSession(" + myXMPPUsername + ").prepareRequest: Exception occured when adding ContactHeader.", e);
return null;
}
return request;
}
use of javax.sip.header.ViaHeader in project XobotOS by xamarin.
the class DefaultRouter method createHop.
/**
* Utility method to create a hop from a SIP URI
*
* @param sipUri
* @return
*/
private final Hop createHop(SipURI sipUri, Request request) {
// always use TLS when secure
String transport = sipUri.isSecure() ? SIPConstants.TLS : sipUri.getTransportParam();
if (transport == null) {
//@see issue 131
ViaHeader via = (ViaHeader) request.getHeader(ViaHeader.NAME);
transport = via.getTransport();
}
// sipUri.removeParameter("transport");
int port;
if (sipUri.getPort() != -1) {
port = sipUri.getPort();
} else {
if (transport.equalsIgnoreCase(SIPConstants.TLS))
port = 5061;
else
// TCP or UDP
port = 5060;
}
String host = sipUri.getMAddrParam() != null ? sipUri.getMAddrParam() : sipUri.getHost();
AddressResolver addressResolver = this.sipStack.getAddressResolver();
return addressResolver.resolveAddress(new HopImpl(host, port, transport));
}
use of javax.sip.header.ViaHeader in project XobotOS by xamarin.
the class SIPMessage method encodeAsBytes.
/**
* Encode the message as a byte array. Use this when the message payload is a binary byte
* array.
*
* @return The Canonical byte array representation of the message (including the canonical
* byte array representation of the SDP payload if it exists all in one contiguous
* byte array).
*/
public byte[] encodeAsBytes(String transport) {
if (this instanceof SIPRequest && ((SIPRequest) this).isNullRequest()) {
return "\r\n\r\n".getBytes();
}
// JvB: added to fix case where application provides the wrong transport
// in the topmost Via header
ViaHeader topVia = (ViaHeader) this.getHeader(ViaHeader.NAME);
try {
topVia.setTransport(transport);
} catch (ParseException e) {
InternalErrorHandler.handleException(e);
}
StringBuffer encoding = new StringBuffer();
synchronized (this.headers) {
Iterator<SIPHeader> it = this.headers.iterator();
while (it.hasNext()) {
SIPHeader siphdr = (SIPHeader) it.next();
if (!(siphdr instanceof ContentLength))
siphdr.encode(encoding);
}
}
contentLengthHeader.encode(encoding);
encoding.append(NEWLINE);
byte[] retval = null;
byte[] content = this.getRawContent();
if (content != null) {
// Append the content
byte[] msgarray = null;
try {
msgarray = encoding.toString().getBytes(getCharset());
} catch (UnsupportedEncodingException ex) {
InternalErrorHandler.handleException(ex);
}
retval = new byte[msgarray.length + content.length];
System.arraycopy(msgarray, 0, retval, 0, msgarray.length);
System.arraycopy(content, 0, retval, msgarray.length, content.length);
} else {
try {
retval = encoding.toString().getBytes(getCharset());
} catch (UnsupportedEncodingException ex) {
InternalErrorHandler.handleException(ex);
}
}
return retval;
}
use of javax.sip.header.ViaHeader in project XobotOS by xamarin.
the class AuthenticationHelperImpl method removeBranchID.
/**
* Removes all via headers from <tt>request</tt> and replaces them with a new one, equal to
* the one that was top most.
*
* @param request the Request whose branchID we'd like to remove.
*
*/
private void removeBranchID(Request request) {
ViaHeader viaHeader = (ViaHeader) request.getHeader(ViaHeader.NAME);
viaHeader.removeParameter("branch");
}
use of javax.sip.header.ViaHeader in project XobotOS by xamarin.
the class SipSessionGroup method extractExternalAddress.
private void extractExternalAddress(ResponseEvent evt) {
Response response = evt.getResponse();
ViaHeader viaHeader = (ViaHeader) (response.getHeader(SIPHeaderNames.VIA));
if (viaHeader == null)
return;
int rport = viaHeader.getRPort();
String externalIp = viaHeader.getReceived();
if ((rport > 0) && (externalIp != null)) {
mExternalIp = externalIp;
mExternalPort = rport;
Log.d(TAG, " got external addr " + externalIp + ":" + rport + " on " + mSipStack);
}
}
Aggregations