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();
}
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);
}
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"));
}
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;
}
Aggregations