Search in sources :

Example 6 with Route

use of ninja.Route in project ninja by ninjaframework.

the class InstrumentedNinja method onRouteRequest.

@Override
@Timed
public void onRouteRequest(Context.Impl context) {
    activeRequests.inc();
    String httpMethod = context.getMethod();
    Route route = router.getRouteFor(httpMethod, context.getRequestPath());
    context.setRoute(route);
    if (route != null) {
        allRequestsMeter.mark();
        try {
            Result result = route.getFilterChain().next(context);
            resultHandler.handleResult(result, context);
        } catch (Exception exception) {
            if (exception instanceof BadRequestException) {
                badRequests.mark();
            } else {
                internalServerErrors.mark();
            }
            Result result = onException(context, exception);
            renderErrorResultAndCatchAndLogExceptions(result, context);
        }
    } else {
        // throw a 404 "not found" because we did not find the route
        routesNotFound.mark();
        Result result = getNotFoundResult(context);
        renderErrorResultAndCatchAndLogExceptions(result, context);
    }
    activeRequests.dec();
}
Also used : BadRequestException(ninja.exceptions.BadRequestException) Route(ninja.Route) BadRequestException(ninja.exceptions.BadRequestException) Result(ninja.Result)

Example 7 with Route

use of ninja.Route in project ninja by ninjaframework.

the class TemplateEngineFreemarkerTest method before.

@Before
public void before() throws Exception {
    //Setup that allows to to execute invoke(...) in a very minimal version.
    when(ninjaProperties.getWithDefault(FREEMARKER_CONFIGURATION_FILE_SUFFIX, ".ftl.html")).thenReturn(".ftl.html");
    templateEngineFreemarker = new TemplateEngineFreemarker(messages, lang, logger, templateEngineHelper, templateEngineManager, templateEngineFreemarkerReverseRouteMethod, templateEngineFreemarkerAssetsAtMethod, templateEngineFreemarkerWebJarsAtMethod, ninjaProperties);
    when(lang.getLanguage(any(Context.class), any(Optional.class))).thenReturn(Optional.<String>empty());
    Session session = Mockito.mock(Session.class);
    when(session.isEmpty()).thenReturn(true);
    when(context.getSession()).thenReturn(session);
    when(context.getRoute()).thenReturn(route);
    when(lang.getLocaleFromStringOrDefault(any(Optional.class))).thenReturn(Locale.ENGLISH);
    FlashScope flashScope = Mockito.mock(FlashScope.class);
    Map<String, String> flashScopeData = new HashMap<>();
    when(flashScope.getCurrentFlashCookieData()).thenReturn(flashScopeData);
    when(context.getFlashScope()).thenReturn(flashScope);
    when(templateEngineHelper.getTemplateForResult(any(Route.class), any(Result.class), Mockito.anyString())).thenReturn("views/template.ftl.html");
    writer = new StringWriter();
    ResponseStreams responseStreams = mock(ResponseStreams.class);
    when(context.finalizeHeaders(any(Result.class))).thenReturn(responseStreams);
    when(responseStreams.getWriter()).thenReturn(writer);
}
Also used : Context(ninja.Context) Optional(java.util.Optional) StringWriter(java.io.StringWriter) ResponseStreams(ninja.utils.ResponseStreams) HashMap(java.util.HashMap) FlashScope(ninja.session.FlashScope) Route(ninja.Route) Session(ninja.session.Session) Result(ninja.Result) Before(org.junit.Before)

Example 8 with Route

use of ninja.Route in project ninja by ninjaframework.

the class ApplicationControllerTest method testRouteOrdering.

@Test
public void testRouteOrdering() {
    NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test);
    Provider<RouteBuilderImpl> routeBuilderImplProvider = Mockito.mock(Provider.class);
    when(routeBuilderImplProvider.get()).thenAnswer((invocation) -> new RouteBuilderImpl(ninjaProperties, ninjaBaseDirectoryResolver));
    RouterImpl router = new RouterImpl(injector, ninjaProperties, routeBuilderImplProvider);
    Routes routes = new Routes(ninjaProperties);
    routes.init(router);
    router.compileRoutes();
    Route route0 = router.getRoutes().get(0);
    Assert.assertThat(route0.getUri(), CoreMatchers.equalTo("/base/middle/app/get"));
    Route route8 = router.getRoutes().get(8);
    Assert.assertThat(route8.getUri(), CoreMatchers.equalTo("/base/middle/app/put"));
    Route route16 = router.getRoutes().get(16);
    Assert.assertThat(route16.getUri(), CoreMatchers.equalTo("/base/middle/app/post"));
}
Also used : NinjaPropertiesImpl(ninja.utils.NinjaPropertiesImpl) RouteBuilderImpl(ninja.RouteBuilderImpl) RouterImpl(ninja.RouterImpl) Routes(testapplication.conf.Routes) Route(ninja.Route) Test(org.junit.Test)

