/**
   * Creates a default SIPResponse message for this request. Note You must add the necessary tags to
   * outgoing responses if need be. For efficiency, this method does not clone the incoming request.
   * If you want to modify the outgoing response, be sure to clone the incoming request as the
   * headers are shared and any modification to the headers of the outgoing response will result in
   * a modification of the incoming request. Tag fields are just copied from the incoming request.
   * Contact headers are removed from the incoming request. Added by Jeff Keyser. Route headers are
   * not added to the response.
   *
   * @param statusCode Status code for the response.
   * @param reasonPhrase Reason phrase for this response.
   * @return A SIPResponse with the status and reason supplied, and a copy of all the original
   *     headers from this request except the ones that are not supposed to be part of the response
   *     .
   */
  public SIPResponse createResponse(int statusCode, String reasonPhrase) {
    SIPResponse newResponse;
    Iterator headerIterator;
    SIPHeader nextHeader;

    newResponse = new SIPResponse();
    try {
      newResponse.setStatusCode(statusCode);
    } catch (ParseException ex) {
      throw new IllegalArgumentException("Bad code " + statusCode);
    }
    if (reasonPhrase != null) newResponse.setReasonPhrase(reasonPhrase);
    else newResponse.setReasonPhrase(SIPResponse.getReasonPhrase(statusCode));
    headerIterator = getHeaders();
    while (headerIterator.hasNext()) {
      nextHeader = (SIPHeader) headerIterator.next();
      if (nextHeader instanceof From
          || nextHeader instanceof To
          || nextHeader instanceof ViaList
          || nextHeader instanceof CallID
          || (nextHeader instanceof RecordRouteList && mustCopyRR(statusCode))
          || nextHeader instanceof CSeq
          // We just copy TimeStamp for all headers (not just 100).
          || nextHeader instanceof TimeStamp) {

        try {

          newResponse.attachHeader((SIPHeader) nextHeader.clone(), false);
        } catch (SIPDuplicateHeaderException e) {
          e.printStackTrace();
        }
      }
    }
    if (MessageFactoryImpl.getDefaultServerHeader() != null) {
      newResponse.setHeader(MessageFactoryImpl.getDefaultServerHeader());
    }
    if (newResponse.getStatusCode() == 100) {
      // Trying is never supposed to have the tag parameter set.
      newResponse.getTo().removeParameter("tag");
    }
    ServerHeader server = MessageFactoryImpl.getDefaultServerHeader();
    if (server != null) {
      newResponse.setHeader(server);
    }
    return newResponse;
  }
Ejemplo n.º 2
0
  /**
   * Handles a new SIP response. It finds a client transaction to handle this message. If none
   * exists, it sends the message directly to the superclass.
   *
   * @param responseReceived Response to handle.
   * @param responseMessageChannel Channel that received message.
   * @return A client transaction.
   */
  protected ServerResponseInterface newSIPServerResponse(
      SIPResponse responseReceived, MessageChannel responseMessageChannel) {
    //	System.out.println("response = " + responseReceived.encode());

    // Iterator through all client transactions
    Iterator<SIPClientTransaction> transactionIterator;
    // Next transaction in the set
    SIPClientTransaction nextTransaction;
    // Transaction to handle this request
    SIPClientTransaction currentTransaction;

    String key = responseReceived.getTransactionId();

    currentTransaction = (SIPClientTransaction) clientTransactionTable.get(key);

    if (currentTransaction == null
        || !currentTransaction.isMessagePartOfTransaction(responseReceived)) {
      // Loop through all server transactions
      synchronized (clientTransactions) {
        transactionIterator = clientTransactions.iterator();
        currentTransaction = null;
        while (transactionIterator.hasNext() && currentTransaction == null) {

          nextTransaction = (SIPClientTransaction) transactionIterator.next();

          // If this transaction should handle this request,
          if (nextTransaction.isMessagePartOfTransaction(responseReceived)) {

            // Mark this transaction as the one to
            // handle this message
            currentTransaction = nextTransaction;
          }
        }
      }

      // If no transaction exists to handle this message,
      if (currentTransaction == null) {

        // Pass the message directly to the TU
        return super.newSIPServerResponse(responseReceived, responseMessageChannel);
      }
    }

    // Set ths transaction's encapsulated response interface
    // from the superclass
    currentTransaction.setResponseInterface(
        super.newSIPServerResponse(responseReceived, currentTransaction));
    return currentTransaction;
  }
  /**
   * Creates a default SIPResponse message for this request. Note You must add the necessary tags to
   * outgoing responses if need be. For efficiency, this method does not clone the incoming request.
   * If you want to modify the outgoing response, be sure to clone the incoming request as the
   * headers are shared and any modification to the headers of the outgoing response will result in
   * a modification of the incoming request. Tag fields are just copied from the incoming request.
   * Contact headers are removed from the incoming request. Added by Jeff Keyser.
   *
   * @param statusCode Status code for the response. Reason phrase is generated.
   * @return A SIPResponse with the status and reason supplied, and a copy of all the original
   *     headers from this request.
   */
  public SIPResponse createResponse(int statusCode) {

    String reasonPhrase = SIPResponse.getReasonPhrase(statusCode);
    return this.createResponse(statusCode, reasonPhrase);
  }