/**
 * Copyright 2010-2011 Voxeo Corporation
 *
 * 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.
 */

package com.voxeo.moho.sip;

import java.util.Map;

import javax.servlet.sip.SipServletResponse;

import com.voxeo.moho.Endpoint;
import com.voxeo.moho.SignalException;
import com.voxeo.moho.common.event.MohoRedirectEvent;
import com.voxeo.moho.event.EventSource;

public class SIPRedirectEventImpl<T extends EventSource> extends MohoRedirectEvent<T> implements SIPRedirectEvent<T> {

  protected SipServletResponse _res;

  protected SIPRedirectEventImpl(final T source, final SipServletResponse res) {
    super(source);
    _res = res;
  }

  @Override
  public SipServletResponse getSipResponse() {
    return _res;
  }

  @Override
  public synchronized void accept(final Map<String, String> headers) throws SignalException {
    this.checkState();
    _accepted = true;
    if (this.source instanceof SIPCallImpl) {
      final SIPCallImpl call = (SIPCallImpl) this.source;
      try {
        call.doResponse(_res, headers);
      }
      catch (final Exception e) {
        throw new SignalException(e);
      }
    }
    else {
      //TODO other source such as Subscrption
    }
  }

  @Override
  public boolean isPermanent() {
    return _res.getStatus() == SipServletResponse.SC_MOVED_PERMANENTLY;
  }

  @Override
  public Endpoint getEndpoint() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Endpoint[] getEndpoints() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void reject(Reason reason, Map<String, String> headers) throws SignalException {
    // TODO Auto-generated method stub
    
  }
}

@@ -657,14 +657,183 @@
 til.
-Map;%0D%0A
+ArrayList;%0D%0Aimport java.util.List;%0D%0Aimport java.util.ListIterator;%0D%0Aimport java.util.Map;%0D%0A%0D%0Aimport javax.servlet.sip.Address;%0D%0Aimport javax.servlet.sip.ServletParseException;
 %0D%0Aim
@@ -1047,16 +1047,61 @@
 Source;%0D
+%0Aimport com.voxeo.moho.spi.ExecutionContext;%0D
 %0A%0D%0Apubli
@@ -1254,16 +1254,56 @@
 e _res;%0D
+%0A  %0D%0A  protected ExecutionContext _ctx;%0D
 %0A%0D%0A  pro
@@ -1412,24 +1412,87 @@
 res = res;%0D%0A
+    _ctx = (ExecutionContext) source.getApplicationContext();%0D%0A
   %7D%0D%0A%0D%0A  @Ov
@@ -2242,175 +2242,959 @@
     
-// TODO Auto-generated method stub%0D%0A    return null;%0D%0A  %7D%0D%0A%0D%0A  @Override%0D%0A  public Endpoint%5B%5D getEndpoints() %7B%0D%0A    // TODO Auto-generated method stub%0D%0A    return null
+try %7B%0D%0A      final ListIterator%3CAddress%3E headers = _res.getAddressHeaders(%22Contact%22);%0D%0A      if (headers.hasNext()) %7B// find and return the first contact header%0D%0A        Address addr = headers.next();%0D%0A        SIPEndpoint ep = new SIPEndpointImpl(_ctx, addr);%0D%0A        return ep;%0D%0A      %7D%0D%0A    %7D%0D%0A    catch (final ServletParseException e) %7B%0D%0A      throw new IllegalArgumentException(e);%0D%0A    %7D%0D%0A    return null;%0D%0A  %7D%0D%0A%0D%0A  @Override%0D%0A  public Endpoint%5B%5D getEndpoints() %7B%0D%0A    final List%3CEndpoint%3E retval = new ArrayList%3CEndpoint%3E();%0D%0A    try %7B%0D%0A      final ListIterator%3CAddress%3E headers = _res.getAddressHeaders(%22Contact%22);%0D%0A      while (headers.hasNext()) %7B%0D%0A        Address addr = headers.next();%0D%0A        SIPEndpoint ep = new SIPEndpointImpl(_ctx, addr);%0D%0A        retval.add(ep);%0D%0A      %7D%0D%0A    %7D%0D%0A    catch (final ServletParseException e) %7B%0D%0A      throw new IllegalArgumentException(e);%0D%0A    %7D%0D%0A    return retval.toArray(new Endpoint%5Bretval.size()%5D)
 ;%0D%0A 

implement redirect event getEndpoint and getEndpoints methods