/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sip.ResponseEvent;
import javax.sip.address.Address;
import javax.sip.address.AddressFactory;
import javax.sip.address.URI;
import javax.sip.header.AllowHeader;
import javax.sip.header.AuthorizationHeader;
import javax.sip.header.CSeqHeader;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ContactHeader;
import javax.sip.header.ExpiresHeader;
import javax.sip.header.FromHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.MaxForwardsHeader;
import javax.sip.header.ToHeader;
import javax.sip.header.ViaHeader;
import javax.sip.header.WWWAuthenticateHeader;
import javax.sip.message.MessageFactory;
import javax.sip.message.Request;
import logger.OhmLogger;

/**
 * Erstellt den Header auf eine Unauthorized Request vom SIP Server nach erstem
 * Versuch der Anmeldung. Es sind immer 2 "Anmeldungen" nötig um sich zu registrieren!!
 *
 * @author Jan
 */
public class SIPUnauthReq
{
  private Request unauthReq;
  private static final Logger lgUnauth = OhmLogger.getLogger();

  public SIPUnauthReq(String myName, String proxyAddress, String myTag,
                      String myIPAddress,
                      int branch, long cseq, int myPort,
                      ContactHeader contactHeader,
                      AddressFactory addressFactory, HeaderFactory headerFactory,
                      ResponseEvent responseEvent, MessageFactory messageFactory)
  {
    try
    {
      Address addresstob = addressFactory.createAddress("sip:" + myName + "@" + proxyAddress);
      ToHeader temp = (ToHeader) responseEvent.getResponse().getHeader("To");

      ToHeader to = headerFactory.createToHeader(addresstob, temp.getTag()); // Integer.toString(myTag)  address + myTag
      URI requestURI = addresstob.getURI();
      CallIdHeader callId = (CallIdHeader) responseEvent.getResponse().getHeader("Call-ID");
      FromHeader from = headerFactory.createFromHeader(addresstob, myTag); //this.contactAddress 
      WWWAuthenticateHeader wwwauth = (WWWAuthenticateHeader) responseEvent.getResponse().getHeader("WWW-Authenticate");

      //wird bei AuthorizationHeader wirklich alles benötigt? war glaub nur mal Test -> testen!!
      AuthorizationHeader auth = headerFactory.createAuthorizationHeader("Digest");//was kommt in den string
      auth.setRealm(wwwauth.getRealm());
      auth.setNonce(wwwauth.getNonce());
      //Address addresstest = this.addressFactory.createAddress("sip:" + myName + "@192.168.100.11"); //vielleicht passwort setzen "sip:"+myName+":\"\"@192.168.100.11"
      Address addresstest = addressFactory.createAddress("sip:" + myName + "@" + proxyAddress);
      auth.setURI(addresstest.getURI());
      auth.setUsername(myName);

      ArrayList via = new ArrayList(); //Via needs a List as input
      ViaHeader viaheader = headerFactory.createViaHeader(myIPAddress, myPort, "UDP", String.valueOf(branch + 2));// String.valueOf(hashCode())string branch
      via.add(viaheader);

      CSeqHeader cSeq = headerFactory.createCSeqHeader(cseq, "REGISTER");
      MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);
      AllowHeader allow = headerFactory.createAllowHeader("NOTIFY,INVITE,ACK,CANCEL,BYE,REFER,INFO,OPTIONS,MESSAGE");
      ExpiresHeader expire = headerFactory.createExpiresHeader(3600); //int value

      //Zusätzliche Register Header nach Unauthorzied
      //Create Request Header
      unauthReq = messageFactory.createRequest(requestURI, "REGISTER", callId, cSeq, from, to, via, maxForwards);
      unauthReq.addHeader(contactHeader);
      unauthReq.addHeader(allow);
      unauthReq.addHeader(expire);
      unauthReq.addHeader(auth);

      lgUnauth.info("Request Header für 2 Registrierung erstellt");
    }
    catch (Exception ex)
    {
      lgUnauth.getLogger(SIPmodel.class.getName()).log(Level.SEVERE, null, ex);
    }

  }

  /**
   * @return the unauthReq
   */
  public Request getUnauthReq()
  {
    return unauthReq;
  }
}
