001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.petstore.console; 018 019import java.util.*; 020 021import org.apache.juneau.commons.inject.*; 022import org.apache.juneau.petstore.console.dev.*; 023import org.apache.juneau.petstore.service.*; 024import org.apache.juneau.rest.server.*; 025import org.apache.juneau.console.datatables.*; 026import org.apache.juneau.rest.server.servlet.*; 027import org.apache.juneau.rest.server.staticfile.*; 028import org.apache.juneau.rest.server.view.*; 029import org.apache.juneau.rest.server.view.freemarker.*; 030import org.apache.juneau.console.freemarker.*; 031import org.apache.juneau.console.views.*; 032 033/** 034 * Base class for every petstore console page resource: registers the console FreeMarker mixin with 035 * {@code base.ftlh} as chrome, and injects the shared {@link PetStore}. 036 * 037 * <p> 038 * Children don't inherit the parent's mixins, so every page class extends this one instead of re-declaring the 039 * mixin, which E-B12 would reject. 040 * 041 * <p> 042 * The views and DataTables toolkit packs link their assets page-relative (e.g. {@code /console/store/juneau-views.js}), 043 * so every page also composes {@link ViewsMixin} and {@link DataTablesMixin} to serve them from its own mount. 044 * 045 * <h2 class='section'>Example:</h2> 046 * <p class='bjava'> 047 * <ja>@Rest</ja>(path=<js>"/ops/audit"</js>) 048 * <jk>public class</jk> AuditRest <jk>extends</jk> PetstoreConsolePage { 049 * <ja>@RestGet</ja>(path=<js>"/"</js>) 050 * <jk>public</jk> View page() { <jk>return</jk> FreemarkerView.<jsm>of</jsm>(<js>"audit.ftlh"</js>); } 051 * } 052 * </p> 053 */ 054@Rest(mixins={FreemarkerMixin.class, ViewsMixin.class, DataTablesMixin.class, WebJarsMixin.class}, responseProcessors=FreemarkerViewRenderer.class) 055public abstract class PetstoreConsolePage extends BasicRestServlet { 056 057 private static final long serialVersionUID = 1L; 058 059 /** Classpath folder holding the console templates. */ 060 public static final String TEMPLATES = "/org/apache/juneau/petstore/console/templates/"; 061 062 /** Response header naming the render path of a builder-twin page, so a twin test can tell the two paths apart. */ 063 public static final String PAGE_HEADER = "X-Petstore-Page"; 064 065 /** 066 * Whether this request asks for the builder twin of a page ({@code ?builder}, with or without a value). 067 * 068 * @param req The request. 069 * @return <jk>true</jk> if the page should render from its {@link PageSpec}. 070 */ 071 public static boolean builder(RestRequest req) { 072 return req.containsQueryParam("builder"); 073 } 074 075 /** 076 * Renders a builder-twin page, marked with {@link #PAGE_HEADER}. 077 * 078 * @param req The request. 079 * @param spec The page spec. 080 * @return The view. 081 */ 082 public static View twin(RestRequest req, PageSpec spec) { 083 return spec.header(PAGE_HEADER, "builder").view(req); 084 } 085 086 @SuppressWarnings({ 087 "java:S2226" // the field is set by the framework's @Bean injection after construction, so it cannot be final. 088 }) 089 @Bean 090 private transient PetStore store; 091 092 /** 093 * The console FreeMarker mixin: petstore templates, {@code base.ftlh} chrome, {@code assetUrl(..)} over this 094 * package's bundled scripts, and the {@link InventoryGaugeCardType} custom card type. 095 * 096 * @return The mixin. 097 */ 098 @Bean 099 public FreemarkerMixin freemarker() { 100 // assetUrl('/console/petstore.js') versions the shared page script served by PetstoreConsoleResource. 101 return ConsoleFreemarkerMixin.create().basePath(TEMPLATES).chromeTemplate("base.ftlh") 102 .adopterAssets(PetstoreConsolePage.class.getClassLoader(), "org/apache/juneau/petstore") 103 .cardType(new InventoryGaugeCardType()).requirePacks("charts") 104 .registerToolkitPack(exportPack()).build(); 105 } 106 107 /** Vendored export libraries behind the {@code datatables-export} pack, in load order. */ 108 static final List<String> EXPORT_LIBS = List.of("jszip.min.js", "pdfmake.min.js", "vfs_fonts.js"); 109 110 /** Mount of the vendored export libraries, under the console root. */ 111 static final String EXPORT_MOUNT = "/console/export/"; 112 113 /** 114 * The {@code datatables-export} pack: the JSZip and pdfMake libraries (with fonts) that the DataTables Excel and PDF 115 * buttons need, served from this console's own {@code /export/*} route. 116 */ 117 private static ToolkitPack exportPack() { 118 return ToolkitPack.create("datatables-export") 119 .js(EXPORT_LIBS) 120 .dependsOn("datatables-buttons") 121 .kind(ToolkitPack.Kind.VENDOR) 122 .resolver((req, path) -> req.getUriResolver().resolve("context:" + EXPORT_MOUNT + path)) 123 .build(); 124 } 125 126 /** 127 * @return The shared store. 128 * @throws IllegalStateException If no {@link PetStore} {@code @Bean} was wired into the host. 129 */ 130 protected PetStore store() { 131 if (store == null) 132 throw new IllegalStateException("No PetStore bean is wired: declare a @Bean PetStore on the host that mounts " + getClass().getSimpleName()); 133 return store; 134 } 135}