Exemplo n.º 1
0
  PwmHttpClientResponse makeApiRequest(
      final HttpMethod method, final String urlPart, final Serializable body)
      throws PwmUnrecoverableException {
    final Map<String, String> headers = new HashMap<>();
    headers.put(PwmConstants.HttpHeader.Content_Type.getHttpName(), "application/json");
    if (locale != null) {
      headers.put(PwmConstants.HttpHeader.Accept_Language.getHttpName(), locale.toLanguageTag());
    }

    final PwmHttpClientRequest pwmHttpClientRequest =
        new PwmHttpClientRequest(
            method, getEndpointURL() + urlPart, JsonUtil.serialize(body), headers);
    return pwmHttpClient.makeRequest(pwmHttpClientRequest);
  }
Exemplo n.º 2
0
  @RequestMapping(
      value = {NEWS_FI, NEWS_SV},
      method = GET)
  public String news(Model model, Locale locale) {
    Urls urls = Urls.get(locale);

    String pageUri = HelpPage.NEWS.getUri(locale.toLanguageTag());

    model.addAttribute(ALT_URI_ATTR, urls.alt().news());
    model.addAttribute("content", infoTextService.getPublished(pageUri));
    model.addAttribute("pageUri", pageUri);
    model.addAttribute("omUser", userService.getCurrentUser().isOm());

    addPiwicIdIfNotAuthenticated(model);

    return NEWS_VIEW;
  }
Exemplo n.º 3
0
 private boolean setLocale(Locale lang, boolean onActivate) {
   Locale[] available = WInputMethodDescriptor.getAvailableLocalesInternal();
   for (int i = 0; i < available.length; i++) {
     Locale locale = available[i];
     if (lang.equals(locale)
         ||
         // special compatibility rule for Japanese and Korean
         locale.equals(Locale.JAPAN) && lang.equals(Locale.JAPANESE)
         || locale.equals(Locale.KOREA) && lang.equals(Locale.KOREAN)) {
       if (isActive) {
         setNativeLocale(locale.toLanguageTag(), onActivate);
       }
       currentLocale = locale;
       return true;
     }
   }
   return false;
 }