Example 9 with Route

use of ninja.Route in project ninja by ninjaframework.

the class DiagnosticErrorRenderer method appendContext.

private DiagnosticErrorRenderer appendContext(Context context) throws IOException {
    s.append("<div class=\"context\">\n");
    s.append("<h2>Route</h2>\n");
    if (context.getRoute() != null) {
        Route route = context.getRoute();
        appendNameValue(s, "Http method", route.getHttpMethod());
        appendNameValue(s, "Controller method", route.getControllerClass().getCanonicalName() + "." + route.getControllerMethod().getName() + "()");
        StringBuilder params = new StringBuilder();
        for (Class type : route.getControllerMethod().getParameterTypes()) {
            if (params.length() > 0) {
                params.append(", ");
            }
            params.append(type.getCanonicalName());
        }
        appendNameValue(s, "Controller parameters", params.toString());
    } else {
        appendNoValues(s);
    }
    s.append("<h2>Session</h2>\n");
    if (context.getSession() != null && !context.getSession().getData().isEmpty()) {
        for (Map.Entry<String, String> sessionEntry : context.getSession().getData().entrySet()) {
            appendNameValue(s, sessionEntry.getKey(), sessionEntry.getValue());
        }
    } else {
        appendNoValues(s);
    }
    s.append("<h2>Flash</h2>\n");
    if (context.getFlashScope() != null && !context.getFlashScope().getCurrentFlashCookieData().isEmpty()) {
        for (Map.Entry<String, String> sessionEntry : context.getFlashScope().getCurrentFlashCookieData().entrySet()) {
            appendNameValue(s, sessionEntry.getKey(), sessionEntry.getValue());
        }
    } else {
        appendNoValues(s);
    }
    s.append("<h2>Attributes</h2>\n");
    Map<String, Object> attributes = context.getAttributes();
    if (attributes != null && !attributes.isEmpty()) {
        for (Map.Entry<String, Object> entry : attributes.entrySet()) {
            appendNameValue(s, entry.getKey(), (entry.getValue() != null ? entry.getValue().toString() : "null"));
        }
    } else {
        appendNoValues(s);
    }
    List<Cookie> cookies = context.getCookies();
    if (cookies == null || cookies.isEmpty()) {
        s.append("<h2>Cookies</h2>\n");
        appendNoValues(s);
    } else {
        for (Cookie cookie : context.getCookies()) {
            s.append("<h2>Cookie: ").append(cookie.getName()).append("</h2>\n");
            appendNameValue(s, "Value", cookie.getValue());
            appendNameValue(s, "Path", cookie.getPath());
            appendNameValue(s, "Domain", cookie.getDomain());
            appendNameValue(s, "HTTP only", cookie.isHttpOnly() + "");
            appendNameValue(s, "Secure", cookie.isSecure() + "");
            appendNameValue(s, "Max age", cookie.getMaxAge() + "");
            appendNameValue(s, "Comment", cookie.getComment());
        }
    }
    s.append("</div>\n");
    return this;
}
Also used : Cookie(ninja.Cookie) Map(java.util.Map) Route(ninja.Route)

Aggregations

Route (ninja.Route)9 Test (org.junit.Test)5 NinjaPropertiesImpl (ninja.utils.NinjaPropertiesImpl)3 Method (java.lang.reflect.Method)2 Bootstrap (ninja.Bootstrap)2 Context (ninja.Context)2 Result (ninja.Result)2 Router (ninja.Router)2 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Cookie (ninja.Cookie)1 RouteBuilderImpl (ninja.RouteBuilderImpl)1 RouterImpl (ninja.RouterImpl)1 BadRequestException (ninja.exceptions.BadRequestException)1 FlashScope (ninja.session.FlashScope)1 Session (ninja.session.Session)1 ResponseStreams (ninja.utils.ResponseStreams)1 Before (org.junit.Before)1