private RESTConfig getRESTConfig(final String endpoint) {
    final String proxyHost = cfg.getProxyHost();
    final int proxyPort = cfg.getProxyPort();
    final boolean trustAllCerts = cfg.getTrustAllCerts();

    return new RESTConfig(endpoint, proxyHost, proxyPort, trustAllCerts);
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    this.handleSpeechToText(request);

    String speechContexts = cfg.getProperty("speechContexts");
    request.setAttribute("xArg", cfg.getProperty("xArg", ""));
    request.setAttribute("speechContexts", speechContexts.split(","));
    request.setAttribute("fnames", this.audioFiles);
    request.setAttribute("cfg", new ConfigBean());

    final String forward = "WEB-INF/Speech.jsp";
    RequestDispatcher dispatcher = request.getRequestDispatcher(forward);
    dispatcher.forward(request, response);
  }
 public void init() {
   try {
     this.cfg = AppConfig.getInstance();
     loadTemplateContent();
     this.audioFiles = this.getAudioFileNames();
   } catch (IOException e) {
     // print stack trace instead of handling
     e.printStackTrace();
   }
 }
  private String[] getAudioFileNames() {
    String audioFolder = cfg.getProperty("audioFolder");

    String dir = getServletContext().getRealPath("/") + audioFolder;

    File[] files = new File(dir).listFiles();
    String[] fnames = new String[files.length];
    for (int i = 0; i < files.length; ++i) {
      fnames[i] = files[i].getName();
    }

    Arrays.sort(fnames);
    return fnames;
  }
  private void handleSpeechToText(HttpServletRequest request) {
    String speechContext = request.getParameter("SpeechContext");
    request.setAttribute("mimeData", getMimeData());
    request.getSession().setAttribute("sessionmimeData", getMimeData());

    if (request.getParameter("SpeechToText") == null) {
      if (speechContext != null) {
        request.getSession().setAttribute("sessionContextName", speechContext);
      } else {
        request.getSession().setAttribute("sessionContextName", "GenericHints");
      }
      return;
    }

    try {
      HttpSession session = request.getSession();
      String xarg = cfg.getProperty("xArg");

      session.setAttribute("sessionContextName", speechContext);

      String endpoint = cfg.getFQDN() + cfg.getProperty("contextPath");
      SpeechService srvc = new SpeechService(getRESTConfig(endpoint));

      String fname = request.getParameter("audio_file");
      session.setAttribute("sessionFileName", fname);

      String audioFolder = cfg.getProperty("audioFolder");
      File file = new File(getPath() + audioFolder + "/" + fname);

      String[] attachments = new String[3];
      attachments[0] =
          new File(getPath() + "/" + cfg.getProperty("GenericHints.template")).getAbsolutePath();
      attachments[1] =
          new File(getPath() + "/" + cfg.getProperty("GrammarList.template")).getAbsolutePath();
      attachments[2] = file.getAbsolutePath();

      String mimeData = this.templateContent.get(speechContext);
      session.setAttribute("sessionmimeData", mimeData);

      OAuthToken token = getToken();
      SpeechResponse response =
          srvc.sendRequest(attachments, token.getAccessToken(), speechContext, xarg);

      request.setAttribute("resultSpeech", response.getResult());
    } catch (Exception e) {
      e.printStackTrace();
      request.setAttribute("errorSpeech", e.getMessage());
    }
  }
  private OAuthToken getToken() throws RESTException {
    try {
      final AppConfig cfg = AppConfig.getInstance();
      final String path = "WEB-INF/token.properties";
      final String tokenFile = getServletContext().getRealPath(path);

      OAuthToken token = OAuthToken.loadToken(tokenFile);
      if (token == null || token.isAccessTokenExpired()) {
        final String endpoint = cfg.getFQDN() + OAuthService.API_URL;
        final String clientId = cfg.getClientId();
        final String clientSecret = cfg.getClientSecret();
        final OAuthService service =
            new OAuthService(getRESTConfig(endpoint), clientId, clientSecret);

        token = service.getToken(cfg.getProperty("scope"));
        token.saveToken(tokenFile);
      }
      return token;
    } catch (IOException ioe) {
      throw new RESTException(ioe);
    }
  }