Exemplo n.º 4
0
 public static void updateLocaleCookie(
     Response.ResponseBuilder builder,
     Locale locale,
     RealmModel realm,
     UriInfo uriInfo,
     String path) {
   if (locale == null) {
     return;
   }
   boolean secure = realm.getSslRequired().isRequired(uriInfo.getRequestUri().getHost());
   builder.cookie(
       new NewCookie(
           LocaleHelper.LOCALE_COOKIE,
           locale.toLanguageTag(),
           path,
           null,
           null,
           31536000,
           secure));
 }
  protected Status processCommunicateAction(
      final Exchange exchange, final CommunicateAction communicateAction) throws IOException {
    final EmotionKind emotionKind =
        Optional.ofNullable(communicateAction.getEmotionKind()).orElse(EmotionKind.NEUTRAL);
    final Locale lang = Optional.ofNullable(communicateAction.getInLanguage()).orElse(Locale.US);
    log.info("Got speech lang-legacy={}: {}", lang.getLanguage(), communicateAction);
    final String avatarId = Optional.ofNullable(communicateAction.getAvatarId()).orElse("nao1");

    // final File wavFile = File.createTempFile("lumen-speech-synthesis_", ".wav");
    //                        final File oggFile = File.createTempFile("lumen-speech-synthesis_",
    // ".ogg");
    try {
      byte[] wavBytes = null;
      if (INDONESIAN.getLanguage().equals(lang.getLanguage())) {
        // Expressive speech (for now, Indonesian only)
        try {
          PhonemeDoc phonemeDoc;
          if (EmotionKind.NEUTRAL == emotionKind) {
            phonemeDoc = speechProsody.performNeutral(communicateAction.getObject());
          } else {
            try {
              final EmotionProsody emotionProsody =
                  emotionProsodies
                      .getEmotion(emotionKind)
                      .orElseThrow(
                          () ->
                              new SpeechSynthesisException(
                                  "Emotion " + emotionKind + " not supported"));
              phonemeDoc = speechProsody.perform(communicateAction.getObject(), emotionProsody);
            } catch (Exception e) {
              log.error(
                  "Cannot speak with emotion "
                      + emotionKind
                      + ", falling back to NEUTRAL: "
                      + communicateAction.getObject(),
                  e);
              phonemeDoc = speechProsody.performNeutral(communicateAction.getObject());
            }
          }

          try (final ByteArrayInputStream objectIn =
                  new ByteArrayInputStream(phonemeDoc.toString().getBytes(StandardCharsets.UTF_8));
              final ByteArrayOutputStream wavStream = new ByteArrayOutputStream();
              final ByteArrayOutputStream err = new ByteArrayOutputStream()) {
            final CommandLine cmdLine = new CommandLine("mbrola");
            cmdLine.addArgument("-v");
            cmdLine.addArgument(String.valueOf(INDONESIAN_AMPLITUDE / 100f));
            cmdLine.addArgument(new File(mbrolaShareFolder, "id1/id1").toString());
            cmdLine.addArgument("-");
            cmdLine.addArgument("-.wav");
            executor.setStreamHandler(new PumpStreamHandler(wavStream, err, objectIn));
            final int executed;
            try {
              executed = executor.execute(cmdLine);
              wavBytes = wavStream.toByteArray();
            } finally {
              log.info("{}: {}", cmdLine, err.toString());
            }
          }
        } catch (Exception e) {
          log.error(
              "Cannot speak Indonesian using prosody engine, falling back to direct espeak: "
                  + communicateAction.getObject(),
              e);
        }
      }
      if (wavBytes == null) {
        // Neutral speech using direct espeak
        try {
          wavBytes = performEspeak(communicateAction, lang);
        } catch (Exception e) {
          if (!Locale.US.getLanguage().equals(lang.getLanguage())) {
            // Indonesian sometimes fails especially "k-k", e.g. "baik koq".
            // retry using English as last resort, as long as it says something!
            log.error(
                "Cannot speak using "
                    + lang.toLanguageTag()
                    + ", falling back to English (US): "
                    + communicateAction.getObject(),
                e);
            wavBytes = performEspeak(communicateAction, Locale.US);
          } else {
            throw e;
          }
        }
      }

      log.info("espeak/mbrola generated {} bytes WAV", wavBytes.length);
      try (final ByteArrayInputStream wavIn = new ByteArrayInputStream(wavBytes);
          final ByteArrayOutputStream bos = new ByteArrayOutputStream();
          final ByteArrayOutputStream err = new ByteArrayOutputStream()) {
        // flac.exe doesn't support mp3, and that's a problem for now (note: mp3 patent is expiring)
        final CommandLine cmdLine = new CommandLine(ffmpegExecutable);
        cmdLine.addArgument("-i");
        cmdLine.addArgument("-"); // cmdLine.addArgument(wavFile.toString());
        cmdLine.addArgument("-ar");
        cmdLine.addArgument(String.valueOf(SAMPLE_RATE));
        cmdLine.addArgument("-ac");
        cmdLine.addArgument("1");
        cmdLine.addArgument("-f");
        cmdLine.addArgument("ogg");
        // without this you'll get FLAC instead, which browsers do not support
        cmdLine.addArgument("-acodec");
        cmdLine.addArgument("libvorbis");
        //                                cmdLine.addArgument("-y"); // happens, weird!
        //                                cmdLine.addArgument(oggFile.toString());
        cmdLine.addArgument("-");
        executor.setStreamHandler(new PumpStreamHandler(bos, err, wavIn));
        final int executed;
        try {
          executed = executor.execute(cmdLine);
        } finally {
          log.info("{}: {}", cmdLine, err.toString());
        }
        //                                Preconditions.checkState(oggFile.exists(), "Cannot convert
        // %s bytes WAV to OGG",
        //                                        wavBytes.length);

        // Send
        //                                final byte[] audioContent =
        // FileUtils.readFileToByteArray(oggFile);
        final byte[] audioContent = bos.toByteArray();
        final String audioContentType = "audio/ogg";

        final AudioObject audioObject = new AudioObject();
        audioObject.setTranscript(communicateAction.getObject());
        audioObject.setInLanguage(lang);
        audioObject.setMediaLayer(MediaLayer.SPEECH);
        audioObject.setContentType(audioContentType + "; rate=" + SAMPLE_RATE);
        audioObject.setContentUrl(
            "data:" + audioContentType + ";base64," + Base64.encodeBase64String(audioContent));
        audioObject.setContentSize((long) audioContent.length);
        //
        // audioObject.setName(FilenameUtils.getName(oggFile.getName()));
        audioObject.setName("lumen-speech-" + new DateTime() + ".ogg");
        audioObject.setDateCreated(new DateTime());
        audioObject.setDatePublished(audioObject.getDateCreated());
        audioObject.setDateModified(audioObject.getDateCreated());
        audioObject.setUploadDate(audioObject.getDateCreated());
        final String audioOutUri =
            "rabbitmq://dummy/amq.topic?connectionFactory=#amqpConnFactory&exchangeType=topic&autoDelete=false&skipQueueDeclare=true&routingKey="
                + AvatarChannel.AUDIO_OUT.key(avatarId);
        log.info("Sending {} to {} ...", audioObject, audioOutUri);
        producer.sendBodyAndHeader(
            audioOutUri,
            toJson.getMapper().writeValueAsBytes(audioObject),
            RabbitMQConstants.EXPIRATION,
            String.valueOf(MESSAGE_EXPIRATION.getMillis()));
      }
    } finally {
      //                            oggFile.delete();
      // wavFile.delete();
    }

    // reply
    log.trace("Exchange {} is {}", exchange.getIn().getMessageId(), exchange.getPattern());
    final Status status = new Status();
    exchange.getOut().setBody(status);
    return status;
    //                        final String replyTo = exchange.getIn().getHeader("rabbitmq.REPLY_TO",
    // String.class);
    //                        if (replyTo != null) {
    //                            log.debug("Sending reply to {} ...", replyTo);
    //                            exchange.getOut().setHeader("rabbitmq.ROUTING_KEY", replyTo);
    //                            exchange.getOut().setHeader("rabbitmq.EXCHANGE_NAME", "");
    //                            exchange.getOut().setHeader("recipients",
    //
    // "rabbitmq://dummy/dummy?connectionFactory=#amqpConnFactory&autoDelete=false,log:OUT." +
    // LumenChannel.SPEECH_SYNTHESIS);
    //                        } else {
    //                            exchange.getOut().setHeader("recipients");
    //                        }
  }
