<commit_before>package com.voxeo.moho.sample;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.voxeo.moho.Application;
import com.voxeo.moho.ApplicationContext;
import com.voxeo.moho.Call;
import com.voxeo.moho.Endpoint;
import com.voxeo.moho.State;
import com.voxeo.moho.TextableEndpoint;
import com.voxeo.moho.event.InputCompleteEvent;
import com.voxeo.moho.event.InviteEvent;
import com.voxeo.moho.event.RegisterEvent;
import com.voxeo.moho.event.TextEvent;
import com.voxeo.moho.media.Recording;
import com.voxeo.moho.media.input.Grammar;
import com.voxeo.moho.media.input.InputCommand;
import com.voxeo.moho.media.input.SimpleGrammar;
import com.voxeo.moho.media.output.AudibleResource;
import com.voxeo.moho.media.output.AudioURIResource;
import com.voxeo.moho.media.output.OutputCommand;
import com.voxeo.moho.media.output.TextToSpeechResource;
import com.voxeo.moho.media.record.RecordCommand;

public class DefaultTestApp implements Application {

  private Map<String, Endpoint> addresses = new ConcurrentHashMap<String, Endpoint>();

  private Map<String, Call> calls = new ConcurrentHashMap<String, Call>();

  private ApplicationContext _ctx = null;

  public void init(ApplicationContext ctx) {
    _ctx = ctx;
  }

  @State
  public void register(final RegisterEvent ev) {
    if (ev.getExpiration() > 0) {
      addresses.put(ev.getEndpoint().getURI().toLowerCase(), ev.getContacts()[0]);
    }
    else {
      addresses.remove(ev.getEndpoint().getURI().toLowerCase());
    }
    ev.accept();
  }

  @State
  public void handleInvite(final InviteEvent inv) {
    final Call call = inv.acceptCall(this);

    try {
      call.join().get();
      call.getMediaService().output("Welcome to Voxeo Prism Test Application").get();
      calls.put(inv.getInvitor().getURI().toLowerCase(), call);

      mainMenu(call);
    }
    catch (Exception ex) {
      call.disconnect();
      calls.remove(call);
      ex.printStackTrace();
    }
  }

  private void mainMenu(Call call) throws Exception {
    call.setApplicationState("main-menu");

    OutputCommand output = new OutputCommand(new TextToSpeechResource(
        "Press or say  1 for testing TTS, Press or say 2 for testing Recording"));
    output.setBargein(true);

    InputCommand input = new InputCommand(new Grammar[] {new SimpleGrammar("1,2"), new SimpleGrammar("one,two")});

    call.getMediaService().prompt(output, input, 0);
  }

