@SuppressWarnings("unchecked") public <T extends ControllerApplication> T getApplication() { return (T) getRouteContext().getApplication(); }
/** * Returns not null only in the context of the web layer (on a HTTP request). * It cannot be useful in a service (server side business layer). * For example if want to have access to PippoSettings from a service you must to inject PippoSettings * in that service and not to use Application.get().getPippoSettings(). * * @return The application instance or null */ public static Application get() { RouteContext routeContext = RouteDispatcher.getRouteContext(); return (routeContext != null) ? routeContext.getApplication() : null; }
protected void setResponseHeaders(URL resourceUrl, RouteContext routeContext) { try { long lastModified = resourceUrl.openConnection().getLastModified(); routeContext.getApplication().getHttpCacheToolkit().addEtag(routeContext, lastModified); String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { routeContext.getResponse().contentType(mimeType); } } catch (Exception e) { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } }
protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { // stream the resource log.debug("Streaming as resource '{}'", resourceUrl); routeContext.getResponse().contentType(mimeType); routeContext.getResponse().ok().resource(resourceUrl.openStream()); } else { // stream the file log.debug("Streaming as file '{}'", resourceUrl); routeContext.getResponse().ok().file(filename, resourceUrl.openStream()); } }
protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { // stream the resource log.debug("Streaming as resource '{}'", resourceUrl); routeContext.getResponse().ok().chunked(chunked).resource(resourceUrl.openStream()); } else { // stream the file log.debug("Streaming as file '{}'", resourceUrl); routeContext.getResponse().ok().chunked(chunked).file(filename, resourceUrl.openStream()); } }
protected void streamResource(URL resourceUrl, RouteContext routeContext) { try { long lastModified = resourceUrl.openConnection().getLastModified(); routeContext.getApplication().getHttpCacheToolkit().addEtag(routeContext, lastModified); if (routeContext.getResponse().getStatus() == HttpConstants.StatusCode.NOT_MODIFIED) { // do not stream anything out, simply return 304 routeContext.getResponse().commit(); } else { sendResource(resourceUrl, routeContext); } } catch (Exception e) { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } }
@Override protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { try { // compile sass to css log.trace("Send css for '{}'", resourceUrl); ScssContext.UrlMode urlMode = ScssContext.UrlMode.ABSOLUTE; String identifier = resourceUrl.getFile(); ScssStylesheet scssStylesheet = ScssStylesheet.get(identifier); if (scssStylesheet == null) { throw new Exception("ScssStylesheet is null for '" + identifier + "'"); } String content = scssStylesheet.toString(); String result = sourceMap.get(content); if (result == null) { scssStylesheet.compile(urlMode); Writer writer = new StringWriter(); scssStylesheet.write(writer, minify); result = writer.toString(); if (routeContext.getApplication().getPippoSettings().isProd()) { sourceMap.put(content, result); } } // send css routeContext.getResponse().contentType("text/css"); routeContext.getResponse().ok().send(result); } catch (Exception e) { throw new PippoRuntimeException(e); } }
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(); }
@Override protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { try { // compile less to css log.trace("Send css for '{}'", resourceUrl); LessSource.URLSource source = new LessSource.URLSource(resourceUrl); String content = source.getContent(); String result = sourceMap.get(content); if (result == null) { ThreadUnsafeLessCompiler compiler = new ThreadUnsafeLessCompiler(); LessCompiler.Configuration configuration = new LessCompiler.Configuration(); configuration.setCompressing(minify); LessCompiler.CompilationResult compilationResult = compiler.compile(resourceUrl, configuration); for (LessCompiler.Problem warning : compilationResult.getWarnings()) { log.warn("Line: {}, Character: {}, Message: {} ", warning.getLine(), warning.getCharacter(), warning.getMessage()); } result = compilationResult.getCss(); if (routeContext.getApplication().getPippoSettings().isProd()) { sourceMap.put(content, result); } } // send css routeContext.getResponse().contentType("text/css"); routeContext.getResponse().ok().send(result); } catch (Exception e) { throw new PippoRuntimeException(e); } }
@SuppressWarnings("unchecked") public <T extends ControllerApplication> T getApplication() { return (T) getRouteContext().getApplication(); }
@Override protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { try { // compile sass to css log.trace("Send css for '{}'", resourceUrl); ScssContext.UrlMode urlMode = ScssContext.UrlMode.ABSOLUTE; String identifier = resourceUrl.getFile(); ScssStylesheet scssStylesheet = ScssStylesheet.get(identifier); if (scssStylesheet == null) { throw new Exception("ScssStylesheet is null for '" + identifier + "'"); } String content = scssStylesheet.toString(); String result = sourceMap.get(content); if (result == null) { scssStylesheet.compile(urlMode); Writer writer = new StringWriter(); scssStylesheet.write(writer, minify); result = writer.toString(); if (routeContext.getApplication().getPippoSettings().isProd()) { sourceMap.put(content, result); } } // send css routeContext.getResponse().contentType("text/css"); routeContext.getResponse().ok().send(result); } catch (Exception e) { throw new PippoRuntimeException(e); } }
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(); }
@Override protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { try { // compile less to css log.trace("Send css for '{}'", resourceUrl); LessSource.URLSource source = new LessSource.URLSource(resourceUrl); String content = source.getContent(); String result = sourceMap.get(content); if (result == null) { ThreadUnsafeLessCompiler compiler = new ThreadUnsafeLessCompiler(); LessCompiler.Configuration configuration = new LessCompiler.Configuration(); configuration.setCompressing(minify); LessCompiler.CompilationResult compilationResult = compiler.compile(resourceUrl, configuration); for (LessCompiler.Problem warning : compilationResult.getWarnings()) { log.warn("Line: {}, Character: {}, Message: {} ", warning.getLine(), warning.getCharacter(), warning.getMessage()); } result = compilationResult.getCss(); if (routeContext.getApplication().getPippoSettings().isProd()) { sourceMap.put(content, result); } } // send css routeContext.getResponse().contentType("text/css"); routeContext.getResponse().ok().send(result); } catch (Exception e) { throw new PippoRuntimeException(e); } }