Search in sources :

Example 1 with BadRequestException

use of io.helidon.webserver.BadRequestException in project helidon by oracle.

the class TranslatorFrontendService method getText.

private void getText(ServerRequest request, ServerResponse response) {
    try {
        String query = request.queryParams().first("q").orElseThrow(() -> new BadRequestException("missing query parameter 'q'"));
        String language = request.queryParams().first("lang").orElseThrow(() -> new BadRequestException("missing query parameter 'lang'"));
        try (Response backendResponse = backendTarget.property(ClientTracingFilter.TRACER_PROPERTY_NAME, request.tracer()).property(ClientTracingFilter.CURRENT_SPAN_CONTEXT_PROPERTY_NAME, request.spanContext().orElse(null)).queryParam("q", query).queryParam("lang", language).request().get()) {
            final String result;
            if (backendResponse.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) {
                result = backendResponse.readEntity(String.class);
            } else {
                result = "Error: " + backendResponse.readEntity(String.class);
            }
            response.send(result + "\n");
        }
    } catch (ProcessingException pe) {
        LOGGER.log(Level.WARNING, "Problem to call translator frontend.", pe);
        response.status(503).send("Translator backend service isn't available.");
    }
}
Also used : Response(jakarta.ws.rs.core.Response) ServerResponse(io.helidon.webserver.ServerResponse) BadRequestException(io.helidon.webserver.BadRequestException) ProcessingException(jakarta.ws.rs.ProcessingException)

Example 2 with BadRequestException

use of io.helidon.webserver.BadRequestException in project helidon by oracle.

the class TranslatorBackendService method getText.

private void getText(ServerRequest request, ServerResponse response) {
    String query = request.queryParams().first("q").orElseThrow(() -> new BadRequestException("missing query parameter 'q'"));
    String language = request.queryParams().first("lang").orElseThrow(() -> new BadRequestException("missing query parameter 'lang'"));
    String translation;
    switch(language) {
        case CZECH:
            translation = TRANSLATED_WORDS_REPOSITORY.get(query + SEPARATOR + CZECH);
            break;
        case SPANISH:
            translation = TRANSLATED_WORDS_REPOSITORY.get(query + SEPARATOR + SPANISH);
            break;
        case CHINESE:
            translation = TRANSLATED_WORDS_REPOSITORY.get(query + SEPARATOR + CHINESE);
            break;
        case HINDI:
            translation = TRANSLATED_WORDS_REPOSITORY.get(query + SEPARATOR + HINDI);
            break;
        case ITALIAN:
            translation = TRANSLATED_WORDS_REPOSITORY.get(query + SEPARATOR + ITALIAN);
            break;
        case FRENCH:
            translation = TRANSLATED_WORDS_REPOSITORY.get(query + SEPARATOR + FRENCH);
            break;
        default:
            response.status(404).send(String.format("Language '%s' not in supported. Supported languages: %s, %s, %s, %s.", language, CZECH, SPANISH, CHINESE, HINDI));
            return;
    }
    if (translation != null) {
        response.send(translation);
    } else {
        response.status(404).send(String.format("Word '%s' not in the dictionary", query));
    }
}
Also used : BadRequestException(io.helidon.webserver.BadRequestException)

Aggregations

BadRequestException (io.helidon.webserver.BadRequestException)2 ServerResponse (io.helidon.webserver.ServerResponse)1 ProcessingException (jakarta.ws.rs.ProcessingException)1 Response (jakarta.ws.rs.core.Response)1