Search in sources :

Example 1 with Message

use of ninja.utils.Message in project ninja by ninjaframework.

the class NinjaDefault method getNotFoundResult.

@Override
public Result getNotFoundResult(Context context) {
    if (isDiagnosticsEnabled()) {
        DiagnosticError diagnosticError = DiagnosticErrorBuilder.build404NotFoundDiagnosticError(true);
        return Results.notFound().render(diagnosticError);
    }
    String messageI18n = messages.getWithDefault(NinjaConstant.I18N_NINJA_SYSTEM_NOT_FOUND_TEXT_KEY, NinjaConstant.I18N_NINJA_SYSTEM_NOT_FOUND_TEXT_DEFAULT, context, Optional.<Result>empty());
    Message message = new Message(messageI18n);
    Result result = Results.notFound().supportedContentTypes(Result.TEXT_HTML, Result.APPLICATION_JSON, Result.APPLICATION_XML).fallbackContentType(Result.TEXT_HTML).render(message).template(ninjaProperties.getWithDefault(NinjaConstant.LOCATION_VIEW_HTML_NOT_FOUND_KEY, NinjaConstant.LOCATION_VIEW_FTL_HTML_NOT_FOUND));
    return result;
}
Also used : DiagnosticError(ninja.diagnostics.DiagnosticError) Message(ninja.utils.Message)

Example 2 with Message

use of ninja.utils.Message in project ninja by ninjaframework.

the class NinjaDefault method getInternalServerErrorResult.

public Result getInternalServerErrorResult(Context context, Exception exception, Result underlyingResult) {
    if (isDiagnosticsEnabled()) {
        DiagnosticError diagnosticError = DiagnosticErrorBuilder.build500InternalServerErrorDiagnosticError(exception, true, underlyingResult);
        return Results.internalServerError().render(diagnosticError);
    }
    logger.error("Emitting bad request 500. Something really wrong when calling route: {} (class: {} method: {})", context.getRequestPath(), context.getRoute().getControllerClass(), context.getRoute().getControllerMethod(), exception);
    String messageI18n = messages.getWithDefault(NinjaConstant.I18N_NINJA_SYSTEM_INTERNAL_SERVER_ERROR_TEXT_KEY, NinjaConstant.I18N_NINJA_SYSTEM_INTERNAL_SERVER_ERROR_TEXT_DEFAULT, context, Optional.<Result>empty());
    Message message = new Message(messageI18n);
    Result result = Results.internalServerError().supportedContentTypes(Result.TEXT_HTML, Result.APPLICATION_JSON, Result.APPLICATION_XML).fallbackContentType(Result.TEXT_HTML).render(message).template(ninjaProperties.getWithDefault(NinjaConstant.LOCATION_VIEW_HTML_INTERNAL_SERVER_ERROR_KEY, NinjaConstant.LOCATION_VIEW_FTL_HTML_INTERNAL_SERVER_ERROR));
    return result;
}
Also used : DiagnosticError(ninja.diagnostics.DiagnosticError) Message(ninja.utils.Message)

Example 3 with Message

use of ninja.utils.Message in project ninja by ninjaframework.

the class NinjaDefault method getForbiddenResult.

@Override
public Result getForbiddenResult(Context context) {
    // diagnostic mode
    if (isDiagnosticsEnabled()) {
        DiagnosticError diagnosticError = DiagnosticErrorBuilder.build403ForbiddenDiagnosticError();
        return Results.forbidden().render(diagnosticError);
    }
    String messageI18n = messages.getWithDefault(NinjaConstant.I18N_NINJA_SYSTEM_FORBIDDEN_REQUEST_TEXT_KEY, NinjaConstant.I18N_NINJA_SYSTEM_FORBIDDEN_REQUEST_TEXT_DEFAULT, context, Optional.<Result>empty());
    Message message = new Message(messageI18n);
    Result result = Results.forbidden().supportedContentTypes(Result.TEXT_HTML, Result.APPLICATION_JSON, Result.APPLICATION_XML).fallbackContentType(Result.TEXT_HTML).render(message).template(ninjaProperties.getWithDefault(NinjaConstant.LOCATION_VIEW_HTML_FORBIDDEN_KEY, NinjaConstant.LOCATION_VIEW_FTL_HTML_FORBIDDEN));
    return result;
}
Also used : DiagnosticError(ninja.diagnostics.DiagnosticError) Message(ninja.utils.Message)

Example 4 with Message

use of ninja.utils.Message in project ninja by ninjaframework.

the class NotFoundTest method testThatNotFoundWorksXml.

@Test
public void testThatNotFoundWorksXml() {
    Response response = makeRequest(Request.GET().url(testServerUrl().path("/_non_existing_url")).addHeader(HttpConstants.HEADER_ACCEPT, HttpConstants.APPLICATION_XML));
    assertEquals(404, response.httpStatus);
    Message message = response.payloadXmlAs(Message.class);
    Assert.assertThat(message.text, CoreMatchers.equalTo(NinjaConstant.I18N_NINJA_SYSTEM_NOT_FOUND_TEXT_DEFAULT));
}
Also used : Response(org.doctester.testbrowser.Response) Message(ninja.utils.Message) Test(org.junit.Test)

Example 5 with Message

use of ninja.utils.Message in project ninja by ninjaframework.

the class NinjaDefaultTest method getInternalServerErrorResult.

@Test
public void getInternalServerErrorResult() throws Exception {
    when(ninjaProperties.getWithDefault(Matchers.eq(NinjaConstant.LOCATION_VIEW_HTML_INTERNAL_SERVER_ERROR_KEY), Matchers.eq(NinjaConstant.LOCATION_VIEW_FTL_HTML_INTERNAL_SERVER_ERROR))).thenReturn(NinjaConstant.LOCATION_VIEW_FTL_HTML_INTERNAL_SERVER_ERROR);
    // real test:
    Result result = ninjaDefault.getInternalServerErrorResult(contextImpl, new Exception("not important"));
    assertThat(result.getStatusCode(), equalTo(Result.SC_500_INTERNAL_SERVER_ERROR));
    assertThat(result.getTemplate(), equalTo(NinjaConstant.LOCATION_VIEW_FTL_HTML_INTERNAL_SERVER_ERROR));
    assertTrue(result.getRenderable() instanceof Message);
    verify(messages).getWithDefault(Matchers.eq(NinjaConstant.I18N_NINJA_SYSTEM_INTERNAL_SERVER_ERROR_TEXT_KEY), Matchers.eq(NinjaConstant.I18N_NINJA_SYSTEM_INTERNAL_SERVER_ERROR_TEXT_DEFAULT), Matchers.eq(contextImpl), any(Optional.class));
    verify(ninjaProperties).getWithDefault(Matchers.eq(NinjaConstant.LOCATION_VIEW_HTML_INTERNAL_SERVER_ERROR_KEY), Matchers.eq(NinjaConstant.LOCATION_VIEW_FTL_HTML_INTERNAL_SERVER_ERROR));
}
Also used : Message(ninja.utils.Message) Optional(java.util.Optional) BadRequestException(ninja.exceptions.BadRequestException) InternalServerErrorException(ninja.exceptions.InternalServerErrorException) Test(org.junit.Test)

Aggregations

Message (ninja.utils.Message)9 DiagnosticError (ninja.diagnostics.DiagnosticError)5 Test (org.junit.Test)4 Optional (java.util.Optional)2 BadRequestException (ninja.exceptions.BadRequestException)2 Response (org.doctester.testbrowser.Response)2 InternalServerErrorException (ninja.exceptions.InternalServerErrorException)1