/** Patch up the request line as necessary. */ protected void setRequestLineDefaults() { String method = requestLine.getMethod(); if (method == null) { CSeq cseq = (CSeq) this.getCSeq(); if (cseq != null) { method = getCannonicalName(cseq.getMethod()); requestLine.setMethod(method); } } }
/** * Get the message as a linked list of strings. Use this if you want to iterate through the * message. * * @return a linked list containing the request line and headers encoded as strings. */ public LinkedList getMessageAsEncodedStrings() { LinkedList retval = super.getMessageAsEncodedStrings(); if (requestLine != null) { this.setRequestLineDefaults(); retval.addFirst(requestLine.encode()); } return retval; }
/** * Match with a template. You can use this if you want to match incoming messages with a pattern * and do something when you find a match. This is useful for building filters/pattern matching * responders etc. * * @param matchObj object to match ourselves with (null matches wildcard) */ public boolean match(Object matchObj) { if (matchObj == null) return true; else if (!matchObj.getClass().equals(this.getClass())) return false; else if (matchObj == this) return true; SIPRequest that = (SIPRequest) matchObj; RequestLine rline = that.requestLine; if (this.requestLine == null && rline != null) return false; else if (this.requestLine == rline) return super.match(matchObj); return requestLine.match(that.requestLine) && super.match(matchObj); }
/** Encode only the headers and not the content. */ public String encodeMessage() { String retval; if (requestLine != null) { this.setRequestLineDefaults(); retval = requestLine.encode() + super.encodeSIPHeaders(); } else if (this.isNullRequest()) { retval = "\r\n\r\n"; } else retval = super.encodeSIPHeaders(); return retval; }
/** * Convert to a formatted string for pretty printing. Note that the encode method converts this * into a sip message that is suitable for transmission. Note hack here if you want to convert the * nice curly brackets into some grotesque XML tag. * * @return a string which can be used to examine the message contents. */ public String debugDump() { String superstring = super.debugDump(); stringRepresentation = ""; sprint(SIPRequest.class.getName()); sprint("{"); if (requestLine != null) sprint(requestLine.debugDump()); sprint(superstring); sprint("}"); return stringRepresentation; }
/** Set the default values in the request URI if necessary. */ protected void setDefaults() { // The request line may be unparseable (set to null by the // exception handler. if (requestLine == null) return; String method = requestLine.getMethod(); // The requestLine may be malformed! if (method == null) return; GenericURI u = (GenericURI) requestLine.getUri(); if (u == null) return; if (method.compareTo(Request.REGISTER) == 0 || method.compareTo(Request.INVITE) == 0) { if (u instanceof SipUri) { SipUri sipUri = (SipUri) u; sipUri.setUserParam(DEFAULT_USER); try { sipUri.setTransportParam(DEFAULT_TRANSPORT); } catch (ParseException ex) { } } } }
/** * Encode this into a byte array. This is used when the body has been set as a binary array and * you want to encode the body as a byte array for transmission. * * @return a byte array containing the SIPRequest encoded as a byte array. */ public byte[] encodeAsBytes(String transport) { if (this.isNullRequest()) { // Encoding a null message for keepalive. return "\r\n\r\n".getBytes(); } else if (this.requestLine == null) { return new byte[0]; } byte[] rlbytes = null; if (requestLine != null) { try { rlbytes = requestLine.encode().getBytes("UTF-8"); } catch (UnsupportedEncodingException ex) { InternalErrorHandler.handleException(ex); } } byte[] superbytes = super.encodeAsBytes(transport); byte[] retval = new byte[rlbytes.length + superbytes.length]; System.arraycopy(rlbytes, 0, retval, 0, rlbytes.length); System.arraycopy(superbytes, 0, retval, rlbytes.length, superbytes.length); return retval; }
/** * Create an ACK request from this request. This is suitable for generating an ACK for an INVITE * client transaction. * * @return an ACK request that is generated from this request. */ public SIPRequest createACKRequest() { RequestLine requestLine = (RequestLine) this.requestLine.clone(); requestLine.setMethod(Request.ACK); return this.createSIPRequest(requestLine, false); }
/** * Create a BYE request from this request. * * @param switchHeaders is a boolean flag that causes from and isServerTransaction to headers to * be swapped. Set this to true if you are the server of the dialog and are generating a BYE * request for the dialog. * @return a new default BYE request. */ public SIPRequest createBYERequest(boolean switchHeaders) { RequestLine requestLine = (RequestLine) this.requestLine.clone(); requestLine.setMethod("BYE"); return this.createSIPRequest(requestLine, switchHeaders); }
/** * Create a new default SIPRequest from the original request. Warning: the newly created * SIPRequest, shares the headers of this request but we generate any new headers that we need to * modify so the original request is umodified. However, if you modify the shared headers after * this request is created, then the newly created request will also be modified. If you want to * modify the original request without affecting the returned Request make sure you clone it * before calling this method. * * <p>Only required headers are copied. * * <ul> * <li>Contact headers are not included in the newly created request. Setting the appropriate * sequence number is the responsibility of the caller. * <li>RouteList is not copied for ACK and CANCEL * <li>Note that we DO NOT copy the body of the argument into the returned header. We do not * copy the content type header from the original request either. These have to be added * seperately and the content length has to be correctly set if necessary the content length * is set to 0 in the returned header. * <li>Contact List is not copied from the original request. * <li>RecordRoute List is not included from original request. * <li>Via header is not included from the original request. * </ul> * * @param requestLine is the new request line. * @param switchHeaders is a boolean flag that causes to and from headers to switch (set this to * true if you are the server of the transaction and are generating a BYE request). If the * headers are switched, we generate new From and To headers otherwise we just use the * incoming headers. * @return a new Default SIP Request which has the requestLine specified. */ public SIPRequest createSIPRequest(RequestLine requestLine, boolean switchHeaders) { SIPRequest newRequest = new SIPRequest(); newRequest.requestLine = requestLine; Iterator headerIterator = this.getHeaders(); while (headerIterator.hasNext()) { SIPHeader nextHeader = (SIPHeader) headerIterator.next(); // For BYE and cancel set the CSeq header to the // appropriate method. if (nextHeader instanceof CSeq) { CSeq newCseq = (CSeq) nextHeader.clone(); nextHeader = newCseq; try { newCseq.setMethod(requestLine.getMethod()); } catch (ParseException e) { } } else if (nextHeader instanceof ViaList) { Via via = (Via) (((ViaList) nextHeader).getFirst().clone()); via.removeParameter("branch"); nextHeader = via; // Cancel and ACK preserve the branch ID. } else if (nextHeader instanceof To) { To to = (To) nextHeader; if (switchHeaders) { nextHeader = new From(to); ((From) nextHeader).removeTag(); } else { nextHeader = (SIPHeader) to.clone(); ((To) nextHeader).removeTag(); } } else if (nextHeader instanceof From) { From from = (From) nextHeader; if (switchHeaders) { nextHeader = new To(from); ((To) nextHeader).removeTag(); } else { nextHeader = (SIPHeader) from.clone(); ((From) nextHeader).removeTag(); } } else if (nextHeader instanceof ContentLength) { ContentLength cl = (ContentLength) nextHeader.clone(); try { cl.setContentLength(0); } catch (InvalidArgumentException e) { } nextHeader = cl; } else if (!(nextHeader instanceof CallID) && !(nextHeader instanceof MaxForwards)) { // Route is kept by dialog. // RR is added by the caller. // Contact is added by the Caller // Any extension headers must be added // by the caller. continue; } try { newRequest.attachHeader(nextHeader, false); } catch (SIPDuplicateHeaderException e) { e.printStackTrace(); } } if (MessageFactoryImpl.getDefaultUserAgentHeader() != null) { newRequest.setHeader(MessageFactoryImpl.getDefaultUserAgentHeader()); } return newRequest; }
/** * Compare for equality. * * @param other object to compare ourselves with. */ public boolean equals(Object other) { if (!this.getClass().equals(other.getClass())) return false; SIPRequest that = (SIPRequest) other; return requestLine.equals(that.requestLine) && super.equals(other); }
/** * Get the method from the request line. * * @return the method from the request line if the method exits and null if the request line or * the method does not exist. */ public String getMethod() { if (requestLine == null) return null; else return requestLine.getMethod(); }
/** * Check header for constraints. (1) Invite options and bye requests can only have SIP URIs in the * contact headers. (2) Request must have cseq, to and from and via headers. (3) Method in request * URI must match that in CSEQ. */ public void checkHeaders() throws ParseException { String prefix = "Missing a required header : "; /* Check for required headers */ if (getCSeq() == null) { throw new ParseException(prefix + CSeqHeader.NAME, 0); } if (getTo() == null) { throw new ParseException(prefix + ToHeader.NAME, 0); } if (this.callIdHeader == null || this.callIdHeader.getCallId() == null || callIdHeader.getCallId().equals("")) { throw new ParseException(prefix + CallIdHeader.NAME, 0); } if (getFrom() == null) { throw new ParseException(prefix + FromHeader.NAME, 0); } if (getViaHeaders() == null) { throw new ParseException(prefix + ViaHeader.NAME, 0); } if (getMaxForwards() == null) { throw new ParseException(prefix + MaxForwardsHeader.NAME, 0); } if (getTopmostVia() == null) throw new ParseException("No via header in request! ", 0); if (getMethod().equals(Request.NOTIFY)) { if (getHeader(SubscriptionStateHeader.NAME) == null) throw new ParseException(prefix + SubscriptionStateHeader.NAME, 0); if (getHeader(EventHeader.NAME) == null) throw new ParseException(prefix + EventHeader.NAME, 0); } else if (getMethod().equals(Request.PUBLISH)) { /* * For determining the type of the published event state, the EPA MUST include a * single Event header field in PUBLISH requests. The value of this header field * indicates the event package for which this request is publishing event state. */ if (getHeader(EventHeader.NAME) == null) throw new ParseException(prefix + EventHeader.NAME, 0); } /* * RFC 3261 8.1.1.8 The Contact header field MUST be present and contain exactly one SIP * or SIPS URI in any request that can result in the establishment of a dialog. For the * methods defined in this specification, that includes only the INVITE request. For these * requests, the scope of the Contact is global. That is, the Contact header field value * contains the URI at which the UA would like to receive requests, and this URI MUST be * valid even if used in subsequent requests outside of any dialogs. * * If the Request-URI or top Route header field value contains a SIPS URI, the Contact * header field MUST contain a SIPS URI as well. */ if (requestLine.getMethod().equals(Request.INVITE) || requestLine.getMethod().equals(Request.SUBSCRIBE) || requestLine.getMethod().equals(Request.REFER)) { if (this.getContactHeader() == null) { // Make sure this is not a target refresh. If this is a target // refresh its ok not to have a contact header. Otherwise // contact header is mandatory. if (this.getToTag() == null) throw new ParseException(prefix + ContactHeader.NAME, 0); } if (requestLine.getUri() instanceof SipUri) { String scheme = ((SipUri) requestLine.getUri()).getScheme(); if ("sips".equalsIgnoreCase(scheme)) { SipUri sipUri = (SipUri) this.getContactHeader().getAddress().getURI(); if (!sipUri.getScheme().equals("sips")) { throw new ParseException("Scheme for contact should be sips:" + sipUri, 0); } } } } /* * Contact header is mandatory for a SIP INVITE request. */ if (this.getContactHeader() == null && (this.getMethod().equals(Request.INVITE) || this.getMethod().equals(Request.REFER) || this.getMethod().equals(Request.SUBSCRIBE))) { throw new ParseException("Contact Header is Mandatory for a SIP INVITE", 0); } if (requestLine != null && requestLine.getMethod() != null && getCSeq().getMethod() != null && requestLine.getMethod().compareTo(getCSeq().getMethod()) != 0) { throw new ParseException("CSEQ method mismatch with Request-Line ", 0); } }