public static Request get() { RouteContext routeContext = RouteDispatcher.getRouteContext(); return (routeContext != null) ? routeContext.getRequest() : null; }
@Override public void handle(RouteContext context) { String rawMethod = context.getRequest().getHttpServletRequest().getMethod(); if (HttpConstants.Method.POST.equals(rawMethod)) { String contentType = new ParameterValue(context.getRequest().getLocale(), context.getHeader("Content-Type")).toString("").toLowerCase(); contentType = StringUtils.getPrefix(contentType, ';').trim(); if (!guardedTypes.contains(contentType)) {
public Request getRequest() { return getRouteContext().getRequest(); }
protected void processRouteInterceptors(RouteContext routeContext) { if (interceptors.isEmpty()) { return; } List<RouteMatch> chain = new ArrayList<>(); for (RouteHandler interceptor : interceptors) { // create a route for interceptor Route route = new Route(routeContext.getRequestMethod(), routeContext.getRequestUri(), interceptor); route.setName(StringUtils.format("{}<{}>", Interceptor.class.getSimpleName(), route.getRouteHandler().getClass().getSimpleName())); route.bindAll(routeContext.getRoute().getAttributes()); // add route in chain RouteMatch match = new RouteMatch(route, null); chain.add(match); } // TODO DefaultRouteContext is hardcoded RouteContext context = new DefaultRouteContext(routeContext.getApplication(), routeContext.getRequest(), routeContext.getResponse(), chain); context.next(); }
public Request getRequest() { return getRouteContext().getRequest(); }
@Override public Object extract(MethodParameter parameter, RouteContext routeContext) { String name = parameter.getParameterName(); return routeContext.getRequest().getFile(name); }
public static Session get() { RouteContext routeContext = RouteDispatcher.getRouteContext(); return (routeContext != null) ? routeContext.getRequest().getSession(false) : null; }
Set<String> contentTypes = getContentTypes(routeContext.getRequest()); if (types.isEmpty()) { types.addAll(getAcceptTypes(routeContext.getRequest()));
protected List<DirEntry> getDirEntries(RouteContext routeContext, File dir, String absoluteDirUri) { List<DirEntry> list = new ArrayList<>(); for (File file : getFiles(dir)) { String fileUrl = routeContext.getRequest().getApplicationPath() + StringUtils.removeEnd(StringUtils.addStart(absoluteDirUri, "/"), "/") + StringUtils.addStart(file.getName(), "/"); list.add(new DirEntry(fileUrl, file)); } if (comparator != null) { list.sort(comparator); } if (!directory.equals(dir)) { File upDir = new File(dir, "../"); list.add(0, new DirEntry(routeContext.getRequest().getApplicationPath() + StringUtils.removeEnd(StringUtils.addStart(absoluteDirUri, "/"), "/") + StringUtils.addStart(upDir.getName(), "/"), upDir)); } return list; }
@Override public void handle(RouteContext routeContext) { // get request path Request request = routeContext.getRequest(); String path = request.getPath(); // add or remove trailing slash if (path.length() > 1) { if (addSlash) { path = StringUtils.addEnd(path, "/"); } else { path = StringUtils.removeEnd(path, "/"); } } if (!path.equals(request.getPath())) { // redirect routeContext.redirect(path); } else { // continue with the next handler routeContext.next(); } }
cookie = routeContext.getRequest().getCookie(cookieName); if (cookie != null && !StringUtils.isNullOrEmpty(cookie.getValue())) { return getLanguageOrDefault(cookie.getValue());
protected void streamResource(URL resourceUrl, RouteContext routeContext) { try { setResponseHeaders(resourceUrl, routeContext); if (routeContext.getResponse().getStatus() == HttpConstants.StatusCode.NOT_MODIFIED) { // do not stream anything out, simply return 304 routeContext.getResponse().commit(); } else { sendResource(resourceUrl, routeContext); } } catch (IOException e) { String message = e.getMessage(); if (!StringUtils.isNullOrEmpty(message)) { log.warn("Error sending resource {} to {}: {}", resourceUrl, routeContext.getRequest().getClientIp(), message); } else { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } } catch (Exception e) { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } }
@Override @SuppressWarnings("unchecked") public Object extract(MethodParameter parameter, RouteContext routeContext) { Header annotation = parameter.getAnnotation(Header.class); String name = getParameterName(parameter, annotation); String pattern = annotation.pattern(); ParameterValue parameterValue = new ParameterValue(routeContext.getRequest().getLocale(), routeContext.getHeader(name)); Class<?> parameterType = parameter.getParameterType(); if (Collection.class.isAssignableFrom(parameterType)) { Class<? extends Collection> collectionType = (Class<? extends Collection>) parameterType; Class<?> objectType = parameter.getParameterGenericType(); return parameterValue.toCollection(collectionType, objectType, pattern); } return parameterValue.to(parameterType, pattern); }
if (!StringUtils.isNullOrEmpty(routeContext.getRequest().getAcceptType())) { String acceptType = routeContext.getRequest().getAcceptType(); if (acceptType.startsWith(HttpConstants.ContentType.TEXT_HTML) || acceptType.startsWith(HttpConstants.ContentType.TEXT_XHTML)) {
public Request getRequest() { return getRouteContext().getRequest(); }
public class PippoApplication extends Application { private final static Logger log = LoggerFactory.getLogger(PippoApplication.class); @Override protected void onInit() { POST("/", new RouteHandler() { @Override public void handle(RouteContext routeContext) { System.out.println(routeContext.getRequest().getBody()); routeContext.send("OK"); } }); }
@Override public Object extract(MethodParameter parameter, RouteContext routeContext) { String name = parameter.getParameterName(); return routeContext.getRequest().getFile(name); }
@Override @SuppressWarnings("unchecked") public Object extract(MethodParameter parameter, RouteContext routeContext) { Header annotation = parameter.getAnnotation(Header.class); String name = getParameterName(parameter, annotation); String pattern = annotation.pattern(); ParameterValue parameterValue = new ParameterValue(routeContext.getRequest().getLocale(), routeContext.getHeader(name)); Class<?> parameterType = parameter.getParameterType(); if (Collection.class.isAssignableFrom(parameterType)) { Class<? extends Collection> collectionType = (Class<? extends Collection>) parameterType; Class<?> objectType = parameter.getParameterGenericType(); return parameterValue.toCollection(collectionType, objectType, pattern); } return parameterValue.to(parameterType, pattern); }
private void initializeApplication() { // hook-up the Metrics dispatch listener MetricsDispatchListener metricsDispatchListener = new MetricsDispatchListener(metricRegistry); application.getRoutePreDispatchListeners().add(metricsDispatchListener); application.getRoutePostDispatchListeners().add(metricsDispatchListener); // set the RedirectException handler application.getErrorHandler().setExceptionHandler(RedirectException.class, (exception, ctx) -> { RedirectException redirectException = (RedirectException) exception; if (!Strings.isNullOrEmpty(redirectException.getSessionUrlAttribute())) { String requestUri = ctx.getRequest().getApplicationUriWithQuery(); ctx.setSession(redirectException.getSessionUrlAttribute(), requestUri); } ctx.redirect(redirectException.getPath()); }); // set the StatusCodeException handler application.getErrorHandler().setExceptionHandler(StatusCodeException.class, (exception, ctx) -> { StatusCodeException statusCodeException = (StatusCodeException) exception; ctx.setLocal("message", statusCodeException.getMessage()); application.getErrorHandler().handle(statusCodeException.getStatusCode(), ctx); }); }
private void initializeApplication() { // hook-up the Metrics dispatch listener MetricsDispatchListener metricsDispatchListener = new MetricsDispatchListener(metricRegistry); application.getRoutePreDispatchListeners().add(metricsDispatchListener); application.getRoutePostDispatchListeners().add(metricsDispatchListener); // set the RedirectException handler application.getErrorHandler().setExceptionHandler(RedirectException.class, (exception, ctx) -> { RedirectException redirectException = (RedirectException) exception; if (!Strings.isNullOrEmpty(redirectException.getSessionUrlAttribute())) { String requestUri = ctx.getRequest().getApplicationUriWithQuery(); ctx.setSession(redirectException.getSessionUrlAttribute(), requestUri); } ctx.redirect(redirectException.getPath()); }); // set the StatusCodeException handler application.getErrorHandler().setExceptionHandler(StatusCodeException.class, (exception, ctx) -> { StatusCodeException statusCodeException = (StatusCodeException) exception; ctx.setLocal("message", statusCodeException.getMessage()); application.getErrorHandler().handle(statusCodeException.getStatusCode(), ctx); }); }