use of gov.nist.javax.sip.header.ContentLength 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 gov.nist.javax.sip.header.ContentLength in project XobotOS by xamarin.
the class SIPMessage method encodeSIPHeaders.
/**
* Encode only the message and exclude the contents (for debugging);
*
* @return a string with all the headers encoded.
*/
protected String encodeSIPHeaders() {
StringBuffer encoding = new StringBuffer();
Iterator<SIPHeader> it = this.headers.iterator();
while (it.hasNext()) {
SIPHeader siphdr = (SIPHeader) it.next();
if (!(siphdr instanceof ContentLength))
siphdr.encode(encoding);
}
return contentLengthHeader.encode(encoding).append(NEWLINE).toString();
}
use of gov.nist.javax.sip.header.ContentLength in project XobotOS by xamarin.
the class SIPResponse method createRequest.
/**
* Generate a request from a response.
*
* @param requestURI -- the request URI to assign to the request.
* @param via -- the Via header to assign to the request
* @param cseq -- the CSeq header to assign to the request
* @param from -- the From header to assign to the request
* @param to -- the To header to assign to the request
* @return -- the newly generated sip request.
*/
public SIPRequest createRequest(SipUri requestURI, Via via, CSeq cseq, From from, To to) {
SIPRequest newRequest = new SIPRequest();
String method = cseq.getMethod();
newRequest.setMethod(method);
newRequest.setRequestURI(requestURI);
this.setBranch(via, method);
newRequest.setHeader(via);
newRequest.setHeader(cseq);
Iterator headerIterator = getHeaders();
while (headerIterator.hasNext()) {
SIPHeader nextHeader = (SIPHeader) headerIterator.next();
// Some headers do not belong in a Request ....
if (SIPMessage.isResponseHeader(nextHeader) || nextHeader instanceof ViaList || nextHeader instanceof CSeq || nextHeader instanceof ContentType || nextHeader instanceof ContentLength || nextHeader instanceof RecordRouteList || nextHeader instanceof RequireList || // JvB: added
nextHeader instanceof ContactList || nextHeader instanceof ContentLength || nextHeader instanceof ServerHeader || nextHeader instanceof ReasonHeader || nextHeader instanceof SessionExpires || nextHeader instanceof ReasonList) {
continue;
}
if (nextHeader instanceof To)
nextHeader = (SIPHeader) to;
else if (nextHeader instanceof From)
nextHeader = (SIPHeader) from;
try {
newRequest.attachHeader(nextHeader, false);
} catch (SIPDuplicateHeaderException e) {
//Should not happen!
e.printStackTrace();
}
}
try {
// JvB: all requests need a Max-Forwards
newRequest.attachHeader(new MaxForwards(70), false);
} catch (Exception d) {
}
if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) {
newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader());
}
return newRequest;
}
use of gov.nist.javax.sip.header.ContentLength in project XobotOS by xamarin.
the class SIPMessage method match.
/**
* Template match for SIP messages. The matchObj is a SIPMessage template to match against.
* This method allows you to do pattern matching with incoming SIP messages. Null matches wild
* card.
*
* @param other is the match template to match against.
* @return true if a match occured and false otherwise.
*/
public boolean match(Object other) {
if (other == null)
return true;
if (!other.getClass().equals(this.getClass()))
return false;
SIPMessage matchObj = (SIPMessage) other;
Iterator<SIPHeader> li = matchObj.getHeaders();
while (li.hasNext()) {
SIPHeader hisHeaders = (SIPHeader) li.next();
List<SIPHeader> myHeaders = this.getHeaderList(hisHeaders.getHeaderName());
// Could not find a header to match his header.
if (myHeaders == null || myHeaders.size() == 0)
return false;
if (hisHeaders instanceof SIPHeaderList) {
ListIterator<?> outerIterator = ((SIPHeaderList<?>) hisHeaders).listIterator();
while (outerIterator.hasNext()) {
SIPHeader hisHeader = (SIPHeader) outerIterator.next();
if (hisHeader instanceof ContentLength)
continue;
ListIterator<?> innerIterator = myHeaders.listIterator();
boolean found = false;
while (innerIterator.hasNext()) {
SIPHeader myHeader = (SIPHeader) innerIterator.next();
if (myHeader.match(hisHeader)) {
found = true;
break;
}
}
if (!found)
return false;
}
} else {
SIPHeader hisHeader = hisHeaders;
ListIterator<SIPHeader> innerIterator = myHeaders.listIterator();
boolean found = false;
while (innerIterator.hasNext()) {
SIPHeader myHeader = (SIPHeader) innerIterator.next();
if (myHeader.match(hisHeader)) {
found = true;
break;
}
}
if (!found)
return false;
}
}
return true;
}
Aggregations