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; }
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); }
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; }
@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); }
@SuppressWarnings("unchecked") public void setup(Languages languages, Messages messages, Router router) { this.languages = languages; this.messages = messages; this.router = router; // set global template variables getModel().put("contextPath", router.getContextPath()); getModel().put("appPath", router.getApplicationPath()); String language = (String) getModel().get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(language); } this.language = language; // prepare the locale Locale locale = (Locale) getModel().get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = languages.getLocaleOrDefault(language); } this.locale = locale; }
@Override public void init(Application application) { super.init(application); Router router = getRouter(); PippoSettings pippoSettings = getPippoSettings(); configuration = new JadeConfiguration(); configuration.setTemplateLoader(new ClassTemplateLoader(JadeTemplateEngine.class, getTemplatePathPrefix())); configuration.setMode(Mode.HTML); if (pippoSettings.isDev()) { configuration.setPrettyPrint(true); configuration.setCaching(false); // disable cache } // set global template variables configuration.getSharedVariables().put("contextPath", router.getContextPath()); configuration.getSharedVariables().put("appPath", router.getApplicationPath()); // allow custom initialization init(application, configuration); }
configuration.setSharedVariable("contextPath", new SimpleScalar(router.getContextPath())); configuration.setSharedVariable("appPath", new SimpleScalar(router.getApplicationPath()));
.strictVariables(false) .extension(new GlobalVariablesExtension() .set("contextPath", router.getContextPath()) .set("appPath", router.getApplicationPath())) .extension(new I18nExtension(application.getMessages()))
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; }
builder.addGlobalData("contextPath", router.getContextPath()); builder.addGlobalData("appPath", router.getApplicationPath());
@SuppressWarnings("unchecked") public void setup(Languages languages, Messages messages, Router router) { this.languages = languages; this.messages = messages; this.router = router; // set global template variables getModel().put("contextPath", router.getContextPath()); getModel().put("appPath", router.getApplicationPath()); String language = (String) getModel().get(PippoConstants.REQUEST_PARAMETER_LANG); if (StringUtils.isNullOrEmpty(language)) { language = languages.getLanguageOrDefault(language); } this.language = language; // prepare the locale Locale locale = (Locale) getModel().get(PippoConstants.REQUEST_PARAMETER_LOCALE); if (locale == null) { locale = languages.getLocaleOrDefault(language); } this.locale = locale; }
@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 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); }
builder.addGlobalData("contextPath", router.getContextPath()); builder.addGlobalData("appPath", router.getApplicationPath());