  @State("main-menu")
  public void maiMenuInputComplete(final InputCompleteEvent evt) {
    switch (evt.getCause()) {
      case MATCH:
        final Call call = (Call) evt.getSource();
        if (evt.getValue().equals("1") || evt.getValue().equalsIgnoreCase("one")) {
          call.setApplicationState("ttsTest");

          if (addresses.get(call.getAddress().getURI()) != null) {
            try {
              ((TextableEndpoint) _ctx.getEndpoint(addresses.get(call.getAddress().getURI().toLowerCase()).getURI()))
                  .sendText((TextableEndpoint) call.getAddress(),
                      "Please type your message. Type exit, quit or bye to return to the main menu.");
            }
            catch (IOException e) {
              call.disconnect();
              calls.remove(call);
              e.printStackTrace();
            }
          }
        }
        else {
          call.setApplicationState("recordTest");

          final String path = evt.getSource().getApplicationContext().getServletContext().getRealPath(
              call.getAddress().getName() + "_" + new Date().getTime() + "_Recording.au");

          URI recordURI = new File(path).toURI();
          call.setAttribute("RecordFileLocation", recordURI);
<commit_msg>update the default test app.<commit_after>package com.voxeo.moho.sample;

import java.io.File;
import java.net.URI;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.sip.SipServletRequest;

import com.voxeo.moho.Application;
import com.voxeo.moho.ApplicationContext;
import com.voxeo.moho.Call;
import com.voxeo.moho.Endpoint;
import com.voxeo.moho.State;
import com.voxeo.moho.TextableEndpoint;
import com.voxeo.moho.event.InputCompleteEvent;
import com.voxeo.moho.event.InviteEvent;
import com.voxeo.moho.event.RegisterEvent;
import com.voxeo.moho.event.TextEvent;
import com.voxeo.moho.media.Recording;
import com.voxeo.moho.media.input.Grammar;
import com.voxeo.moho.media.input.InputCommand;
import com.voxeo.moho.media.input.SimpleGrammar;
import com.voxeo.moho.media.output.AudibleResource;
import com.voxeo.moho.media.output.AudioURIResource;
import com.voxeo.moho.media.output.OutputCommand;
import com.voxeo.moho.media.output.TextToSpeechResource;
import com.voxeo.moho.media.record.RecordCommand;
import com.voxeo.moho.sip.SIPInviteEvent;

public class DefaultTestApp implements Application {

  private Map<String, Endpoint> addresses = new ConcurrentHashMap<String, Endpoint>();

  private Map<String, Call> calls = new ConcurrentHashMap<String, Call>();

  private ApplicationContext _ctx = null;

  public void init(ApplicationContext ctx) {
    _ctx = ctx;
  }

  @State
  public void register(final RegisterEvent ev) {
    if (ev.getExpiration() > 0) {
      addresses.put(ev.getEndpoint().getURI().toLowerCase(), ev.getContacts()[0]);
    }
    else {
      addresses.remove(ev.getEndpoint().getURI().toLowerCase());
    }
    ev.accept();
  }

  @State
  public void handleInvite(final InviteEvent inv) {
    final Call call = inv.acceptCall(this);

    try {
      SipServletRequest req = ((SIPInviteEvent) inv).getSipRequest();
      call.setAttribute("RemoteContact", req.getAddressHeader("Contact").getURI());
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }

    try {
      call.join().get();
      call.getMediaService().output("Welcome to Voxeo Prism Test Application").get();
      calls.put(inv.getInvitor().getURI().toLowerCase(), call);

      mainMenu(call);
    }
    catch (Exception ex) {
      call.disconnect();
      calls.remove(call);
      ex.printStackTrace();
    }
  }

  private void mainMenu(Call call) throws Exception {
    call.setApplicationState("main-menu");

    OutputCommand output = new OutputCommand(new TextToSpeechResource(
        "Press or say  1 for testing TTS, Press or say 2 for testing Recording"));
    output.setBargein(true);

    InputCommand input = new InputCommand(new Grammar[] {new SimpleGrammar("1,2"), new SimpleGrammar("one,two")});

    call.getMediaService().prompt(output, input, 0);
  }

  @State("main-menu")
  public void maiMenuInputComplete(final InputCompleteEvent evt) {
    switch (evt.getCause()) {
      case MATCH:
        final Call call = (Call) evt.getSource();
        if (evt.getValue().equals("1") || evt.getValue().equalsIgnoreCase("one")) {
          call.setApplicationState("ttsTest");

          try {
            TextableEndpoint endpoint = null;
            if (addresses.get(call.getAddress().getURI().toLowerCase()) != null) {
              endpoint = (TextableEndpoint) addresses.get(call.getAddress().getURI().toLowerCase());
            }
            else {
              endpoint = (TextableEndpoint) _ctx.getEndpoint(((javax.servlet.sip.URI) call
                  .getAttribute("RemoteContact")).toString());
            }

            endpoint.sendText((TextableEndpoint) call.getAddress(),
                "Please type your message. Type exit, quit or bye to return to the main menu.");
          }
          catch (Exception e) {
            call.disconnect();
            calls.remove(call);
            e.printStackTrace();
          }

        }
        else {
          call.setApplicationState("recordTest");

          final String path = evt.getSource().getApplicationContext().getServletContext().getRealPath(
              call.getAddress().getName() + "_" + new Date().getTime() + "_Recording.au");

          URI recordURI = new File(path).toURI();
          call.setAttribute("RecordFileLocation", recordURI);

{'fraction_non_alphanumeric': 0.12958856861295887, 'fraction_numerical': 0.001970928800197093, 'mean_word_length': 3.3048780487804876, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3526840', 'n_tokens_mistral': 2554, 'n_tokens_neox': 2510, 'n_words': 477}