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);
    }
  }