use of javax.sip.header.ContactHeader in project jain-sip.ha by RestComm.
the class SIPDialogCacheData method updateDialogMetaData.
/**
* Update the haSipDialog passed in param with the dialogMetaData and app meta data
* @param dialogMetaData
* @param dialogAppData
* @param haSipDialog
* @throws ParseException
* @throws PeerUnavailableException
*/
private void updateDialogMetaData(Map<String, Object> dialogMetaData, Object dialogAppData, HASipDialog haSipDialog, boolean recreation) throws ParseException, PeerUnavailableException {
haSipDialog.setMetaDataToReplicate(dialogMetaData, recreation);
haSipDialog.setApplicationDataToReplicate(dialogAppData);
final String contactStringified = (String) dialogMetaData.get(AbstractHASipDialog.CONTACT_HEADER);
if (logger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
logger.logDebug("contactStringified " + contactStringified);
}
if (contactStringified != null) {
Address contactAddress = SipFactory.getInstance().createAddressFactory().createAddress(contactStringified);
ContactHeader contactHeader = SipFactory.getInstance().createHeaderFactory().createContactHeader(contactAddress);
if (logger.isLoggingEnabled(StackLogger.TRACE_DEBUG)) {
logger.logDebug("contactHeader " + contactHeader);
logger.logDebug("contactURI " + contactHeader.getAddress().getURI());
}
haSipDialog.setContactHeader(contactHeader);
}
}
use of javax.sip.header.ContactHeader in project XobotOS by xamarin.
the class SIPDialog method createRequest.
/**
* The method that actually does the work of creating a request.
*
* @param method
* @param response
* @return
* @throws SipException
*/
private Request createRequest(String method, SIPResponse sipResponse) throws SipException {
if (method == null || sipResponse == null)
throw new NullPointerException("null argument");
if (method.equals(Request.CANCEL))
throw new SipException("Dialog.createRequest(): Invalid request");
if (this.getState() == null || (this.getState().getValue() == TERMINATED_STATE && !method.equalsIgnoreCase(Request.BYE)) || (this.isServer() && this.getState().getValue() == EARLY_STATE && method.equalsIgnoreCase(Request.BYE)))
throw new SipException("Dialog " + getDialogId() + " not yet established or terminated " + this.getState());
SipUri sipUri = null;
if (this.getRemoteTarget() != null)
sipUri = (SipUri) this.getRemoteTarget().getURI().clone();
else {
sipUri = (SipUri) this.getRemoteParty().getURI().clone();
sipUri.clearUriParms();
}
CSeq cseq = new CSeq();
try {
cseq.setMethod(method);
cseq.setSeqNumber(this.getLocalSeqNumber());
} catch (Exception ex) {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logError("Unexpected error");
InternalErrorHandler.handleException(ex);
}
/*
* Add a via header for the outbound request based on the transport of the message
* processor.
*/
ListeningPointImpl lp = (ListeningPointImpl) this.sipProvider.getListeningPoint(sipResponse.getTopmostVia().getTransport());
if (lp == null) {
if (sipStack.isLoggingEnabled())
sipStack.getStackLogger().logError("Cannot find listening point for transport " + sipResponse.getTopmostVia().getTransport());
throw new SipException("Cannot find listening point for transport " + sipResponse.getTopmostVia().getTransport());
}
Via via = lp.getViaHeader();
From from = new From();
from.setAddress(this.localParty);
To to = new To();
to.setAddress(this.remoteParty);
SIPRequest sipRequest = sipResponse.createRequest(sipUri, via, cseq, from, to);
if (SIPRequest.isTargetRefresh(method)) {
ContactHeader contactHeader = ((ListeningPointImpl) this.sipProvider.getListeningPoint(lp.getTransport())).createContactHeader();
((SipURI) contactHeader.getAddress().getURI()).setSecure(this.isSecure());
sipRequest.setHeader(contactHeader);
}
try {
/*
* Guess of local sequence number - this is being re-set when the request is actually
* dispatched
*/
cseq = (CSeq) sipRequest.getCSeq();
cseq.setSeqNumber(this.localSequenceNumber + 1);
} catch (InvalidArgumentException ex) {
InternalErrorHandler.handleException(ex);
}
if (method.equals(Request.SUBSCRIBE)) {
if (eventHeader != null)
sipRequest.addHeader(eventHeader);
}
try {
if (this.getLocalTag() != null) {
from.setTag(this.getLocalTag());
} else {
from.removeTag();
}
if (this.getRemoteTag() != null) {
to.setTag(this.getRemoteTag());
} else {
to.removeTag();
}
} catch (ParseException ex) {
InternalErrorHandler.handleException(ex);
}
// get the route list from the dialog.
this.updateRequest(sipRequest);
return sipRequest;
}
use of javax.sip.header.ContactHeader in project XobotOS by xamarin.
the class MessageChannel method logResponse.
/**
* Log a response received at this message channel. This is used for processing incoming
* responses to a client transaction.
*
* @param receptionTime is the time at which the response was received.
* @param status is the processing status of the message.
*
*/
public void logResponse(SIPResponse sipResponse, long receptionTime, String status) {
int peerport = getPeerPort();
if (peerport == 0 && sipResponse.getContactHeaders() != null) {
ContactHeader contact = (ContactHeader) sipResponse.getContactHeaders().getFirst();
peerport = ((AddressImpl) contact.getAddress()).getPort();
}
String from = getPeerAddress().toString() + ":" + peerport;
String to = this.getHost() + ":" + getPort();
this.getSIPStack().serverLogger.logMessage(sipResponse, from, to, status, false, receptionTime);
}
use of javax.sip.header.ContactHeader in project load-balancer by RestComm.
the class TestSipListener method processSubscribe.
/**
* Process the invite request.
*/
public void processSubscribe(RequestEvent requestEvent, ServerTransaction serverTransaction) {
SipProvider sipProvider = (SipProvider) requestEvent.getSource();
Request request = requestEvent.getRequest();
try {
logger.info("notifier: got an Subscribe sending OK");
logger.info("notifier: " + request);
logger.info("notifier : dialog = " + requestEvent.getDialog());
EventHeader eventHeader = (EventHeader) request.getHeader(EventHeader.NAME);
// this.gotSubscribeRequest = true;
// Always create a ServerTransaction, best as early as possible in the code
Response response = null;
ServerTransaction st = requestEvent.getServerTransaction();
if (st == null) {
st = sipProvider.getNewServerTransaction(request);
}
// Check if it is an initial SUBSCRIBE or a refresh / unsubscribe
boolean isInitial = requestEvent.getDialog() == null;
if (isInitial) {
// JvB: need random tags to test forking
String toTag = Integer.toHexString((int) (Math.random() * Integer.MAX_VALUE));
response = protocolObjects.messageFactory.createResponse(202, request);
ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
// Sanity check: to header should not ahve a tag. Else the dialog
// should have matched
// Application is supposed to set.
toHeader.setTag(toTag);
this.dialog = st.getDialog();
// subscribe dialogs do not terminate on bye.
this.dialog.terminateOnBye(false);
} else {
response = protocolObjects.messageFactory.createResponse(202, request);
this.dialog = st.getDialog();
// subscribe dialogs do not terminate on bye.
this.dialog.terminateOnBye(false);
}
// Both 2xx response to SUBSCRIBE and NOTIFY need a Contact
Address address = null;
if (!isIpv6)
address = protocolObjects.addressFactory.createAddress("Notifier <sip:127.0.0.1>");
else
address = protocolObjects.addressFactory.createAddress("Notifier <sip:[::1]>");
((SipURI) address.getURI()).setPort(sipProvider.getListeningPoint(ListeningPoint.UDP).getPort());
ContactHeader contactHeader = protocolObjects.headerFactory.createContactHeader(address);
response.addHeader(contactHeader);
// Expires header is mandatory in 2xx responses to SUBSCRIBE
ExpiresHeader expires = (ExpiresHeader) request.getHeader(ExpiresHeader.NAME);
if (expires == null) {
// rather short
expires = protocolObjects.headerFactory.createExpiresHeader(30);
}
response.addHeader(expires);
/*
* JvB: The SUBSCRIBE MUST be answered first. See RFC3265 3.1.6.2:
* "[...] a NOTIFY message is always sent immediately after any 200-
* class response to a SUBSCRIBE request"
*
* Do this before creating the NOTIFY request below
*/
st.sendResponse(response);
/*
* NOTIFY requests MUST contain a "Subscription-State" header with a
* value of "active", "pending", or "terminated". The "active" value
* indicates that the subscription has been accepted and has been
* authorized (in most cases; see section 5.2.). The "pending" value
* indicates that the subscription has been received, but that
* policy information is insufficient to accept or deny the
* subscription at this time. The "terminated" value indicates that
* the subscription is not active.
*/
if (sendNotify) {
Request notifyRequest = dialog.createRequest("NOTIFY");
// Mark the contact header, to check that the remote contact is updated
// ((SipURI)contactHeader.getAddress().getURI()).setParameter("id","not");
// Initial state is pending, second time we assume terminated (Expires==0)
SubscriptionStateHeader sstate = protocolObjects.headerFactory.createSubscriptionStateHeader(expires.getExpires() != 0 ? SubscriptionStateHeader.PENDING : SubscriptionStateHeader.TERMINATED);
allSubscriptionStates.add(sstate.getState().toLowerCase());
// Need a reason for terminated
if (sstate.getState().equalsIgnoreCase("terminated")) {
sstate.setReasonCode("deactivated");
}
notifyRequest.addHeader(sstate);
notifyRequest.setHeader(eventHeader);
notifyRequest.setHeader(contactHeader);
// notifyRequest.setHeader(routeHeader);
ClientTransaction ct = sipProvider.getNewClientTransaction(notifyRequest);
if (sstate.getState().equals(SubscriptionStateHeader.TERMINATED)) {
Thread.sleep(timeToWaitBetweenSubsNotify);
}
// Let the other side know that the tx is pending acceptance
//
dialog.sendRequest(ct);
logger.info("NOTIFY Branch ID " + ((ViaHeader) request.getHeader(ViaHeader.NAME)).getParameter("branch"));
logger.info("Dialog " + dialog);
logger.info("Dialog state after pending NOTIFY: " + dialog.getState());
if (expires.getExpires() != 0) {
Thread myEventSource = new Thread(new MyEventSource(this, eventHeader));
myEventSource.start();
}
}
} catch (Throwable ex) {
logger.info(ex.getMessage(), ex);
}
}
use of javax.sip.header.ContactHeader in project load-balancer by RestComm.
the class BackToBackUserAgent method forwardResponse.
private void forwardResponse(ResponseEvent receivedResponse) throws SipException {
try {
ServerTransaction serverTransaction = (ServerTransaction) receivedResponse.getClientTransaction().getApplicationData();
Request stRequest = serverTransaction.getRequest();
Response newResponse = this.messageFactory.createResponse(receivedResponse.getResponse().getStatusCode(), stRequest);
ListeningPoint peerListeningPoint = sp.getListeningPoint(transport);
ContactHeader peerContactHeader = ((ListeningPointExt) peerListeningPoint).createContactHeader();
newResponse.setHeader(peerContactHeader);
serverTransaction.sendResponse(newResponse);
} catch (InvalidArgumentException e) {
throw new SipException("invalid response", e);
} catch (ParseException e) {
throw new SipException("invalid response", e);
}
}
Aggregations