package com.googlecode.test.sip.phone.sip.handle.uas;

import static com.googlecode.test.sip.phone.sip.SipConstants.Factorys.MESSAGE_FACTORY;

import javax.sip.RequestEvent;
import javax.sip.ServerTransaction;
import javax.sip.header.CallIdHeader;
import javax.sip.header.Header;
import javax.sip.message.Request;
import javax.sip.message.Response;

import org.apache.log4j.Logger;

import com.googlecode.test.sip.phone.sip.phone.AbstractSipPhone;

public class ByeRequestHandler extends AbstractRequestHandler {
	
	private static final Logger LOG = Logger.getLogger(ByeRequestHandler.class);


	public ByeRequestHandler(AbstractSipPhone sipPhone) {
		super(sipPhone);
 	}

	@Override
	public void handle(RequestEvent requestEvent) {
 		Request request = requestEvent.getRequest();
 		try {
			//ServerTransaction newServerTransaction = this.sipPhone.getSipProvider().getNewServerTransaction(requestEvent.getRequest());
 	 
			ServerTransaction serverTransaction = requestEvent.getServerTransaction();
 	 
 			Header alsoHeader = request.getHeader("Also");
 			
 			
 			
 			if (serverTransaction != null) {
					Response response = MESSAGE_FACTORY.createResponse(Response.OK, request);
					LOG.info(response);
					serverTransaction.sendResponse(response);
	  		}
 			
 			if(alsoHeader!=null){
 				CallIdHeader callIdHeader=(CallIdHeader)request.getHeader(CallIdHeader.NAME);
 				this.sipPhone.stopRtpSession();
				this.sipPhone.invite(alsoHeader.toString(), callIdHeader.getCallId()+"_also");
				 				
 			}else{
  				sipPhone.stopRtpSession();
 			}

		

		} catch (Exception ex) {
			ex.printStackTrace();
		}
			 
 
	}

}

@@ -816,143 +816,9 @@
  %7B%0D%0A
-%09%09%09//ServerTransaction newServerTransaction = this.sipPhone.getSipProvider().getNewServerTransaction(requestEvent.getRequest());%0D%0A %09 %0D%0A
+ 
 %09%09%09S
@@ -893,20 +893,16 @@
 on();%0D%0A 
-%09 %0D%0A
  %09%09%09Head
@@ -946,27 +946,16 @@
 so%22);%0D%0A 
-%09%09%09%0D%0A %09%09%09%0D%0A
  %09%09%09%0D%0A %09
@@ -1459,14 +1459,9 @@
 %09%7D%0D%0A
-%0D%0A%09%09%0D%0A
+ 
 %0D%0A%09%09

format the bye request