use of javax.sip.header.Header in project load-balancer by RestComm.
the class Shootist method sendInitialInvite.
public void sendInitialInvite() {
try {
if (!started)
start();
String fromName = "BigGuy";
String fromSipAddress = "here.com";
String fromDisplayName = "The Master Blaster";
String toSipAddress = "there.com";
String toUser = "LittleGuy";
String toDisplayName = "The Little Blister";
// create >From Header
SipURI fromAddress = addressFactory.createSipURI(fromName, fromSipAddress);
Address fromNameAddress = addressFactory.createAddress(fromAddress);
fromNameAddress.setDisplayName(fromDisplayName);
FromHeader fromHeader = headerFactory.createFromHeader(fromNameAddress, "12345");
// create To Header
SipURI toAddress = addressFactory.createSipURI(toUser, toSipAddress);
Address toNameAddress = addressFactory.createAddress(toAddress);
toNameAddress.setDisplayName(toDisplayName);
ToHeader toHeader = headerFactory.createToHeader(toNameAddress, null);
// create Request URI
SipURI requestURI = addressFactory.createSipURI(toUser, peerHostPort);
// Create ViaHeaders
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
String ipAddress = udpListeningPoint.getIPAddress();
ViaHeader viaHeader = headerFactory.createViaHeader(ipAddress, sipProvider.getListeningPoint(transport).getPort(), transport, null);
// add via headers
viaHeaders.add(viaHeader);
// Create ContentTypeHeader
ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("application", "sdp");
// Create a new CallId header
CallIdHeader callIdHeader = sipProvider.getNewCallId();
// Create a new Cseq header
CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L, Request.INVITE);
// Create a new MaxForwardsHeader
MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
// Create the request.
Request request = messageFactory.createRequest(requestURI, Request.INVITE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
// Create contact headers
String host = "127.0.0.1";
SipURI contactUrl = addressFactory.createSipURI(fromName, host);
contactUrl.setPort(udpListeningPoint.getPort());
contactUrl.setLrParam();
// Create the contact name address.
SipURI contactURI = addressFactory.createSipURI(fromName, host);
contactURI.setPort(sipProvider.getListeningPoint(transport).getPort());
Address contactAddress = addressFactory.createAddress(contactURI);
// Add the contact address.
contactAddress.setDisplayName(fromName);
contactHeader = headerFactory.createContactHeader(contactAddress);
request.addHeader(contactHeader);
// You can add extension headers of your own making
// to the outgoing SIP request.
// Add the extension header.
Header extensionHeader = headerFactory.createHeader("My-Header", "my header value");
request.addHeader(extensionHeader);
String sdpData = "v=0\r\n" + "o=4855 13760799956958020 13760799956958020" + " IN IP4 129.6.55.78\r\n" + "s=mysession session\r\n" + "p=+46 8 52018010\r\n" + "c=IN IP4 129.6.55.78\r\n" + "t=0 0\r\n" + "m=audio 6022 RTP/AVP 0 4 18\r\n" + "a=rtpmap:0 PCMU/8000\r\n" + "a=rtpmap:4 G723/8000\r\n" + "a=rtpmap:18 G729A/8000\r\n" + "a=ptime:20\r\n";
byte[] contents = sdpData.getBytes();
request.setContent(contents, contentTypeHeader);
// You can add as many extension headers as you
// want.
extensionHeader = headerFactory.createHeader("My-Other-Header", "my new header value ");
request.addHeader(extensionHeader);
Header callInfoHeader = headerFactory.createHeader("Call-Info", "<http://www.antd.nist.gov>");
request.addHeader(callInfoHeader);
// Create the client transaction.
inviteTid = sipProvider.getNewClientTransaction(request);
// send the request out.
inviteTid.sendRequest();
dialog = inviteTid.getDialog();
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
fail("Unxpected exception ");
}
}
use of javax.sip.header.Header in project load-balancer by RestComm.
the class SIPBalancerForwarder method securityCheck.
/**
* @param request
* @return
*/
private boolean securityCheck(Request request) {
// User-Agent: sipcli/v1.8
// User-Agent: friendly-scanner
// To: "sipvicious" <sip:100@1.1.1.1>
// From: "sipvicious" <sip:100@1.1.1.1>;tag=3336353363346565313363340133313330323436343236
// From: "1" <sip:1@87.202.36.237>;tag=3e7a78de
Header userAgentHeader = request.getHeader("User-Agent");
Header toHeader = request.getHeader("To");
Header fromHeader = request.getHeader("From");
for (String blockedValue : balancerRunner.balancerContext.blockedList) {
if (userAgentHeader != null && userAgentHeader.toString().toLowerCase().contains(blockedValue.toLowerCase())) {
return false;
} else if (toHeader != null && toHeader.toString().toLowerCase().contains(blockedValue.toLowerCase())) {
return false;
} else if (fromHeader != null && fromHeader.toString().toLowerCase().contains(blockedValue.toLowerCase())) {
return false;
}
}
return true;
}
use of javax.sip.header.Header in project jain-sip.ha by RestComm.
the class SimpleB2BUAHandler method forwardResponse.
/**
* @param responseEvent
* @throws InvalidArgumentException
*/
@SuppressWarnings("unchecked")
private void forwardResponse(Response receivedResponse) throws SipException, InvalidArgumentException {
if (receivedResponse.getStatusCode() == 202) {
return;
}
System.out.println("Forwarding " + receivedResponse);
final ServerTransaction origServerTransaction = this.getServerTransaction();
Response forgedResponse = null;
try {
forgedResponse = messageFactory.createResponse(receivedResponse.getStatusCode(), origServerTransaction.getRequest());
} catch (ParseException e) {
throw new SipException("Failed to forge message", e);
}
// if ((getIncomingDialog() == null || getIncomingDialog().getState() == DialogState.EARLY) && localTag != null && getIncomingDialog() != null && getIncomingDialog().isServer()) {
// // no tag set in the response, since the dialog creating transaction didn't had it
// try {
// ((ToHeader)forgedResponse.getHeader(ToHeader.NAME)).setTag(localTag);
// } catch (ParseException e) {
// throw new SipException("Failed to set local tag", e);
// }
// }
// copy headers
ListIterator<String> lit = receivedResponse.getHeaderNames();
String headerName = null;
ListIterator<Header> headersIterator = null;
while (lit.hasNext()) {
headerName = lit.next();
if (SimpleB2BUAHandler.getHeadersToOmmitOnResponseCopy().contains(headerName)) {
continue;
} else {
forgedResponse.removeHeader(headerName);
headersIterator = receivedResponse.getHeaders(headerName);
while (headersIterator.hasNext()) {
forgedResponse.addLast((Header) headersIterator.next().clone());
}
}
}
// Copy content
final byte[] rawOriginal = receivedResponse.getRawContent();
if (rawOriginal != null && rawOriginal.length != 0) {
final byte[] copy = new byte[rawOriginal.length];
System.arraycopy(rawOriginal, 0, copy, 0, copy.length);
try {
forgedResponse.setContent(copy, (ContentTypeHeader) forgedResponse.getHeader(ContentTypeHeader.NAME));
} catch (ParseException e) {
throw new SipException("Failed to copy content.", e);
}
}
// set contact if the received response had it
if (receivedResponse.getHeader(ContactHeader.NAME) != null) {
final String transport = ((ViaHeader) forgedResponse.getHeader(ViaHeader.NAME)).getTransport();
forgedResponse.setHeader(((ListeningPointImpl) sipProvider.getListeningPoint(transport)).createContactHeader());
}
// set the to tag mandatory so that the dialog gets replicated
if (((MessageExt) forgedResponse).getToHeader().getTag() == null) {
try {
((MessageExt) forgedResponse).getToHeader().setTag("696");
} catch (ParseException e) {
e.printStackTrace();
}
}
origServerTransaction.sendResponse(forgedResponse);
}
use of javax.sip.header.Header in project smscgateway by RestComm.
the class TxSipServerSbb method onMESSAGE.
// *********
// SIP Event Handlers
public void onMESSAGE(javax.sip.RequestEvent event, ActivityContextInterface aci) {
if (this.logger.isFineEnabled()) {
this.logger.fine("onMESSAGE " + event);
}
Sip sip = sipManagement.getSipByName(SipManagement.SIP_NAME);
try {
final Request request = event.getRequest();
byte[] message = request.getRawContent();
final ToHeader toHeader = (ToHeader) request.getHeader(ToHeader.NAME);
final String toUser = ((SipUri) toHeader.getAddress().getURI()).getUser();
final FromHeader fromHeader = (FromHeader) request.getHeader(FromHeader.NAME);
final String fromUser = ((SipUri) fromHeader.getAddress().getURI()).getUser();
// Persist this message
TargetAddress ta = this.createDestTargetAddress(toUser, sip.getNetworkId());
byte[] udh = null;
Header udhHeader = request.getHeader(SipXHeaders.XSmsUdh);
if (udhHeader != null) {
udh = this.hexStringToByteArray(((SIPHeader) udhHeader).getValue());
}
Header codingHeader = request.getHeader(SipXHeaders.XSmsCoding);
DataCodingSchemeImpl codingSchme = dcsGsm7;
if (codingHeader != null) {
int dcs = Integer.parseInt(((SIPHeader) codingHeader).getValue());
codingSchme = this.createDataCodingScheme(dcs);
}
Date validityPeriod = null;
Header validityHeader = request.getHeader(SipXHeaders.XSmsValidty);
if (validityHeader != null) {
try {
validityPeriod = MessageUtil.parseDate(((SIPHeader) validityHeader).getValue());
} catch (ParseException e) {
logger.severe("ParseException when parsing ValidityPeriod field: " + e.getMessage(), e);
if (smscPropertiesManagement.isGenerateRejectionCdr()) {
generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e.getMessage(), true);
}
ServerTransaction serverTransaction = event.getServerTransaction();
Response res;
try {
res = (this.messageFactory.createResponse(500, serverTransaction.getRequest()));
event.getServerTransaction().sendResponse(res);
} catch (Exception e1) {
this.logger.severe("Exception while trying to send 500 response to sip", e1);
}
return;
}
}
// Registered Delivery
int regDeliveryInt = 0;
Header regDeliveryHeader = request.getHeader(SipXHeaders.XRegDelivery);
if (regDeliveryHeader != null) {
regDeliveryInt = Integer.parseInt(((SIPHeader) regDeliveryHeader).getValue());
}
Sms sms = null;
try {
sms = this.createSmsEvent(fromUser, message, ta, persistence, udh, codingSchme, validityPeriod, regDeliveryInt, sip.getNetworkId());
this.processSms(sms, persistence);
} catch (SmscProcessingException e1) {
if (!e1.isSkipErrorLogging()) {
if (e1.isIsWarning()) {
this.logger.warning(e1.getMessage());
} else {
this.logger.severe(e1.getMessage(), e1);
}
smscStatAggregator.updateMsgInFailedAll();
}
if (smscPropertiesManagement.isGenerateRejectionCdr() && !e1.isMessageRejectCdrCreated()) {
if (sms != null) {
generateCDR(sms, CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), false, true);
} else {
generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), true);
}
}
ServerTransaction serverTransaction = event.getServerTransaction();
Response res;
try {
res = (this.messageFactory.createResponse(500, serverTransaction.getRequest()));
event.getServerTransaction().sendResponse(res);
} catch (Exception e) {
this.logger.severe("Exception while trying to send Ok response to sip", e);
}
return;
} catch (Throwable e1) {
this.logger.severe("Exception while processing a message from sip", e1);
smscStatAggregator.updateMsgInFailedAll();
if (smscPropertiesManagement.isGenerateRejectionCdr()) {
generateCDR(new String(message, utf8), sip.getNetworkId(), fromUser, toUser, ta.getNetworkId(), ta.getAddrTon(), ta.getAddrNpi(), CdrGenerator.CDR_SUBMIT_FAILED_SIP, e1.getMessage(), true);
}
ServerTransaction serverTransaction = event.getServerTransaction();
Response res;
try {
// TODO: we possibly need to response ERROR message to sip
res = (this.messageFactory.createResponse(200, serverTransaction.getRequest()));
event.getServerTransaction().sendResponse(res);
} catch (Exception e) {
this.logger.severe("Exception while trying to send Ok response to sip", e);
}
return;
}
ServerTransaction serverTransaction = event.getServerTransaction();
Response res;
try {
res = (this.messageFactory.createResponse(200, serverTransaction.getRequest()));
event.getServerTransaction().sendResponse(res);
} catch (Exception e) {
this.logger.severe("Exception while trying to send Ok response to sip", e);
}
} catch (Exception e) {
this.logger.severe("Error while trying to process received the SMS " + event, e);
}
}
Aggregations