/**
   * Handle 422 response
   *
   * @param resp 422 response
   */
  private void handle422SessionTooSmall(SipResponse resp) {
    try {
      // 422 response received
      if (logger.isActivated()) {
        logger.info("422 response received");
      }

      // Extract the Min-SE value
      int minExpire = SipUtils.getMinSessionExpirePeriod(resp);
      if (minExpire == -1) {
        if (logger.isActivated()) {
          logger.error("Can't read the Min-SE value");
        }
        handleError(
            new FileSharingError(FileSharingError.UNEXPECTED_EXCEPTION, "No Min-SE value found"));
        return;
      }

      // Set the min expire value
      getDialogPath().setMinSessionExpireTime(minExpire);

      // Set the expire value
      getDialogPath().setSessionExpireTime(minExpire);

      // Increment the Cseq number of the dialog path
      getDialogPath().incrementCseq();

      // Create a new INVITE with the right expire period
      if (logger.isActivated()) {
        logger.info("Send new INVITE");
      }
      SipRequest invite =
          SipMessageFactory.createInvite(
              getDialogPath(),
              InstantMessagingService.FT_FEATURE_TAGS,
              getDialogPath().getLocalContent());

      // Set the Authorization header
      getAuthenticationAgent().setAuthorizationHeader(invite);

      // Reset initial request in the dialog path
      getDialogPath().setInvite(invite);

      // Send INVITE request
      sendInvite(invite);

    } catch (Exception e) {
      if (logger.isActivated()) {
        logger.error("Session initiation has failed", e);
      }

      // Unexpected error
      handleError(new FileSharingError(FileSharingError.UNEXPECTED_EXCEPTION, e.getMessage()));
    }
  }
  /**
   * Create INVITE request
   *
   * @param content Content part
   * @return Request
   * @throws SipException
   */
  private SipRequest createInviteRequest(String content) throws SipException {
    SipRequest invite =
        SipMessageFactory.createInvite(
            getDialogPath(), getFeatureTags(), getAcceptContactTags(), content);

    // Test if there is a subject
    if (getSubject() != null) {
      // Add a subject header
      invite.addHeader(SubjectHeader.NAME, StringUtils.encodeUTF8(getSubject()));
    }

    // Add a contribution ID header
    invite.addHeader(ChatUtils.HEADER_CONTRIBUTION_ID, getContributionID());

    return invite;
  }
  /**
   * Handle 407 Proxy Authentication Required
   *
   * @param resp 407 response
   */
  public void handle407Authentication(SipResponse resp) {
    try {
      if (logger.isActivated()) {
        logger.info("407 response received");
      }

      // Set the remote tag
      getDialogPath().setRemoteTag(resp.getToTag());

      // Update the authentication agent
      getAuthenticationAgent().readProxyAuthenticateHeader(resp);

      // Increment the Cseq number of the dialog path
      getDialogPath().incrementCseq();

      // Create a second INVITE request with the right token
      if (logger.isActivated()) {
        logger.info("Send second INVITE");
      }
      SipRequest invite =
          SipMessageFactory.createInvite(
              getDialogPath(),
              InstantMessagingService.FT_FEATURE_TAGS,
              getDialogPath().getLocalContent());

      // Reset initial request in the dialog path
      getDialogPath().setInvite(invite);

      // Set the Proxy-Authorization header
      getAuthenticationAgent().setProxyAuthorizationHeader(invite);

      // Send INVITE request
      sendInvite(invite);

    } catch (Exception e) {
      if (logger.isActivated()) {
        logger.error("Session initiation has failed", e);
      }

      // Unexpected error
      handleError(new FileSharingError(FileSharingError.UNEXPECTED_EXCEPTION, e.getMessage()));
    }
  }
  private SipRequest createSipInvite() {
    logger.debug("createSipInvite()");
    // Set setup mode
    String localSetup = createSetupOffer();
    if (logger.isActivated()) {
      logger.debug("Local setup attribute is " + localSetup);
    }

    // Set local port
    int localMsrpPort = 9; // See RFC4145, Page 4

    // Create the MSRP manager
    String localIpAddress =
        getImsService()
            .getImsModule()
            .getCurrentNetworkInterface()
            .getNetworkAccess()
            .getIpAddress();
    msrpMgr = new MsrpManager(localIpAddress, localMsrpPort);

    // Build SDP part
    String ntpTime = SipUtils.constructNTPtime(System.currentTimeMillis());
    String ipAddress = getDialogPath().getSipStack().getLocalIpAddress();
    /** M: add for MSRPoTLS @{ */
    String protocol = getCurrentProtocol();
    String sdp = null;
    if (PROTOCOL_TLS.equals(protocol)) {
      sdp =
          "v=0"
              + SipUtils.CRLF
              + "o=- "
              + ntpTime
              + " "
              + ntpTime
              + " IN IP4 "
              + ipAddress
              + SipUtils.CRLF
              + "s=-"
              + SipUtils.CRLF
              + "c=IN IP4 "
              + ipAddress
              + SipUtils.CRLF
              + "t=0 0"
              + SipUtils.CRLF
              + "m=message "
              + localMsrpPort
              + " TCP/TLS/MSRP *"
              + SipUtils.CRLF
              + "a=path:"
              + msrpMgr.getLocalMsrpsPath()
              + SipUtils.CRLF
              + "a=fingerprint:"
              + KeyStoreManager.getFingerPrint()
              + SipUtils.CRLF
              + "a=setup:"
              + localSetup
              + SipUtils.CRLF
              + "a=accept-types: "
              + getContent().getEncoding()
              + SipUtils.CRLF
              + "a=file-transfer-id:"
              + getFileTransferId()
              + SipUtils.CRLF
              + "a=file-disposition:attachment"
              + SipUtils.CRLF
              + "a=sendonly"
              + SipUtils.CRLF;
    } else {
      sdp =
          "v=0"
              + SipUtils.CRLF
              + "o=- "
              + ntpTime
              + " "
              + ntpTime
              + " IN IP4 "
              + ipAddress
              + SipUtils.CRLF
              + "s=-"
              + SipUtils.CRLF
              + "c=IN IP4 "
              + ipAddress
              + SipUtils.CRLF
              + "t=0 0"
              + SipUtils.CRLF
              + "m=message "
              + localMsrpPort
              + " TCP/MSRP *"
              + SipUtils.CRLF
              + "a=path:"
              + msrpMgr.getLocalMsrpPath()
              + SipUtils.CRLF
              + "a=setup:"
              + localSetup
              + SipUtils.CRLF
              + "a=accept-types: "
              + getContent().getEncoding()
              + SipUtils.CRLF
              + "a=file-transfer-id:"
              + getFileTransferId()
              + SipUtils.CRLF
              + "a=file-disposition:attachment"
              + SipUtils.CRLF
              + "a=sendonly"
              + SipUtils.CRLF;
    }
    int maxSize = FileSharingSession.getMaxFileSharingSize();
    if (maxSize > 0) {
      sdp += "a=max-size:" + maxSize + SipUtils.CRLF;
    }

    /** @} */
    // Set File-selector attribute
    String selector = getFileSelectorAttribute();
    if (selector != null) {
      sdp += "a=file-selector:" + selector + SipUtils.CRLF;
    }

    // Set File-location attribute
    String location = getFileLocationAttribute();
    if (location != null) {
      sdp += "a=file-location:" + location + SipUtils.CRLF;
    }

    // Set the local SDP part in the dialog path
    getDialogPath().setLocalContent(sdp);

    // Create an INVITE request
    if (logger.isActivated()) {
      logger.info("Create INVITE");
    }
    try {
      SipRequest invite;
      invite =
          SipMessageFactory.createInvite(
              getDialogPath(), InstantMessagingService.FT_FEATURE_TAGS, sdp);
      // Set the Authorization header
      getAuthenticationAgent().setAuthorizationHeader(invite);

      // Set initial request in the dialog path
      getDialogPath().setInvite(invite);
      return invite;
    } catch (SipException e) {
      e.printStackTrace();
    } catch (CoreException e) {
      e.printStackTrace();
    }
    logger.error("Create sip invite failed, return null.");
    return null;
  }