content: /*******************************************************************************
   * Software Name : RCS IMS Stack
   *
   * Copyright (C) 2010-2016 Orange.
   * Copyright (C) 2014 Sony Mobile Communications Inc.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   * NOTE: This file has been modified by Sony Mobile Communications Inc.
   * Modifications are licensed under the License.
   ******************************************************************************/
  
  package com.gsma.rcs.core.ims.service.im.chat;
  
  import com.gsma.rcs.core.FileAccessException;
  import com.gsma.rcs.core.ims.network.NetworkException;
  import com.gsma.rcs.core.ims.network.sip.SipMessageFactory;
  import com.gsma.rcs.core.ims.protocol.PayloadException;
  import com.gsma.rcs.core.ims.protocol.sdp.SdpUtils;
  import com.gsma.rcs.core.ims.protocol.sip.SipRequest;
  import com.gsma.rcs.core.ims.protocol.sip.SipResponse;
  import com.gsma.rcs.core.ims.service.im.InstantMessagingService;
  import com.gsma.rcs.provider.contact.ContactManager;
  import com.gsma.rcs.provider.messaging.MessagingLog;
  import com.gsma.rcs.provider.settings.RcsSettings;
  import com.gsma.rcs.utils.logger.Logger;
  
  import android.text.TextUtils;
  
  import java.text.ParseException;
  
  import javax2.sip.InvalidArgumentException;
  import javax2.sip.header.SubjectHeader;
  
  /**
   * Rejoin a group chat session
   * 
   * @author Jean-Marc AUFFRET
   */
  public class RejoinGroupChatSession extends GroupChatSession {
  
      private final static Logger sLogger = Logger.getLogger(RejoinGroupChatSession.class.getName());
  
      /**
       * Constructor
       * 
       * @param imService InstantMessagingService
       * @param groupChatInfo Group Chat information
       * @param rcsSettings Rcs settings
       * @param messagingLog Messaging log
       * @param timestamp Local timestamp for the session
       * @param contactManager the contact manager
       */
      public RejoinGroupChatSession(InstantMessagingService imService, GroupChatInfo groupChatInfo,
              RcsSettings rcsSettings, MessagingLog messagingLog, long timestamp,
              ContactManager contactManager) {
          super(imService, null, groupChatInfo.getRejoinId(), groupChatInfo.getParticipants(),
                  rcsSettings, messagingLog, timestamp, contactManager);
  
          if (!TextUtils.isEmpty(groupChatInfo.getSubject())) {
              setSubject(groupChatInfo.getSubject());
          }
          createOriginatingDialogPath();
          setContributionID(groupChatInfo.getContributionId());
      }
  
      @Override
      public void run() {
          try {
              if (sLogger.isActivated()) {
                  sLogger.info("Rejoin an existing group chat session");
              }
  
              String localSetup = createSetupOffer();
              if (sLogger.isActivated()) {
                  sLogger.debug("Local setup attribute is ".concat(localSetup));
              }
  
              int localMsrpPort;
              if ("active".equals(localSetup)) {
                  localMsrpPort = 9; /* See RFC4145, Page 4 */
              } else {
                  localMsrpPort = getMsrpMgr().getLocalMsrpPort();
              }
  
              String ipAddress = getDialogPath().getSipStack().getLocalIpAddress();
              String sdp = SdpUtils.buildGroupChatSDP(ipAddress, localMsrpPort, getMsrpMgr()
                      .getLocalSocketProtocol(), getAcceptTypes(), getWrappedTypes(), localSetup,
                      getMsrpMgr().getLocalMsrpPath(), SdpUtils.DIRECTION_SENDRECV);
  
              getDialogPath().setLocalContent(sdp);
  
              if (sLogger.isActivated()) {
                  sLogger.info("Send INVITE");
              }
              SipRequest invite = createInviteRequest(sdp);
  
              getAuthenticationAgent().setAuthorizationHeader(invite);
  
              getDialogPath().setInvite(invite);
  
              sendInvite(invite);
  
          } catch (InvalidArgumentException | ParseException | FileAccessException | PayloadException
                  | NetworkException | RuntimeException e) {
              handleError(new ChatError(ChatError.SESSION_REJOIN_FAILED, e));
          }
      }
  
      /**
       * Create INVITE request
       * 
       * @param content Content part
       * @return Request
       * @throws PayloadException
       */
      private SipRequest createInviteRequest(String content) throws PayloadException {
          try {
              SipRequest invite = SipMessageFactory.createInvite(getDialogPath(), getFeatureTags(),
                      getAcceptContactTags(), content);
              final String subject = getSubject();
              if (subject != null) {
                  invite.addHeader(SubjectHeader.NAME, subject);
              }
              invite.addHeader(ChatUtils.HEADER_CONTRIBUTION_ID, getContributionID());
              return invite;
  
          } catch (ParseException e) {
              throw new PayloadException("Failed to create invite request!", e);
          }
      }
  
      @Override
      public SipRequest createInvite() throws PayloadException {
          return createInviteRequest(getDialogPath().getLocalContent());
      }
  
      @Override
      public void handle404SessionNotFound(SipResponse resp) {
          handleError(new ChatError(ChatError.SESSION_NOT_FOUND, resp.getReasonPhrase()));
      }
  
      @Override
      public boolean isInitiatedByRemote() {
          return false;
      }
  
      @Override
      public void startSession() {
          getImsService().getImsModule().getInstantMessagingService().addSession(this);
          start();
      }
  }
  