private VelocityContext createVelocityContext(Map<String, Object> model) { // prepare the locale-aware i18n method String language = (String) model.get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(language); } // prepare the locale-aware prettyTime method Locale locale = (Locale) model.get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = languages.getLocaleOrDefault(language); } VelocityContext context = new VelocityContext(model); context.put("pippo", new PippoHelper(messages, language, locale, router)); context.put("contextPath", router.getContextPath()); context.put("appPath", router.getApplicationPath()); return context; }
private String classpathResourceAt(String path, AtomicReference<String> patternRef, Class<? extends ClasspathResourceHandler> resourceHandlerClass) { if (patternRef.get() == null) { String pattern = router.uriPatternFor(resourceHandlerClass); if (pattern == null) { throw new PippoRuntimeException("You must register a route for {}", resourceHandlerClass.getSimpleName()); } patternRef.set(pattern); } Map<String, Object> parameters = new HashMap<>(); parameters.put(ClasspathResourceHandler.PATH_PARAMETER, path); return router.uriFor(patternRef.get(), parameters); }
@Override public void addRoute(Route route) { getRouter().addRoute(route); }
@Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest; HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse; // set the application path from the servlet request since we don't know it at initialization String applicationPath = application.getRouter().getContextPath() + httpServletRequest.getServletPath(); application.getRouter().setApplicationPath(applicationPath); // create Request, Response objects RequestResponseFactory requestResponseFactory = application.getRequestResponseFactory(); RequestResponse requestResponse = requestResponseFactory.createRequestResponse(httpServletRequest, httpServletResponse); Request request = requestResponse.getRequest(); Response response = requestResponse.getResponse(); // create a URI to automatically decode the path URI uri = URI.create(httpServletRequest.getRequestURL().toString()); String requestUri = uri.getPath(); String requestPath = request.getPath(); log.trace("The relative path for '{}' is '{}'", requestUri, requestPath); log.debug("Request {} '{}'", request.getMethod(), requestPath); // dispatch route(s) routeDispatcher.dispatch(request, response); }
@Override protected void setup() { String basePath = Strings.nullToEmpty(getSettings().getString(RestServlet.SETTING_URL, null)).trim(); serve(basePath + "/*").with(RestServlet.class); final PippoSettings pippoSettings = getPippoSettings(getSettings()); final Application application = new Application(pippoSettings); // must set context path before starting application application.getRouter().setContextPath(getSettings().getContextPath()); // must set application path before starting application String contextPath = application.getRouter().getContextPath(); String applicationPath = StringUtils.addEnd(contextPath, "/") + StringUtils.removeStart(basePath, "/"); application.getRouter().setApplicationPath(applicationPath); // start the application which sets up all initializers application.init(); bind(Application.class).toInstance(application); bind(Router.class).toInstance(application.getRouter()); bind(Messages.class).toInstance(application.getMessages()); bind(Languages.class).toInstance(application.getLanguages()); bind(MimeTypes.class).toInstance(application.getMimeTypes()); bind(ErrorHandler.class).toInstance(application.getErrorHandler()); bind(TemplateEngine.class).toInstance(application.getTemplateEngine()); bind(HttpCacheToolkit.class).toInstance(application.getHttpCacheToolkit()); bind(ContentTypeEngines.class).toInstance(application.getContentTypeEngines()); bind(RestService.class); }
@Override public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) { String nameOrUriPattern = (String) args.get("nameOrUriPattern"); Map<String, Object> parameters = (Map<String, Object>) args.get("parameters"); return router.uriFor(nameOrUriPattern, parameters); }
@Override public void start() { initializeApplication(); String border = Strings.padEnd("", Constants.MIN_BORDER_LENGTH, '-'); Optional<String> applicationPackage = Optional.fromNullable(settings.getApplicationPackage()); log.info(border); log.info("Registered engines"); log.info(border); logEngines(); log.debug(border); log.debug("Initializing router"); log.debug(border); initializeRouter(applicationPackage); log.info(""); log.info(border); log.info("RESTful routes ({}) served on base path '{}'", router.getRoutes().size(), Strings.isNullOrEmpty(router.getApplicationPath()) ? "/" : router.getApplicationPath()); log.info(border); logRoutes(router); isRunning = true; }
String webJarsUri = router.uriPatternFor(WebjarsResourceHandler.class); if (webJarsUri == null) { WebjarsResourceHandler webjars = new WebjarsResourceHandler(); router.addRoute(new Route(HttpMethod.GET, webjars.getUriPattern(), webjars));
@Override public void redirect(String nameOrUriPattern, Map<String, Object> parameters) { String uri = uriFor(nameOrUriPattern, parameters); String applicationPath = application.getRouter().getApplicationPath(); if (!applicationPath.isEmpty()) { // remove application path uri = StringUtils.removeStart(uri.substring(applicationPath.length()), "/"); } redirect(uri); }
protected WebSocketRouter.WebSocketMatch findWebSocketRoute(String requestUri) { String applicationPath = getApplication().getRouter().getContextPath(); String path = applicationPath.isEmpty() ? requestUri : requestUri.substring(applicationPath.length()); if (StringUtils.isNullOrEmpty(path)) { path = "/"; } return getApplication().getWebSocketRouter().match(path); }
/** * Get the template bindings for the error response. * * @param routeContext * @return bindings map */ protected Map<String, Object> prepareTemplateBindings(int statusCode, RouteContext routeContext) { Map<String, Object> locals = new LinkedHashMap<>(); locals.put("applicationName", application.getApplicationName()); locals.put("applicationVersion", application.getApplicationVersion()); locals.put("runtimeMode", application.getPippoSettings().getRuntimeMode().toString()); if (application.getPippoSettings().isDev()) { locals.put("routes", application.getRouter().getRoutes()); } return locals; }
application.getRouter().setContextPath(contextPath); application.getRouter().setApplicationPath(applicationPath);
@Override protected void setup() { String basePath = Strings.nullToEmpty(getSettings().getString(RestServlet.SETTING_URL, null)).trim(); serve(basePath + "/*").with(RestServlet.class); final PippoSettings pippoSettings = getPippoSettings(getSettings()); final Application application = new Application(pippoSettings); // must set context path before starting application application.getRouter().setContextPath(getSettings().getContextPath()); // must set application path before starting application String contextPath = application.getRouter().getContextPath(); String applicationPath = StringUtils.addEnd(contextPath, "/") + StringUtils.removeStart(basePath, "/"); application.getRouter().setApplicationPath(applicationPath); // start the application which sets up all initializers application.init(); bind(Application.class).toInstance(application); bind(Router.class).toInstance(application.getRouter()); bind(Messages.class).toInstance(application.getMessages()); bind(Languages.class).toInstance(application.getLanguages()); bind(MimeTypes.class).toInstance(application.getMimeTypes()); bind(ErrorHandler.class).toInstance(application.getErrorHandler()); bind(TemplateEngine.class).toInstance(application.getTemplateEngine()); bind(HttpCacheToolkit.class).toInstance(application.getHttpCacheToolkit()); bind(ContentTypeEngines.class).toInstance(application.getContentTypeEngines()); bind(RestService.class); }
@Override public String uriFor(String nameOrUriPattern, Map<String, Object> parameters) { return application.getRouter().uriFor(nameOrUriPattern, parameters); }
@Override public void start() { initializeApplication(); String border = Strings.padEnd("", Constants.MIN_BORDER_LENGTH, '-'); Optional<String> applicationPackage = Optional.fromNullable(settings.getApplicationPackage()); log.info(border); log.info("Registered engines"); log.info(border); logEngines(); log.debug(border); log.debug("Initializing router"); log.debug(border); initializeRouter(applicationPackage); log.info(""); log.info(border); log.info("RESTful routes ({}) served on base path '{}'", router.getRoutes().size(), Strings.isNullOrEmpty(router.getApplicationPath()) ? "/" : router.getApplicationPath()); log.info(border); logRoutes(router); isRunning = true; }
String webJarsUri = router.uriPatternFor(WebjarsResourceHandler.class); if (webJarsUri == null) { WebjarsResourceHandler webjars = new WebjarsResourceHandler(); router.addRoute(new Route(HttpMethod.GET, webjars.getUriPattern(), webjars));
public Request(HttpServletRequest servletRequest, Application application) { this.httpServletRequest = servletRequest; this.contentTypeEngines = application.getContentTypeEngines(); applicationPath = application.getRouter().getApplicationPath(); // fill (query&post) parameters if any initParameters(); // empty path parameters for now (see internalSetPathParameters method) pathParameters = Collections.unmodifiableMap(new HashMap<String, ParameterValue>()); // init all parameters initAllParameters(); }
content.append('\n'); List<Route> routes = application.getRouter().getRoutes(); for (Route route : routes) { content.append('\t').append(route.getRequestMethod()).append(' ').append(route.getUriPattern());
public Response(HttpServletResponse httpServletResponse, Application application) { this.httpServletResponse = httpServletResponse; this.contentTypeEngines = application.getContentTypeEngines(); this.templateEngine = application.getTemplateEngine(); this.httpServletResponse.setCharacterEncoding(StandardCharsets.UTF_8.toString()); this.contextPath = application.getRouter().getContextPath(); this.applicationPath = StringUtils.removeEnd(application.getRouter().getApplicationPath(), "/"); this.mimeTypes = application.getMimeTypes(); this.status = 0; }
private String classpathResourceAt(String path, AtomicReference<String> patternRef, Class<? extends ClasspathResourceHandler> resourceHandlerClass) { if (patternRef.get() == null) { String pattern = router.uriPatternFor(resourceHandlerClass); if (pattern == null) { throw new PippoRuntimeException("You must register a route for {}", resourceHandlerClass.getSimpleName()); } patternRef.set(pattern); } Map<String, Object> parameters = new HashMap<>(); parameters.put(ClasspathResourceHandler.PATH_PARAMETER, path); return router.uriFor(patternRef.get(), parameters); }