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 static java.nio.charset.StandardCharsets.*;
020
021import java.io.*;
022
023import org.apache.juneau.http.*;
024import org.apache.juneau.http.response.*;
025import org.apache.juneau.petstore.console.dev.*;
026import org.apache.juneau.petstore.console.ops.*;
027import org.apache.juneau.petstore.console.orders.*;
028import org.apache.juneau.petstore.console.pets.*;
029import org.apache.juneau.petstore.console.users.*;
030import org.apache.juneau.rest.server.*;
031
032/**
033 * Root of the petstore admin console, mounted at {@code /console} beside the {@code /petstore} API and sharing its
034 * {@link org.apache.juneau.petstore.service.PetStore}.
035 *
036 * <p>
037 * Every page renders through {@code base.ftlh} (the {@code <@console>} chrome and nav tree).
038 *
039 * <h2 class='section'>Example:</h2>
040 * <p class='bjava'>
041 *    <jc>// Mount it in a runner beside the API; both see the same @Bean PetStore.</jc>
042 *    <ja>@Rest</ja>(children={PetStoreResource.<jk>class</jk>, PetstoreConsoleResource.<jk>class</jk>})
043 *    <jk>public class</jk> RootResources <jk>extends</jk> BasicRestServletGroup {
044 *       <ja>@Bean</ja> <jk>public</jk> PetStore petStore() { <jk>return</jk> PetstoreSeed.<jsm>create</jsm>().populate(<jk>new</jk> PetStore()); }
045 *    }
046 * </p>
047 * <p class='bcode'>
048 *    &lt;#-- base.ftlh --&gt;
049 *    &lt;@console brand="Juneau Petstore"&gt;
050 *      &lt;@navigation&gt;&lt;@node id="store" label="Store" href="/console/store"/&gt; ... &lt;/@navigation&gt;
051 *      &lt;@main/&gt;
052 *    &lt;/@console&gt;
053 * </p>
054 */
055@Rest(
056   path="/console",
057   title="Juneau Petstore console",
058   children={
059      StoreRest.class,
060      PetsRest.class,
061      OrdersRest.class,
062      UsersRest.class,
063      OpsRest.class,
064      DevRest.class,
065      AboutRest.class
066   }
067)
068@SuppressWarnings({
069   "java:S110", // Inheritance depth comes from the BasicRestServlet hierarchy, not this page.
070   "resource" // The response writer is owned by the servlet container; Eclipse JDT @Owning warning is by design.
071})
072public class PetstoreConsoleResource extends PetstoreConsolePage {
073
074   private static final long serialVersionUID = 1L;
075
076   /**
077    * {@code GET /console} redirects to the store dashboard.
078    *
079    * @param req The request.
080    * @return A 303 to {@code /console/store}.
081    */
082   @RestGet(path="/")
083   public SeeOther index(RestRequest req) {
084      return new SeeOther().setLocation(req.getUriResolver().resolve("servlet:/store"));
085   }
086
087   /**
088    * Serves {@code petstore.js}, the shared page script {@code base.ftlh} loads after the toolkit on every page.
089    *
090    * @param res The response.
091    * @throws IOException If the bundled script can't be read.
092    */
093   @RestGet(path="/petstore.js")
094   public void script(RestResponse res) throws IOException {
095      try (var in = PetstoreConsoleResource.class.getResourceAsStream("petstore.js")) {
096         if (in == null)
097            throw new IllegalStateException("Missing classpath resource 'petstore.js' beside " + PetstoreConsoleResource.class.getName());
098         res.setContentType("text/javascript;charset=utf-8");
099         res.getWriter().write(new String(in.readAllBytes(), UTF_8));
100      }
101   }
102
103   /**
104    * Serves one of the vendored export libraries ({@code jszip.min.js}, {@code pdfmake.min.js}, {@code vfs_fonts.js})
105    * that the {@code datatables-export} toolkit pack loads.
106    *
107    * @param res The response.
108    * @param name The library file name; anything outside the fixed list is a 404.
109    * @throws IOException If the bundled library can't be read.
110    */
111   @RestGet(path="/export/{name}")
112   public void exportLib(RestResponse res, @Path("name") String name) throws IOException {
113      if (!EXPORT_LIBS.contains(name))
114         throw new NotFound("No such export library");
115      try (var in = PetstoreConsoleResource.class.getResourceAsStream("/htdocs/vendor/" + name)) {
116         if (in == null)
117            throw new IllegalStateException("Missing classpath resource 'htdocs/vendor/" + name + "'");
118         res.setContentType("text/javascript;charset=utf-8");
119         res.getWriter().write(new String(in.readAllBytes(), UTF_8));
120      }
121   }
122}