Exemplo n.º 6
0
  public static Locale getLocale(
      RealmModel realm, UserModel user, UriInfo uriInfo, HttpHeaders httpHeaders) {
    if (!realm.isInternationalizationEnabled()) {
      return Locale.ENGLISH;
    }

    // 0. kc_locale query parameter
    if (uriInfo != null && uriInfo.getQueryParameters().containsKey(KC_LOCALE_PARAM)) {
      String localeString = uriInfo.getQueryParameters().getFirst(KC_LOCALE_PARAM);
      Locale locale = findLocale(realm.getSupportedLocales(), localeString);
      if (locale != null) {
        if (user != null) {
          user.setAttribute(UserModel.LOCALE, locale.toLanguageTag());
        }
        return locale;
      } else {
        LOGGER.infof("Locale %s is not supported.", localeString);
      }
    }

    // 1. Locale cookie
    if (httpHeaders != null && httpHeaders.getCookies().containsKey(LOCALE_COOKIE)) {
      String localeString = httpHeaders.getCookies().get(LOCALE_COOKIE).getValue();
      Locale locale = findLocale(realm.getSupportedLocales(), localeString);
      if (locale != null) {
        if (user != null && user.getAttribute(UserModel.LOCALE) == null) {
          user.setAttribute(UserModel.LOCALE, locale.toLanguageTag());
        }
        return locale;
      } else {
        LOGGER.infof("Locale %s is not supported.", localeString);
      }
    }

    // 2. User profile
    if (user != null && user.getAttributes().containsKey(UserModel.LOCALE)) {
      String localeString = user.getAttribute(UserModel.LOCALE);
      Locale locale = findLocale(realm.getSupportedLocales(), localeString);
      if (locale != null) {

        return locale;
      } else {
        LOGGER.infof("Locale %s is not supported.", localeString);
      }
    }

    // 3. ui_locales query parameter
    if (uriInfo != null && uriInfo.getQueryParameters().containsKey(UI_LOCALES_PARAM)) {
      String localeString = uriInfo.getQueryParameters().getFirst(UI_LOCALES_PARAM);
      Locale locale = findLocale(realm.getSupportedLocales(), localeString.split(" "));
      if (locale != null) {
        return locale;
      } else {
        LOGGER.infof("Locale %s is not supported.", localeString);
      }
    }

    // 4. Accept-Language http header
    if (httpHeaders != null
        && httpHeaders.getAcceptableLanguages() != null
        && !httpHeaders.getAcceptableLanguages().isEmpty()) {
      for (Locale l : httpHeaders.getAcceptableLanguages()) {
        String localeString = l.toLanguageTag();
        Locale locale = findLocale(realm.getSupportedLocales(), localeString);
        if (locale != null) {
          return locale;
        } else {
          LOGGER.infof("Locale %s is not supported.", localeString);
        }
      }
    }

    // 5. Default realm locale
    if (realm.getDefaultLocale() != null) {
      return Locale.forLanguageTag(realm.getDefaultLocale());
    }

    return Locale.ENGLISH;
  }
 public static long openCollator(Locale locale) {
   return openCollator(locale.toLanguageTag());
 }