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.rest.server.staticfile; 018 019import java.util.*; 020 021import org.apache.juneau.http.*; 022import org.apache.juneau.http.response.*; 023import org.apache.juneau.rest.server.*; 024import org.apache.juneau.rest.server.servlet.*; 025 026/** 027 * Child-resource flavor of the static-files capability. 028 * 029 * <p> 030 * Mounts as a <b>routed child</b> via {@link Rest#children() @Rest(children=StaticFilesResource.class)} 031 * under a parent at the subtree {@code /static} and serves static files by delegating to the 032 * flavor-neutral {@link StaticFiles} worker bean resolved from the request context 033 * ({@link RestContext#getStaticFiles()}) — the same worker the {@link StaticFilesMixin mixin} and 034 * {@link StaticFilesServlet servlet} flavors use, so the three forms cannot drift. 035 * 036 * <p> 037 * Whereas the {@link StaticFilesMixin} mixin pins its op at {@code /static/*} to merge into a host's 038 * namespace, this child declares the subtree at the class level ({@link Rest#path() @Rest(path="/static")}) 039 * and pins the ops at {@code /*} — the op must <b>not</b> re-pin {@code /static/*} or it would 040 * double-path to {@code /static/static/*} under the routed-subtree wiring. 041 * 042 * <h5 class='section'>See Also:</h5><ul> 043 * <li class='jc'>{@link StaticFilesMixin} 044 * <li class='jc'>{@link StaticFilesServlet} 045 * <li class='jc'>{@link StaticFiles} 046 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/StaticFiles">Static files</a> 047 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerComposition">REST Server — Mixins and Multi-Mount Paths</a> 048 * </ul> 049 * 050 * @since 10.0.0 051 */ 052// @formatter:off 053@Rest(path="/static") 054public class StaticFilesResource extends RestResource { 055 056 /** 057 * [GET /*] — serve a static file from the active {@link StaticFiles} worker bean. 058 * 059 * @param req The current REST request — supplies {@link RestContext#getStaticFiles()}. 060 * @param path The trailing remainder after the mount prefix. 061 * @param locale The request locale (used for localized resource lookups). 062 * @return The matching {@link HttpResource} (with content type + cache headers). 063 * @throws NotFound If no resource matches the requested path. 064 */ 065 @RestGet( 066 path="/*", 067 summary="Static files", 068 description="Static file retrieval.", 069 swagger=@OpSwagger(ignore=true) 070 ) 071 public HttpResource getStaticFile(RestRequest req, @Path("/*") String path, Locale locale) { 072 return req.getContext().getStaticFiles().resolve(path, locale).orElseThrow(NotFound::new); 073 } 074 075 /** 076 * [HEAD /*] — return GET headers for a static file without the body. 077 * 078 * @param req The current REST request — supplies {@link RestContext#getStaticFiles()}. 079 * @param path The trailing remainder after the mount prefix. 080 * @param locale The request locale (used for localized resource lookups). 081 * @return The matching {@link HttpResource} (with headers; body suppressed by the processor). 082 * @throws NotFound If no resource matches the requested path. 083 */ 084 @RestOp( 085 method="HEAD", 086 path="/*", 087 summary="Static files (HEAD)", 088 description="Static file metadata retrieval.", 089 swagger=@OpSwagger(ignore=true) 090 ) 091 public HttpResource headStaticFile(RestRequest req, @Path("/*") String path, Locale locale) { 092 return getStaticFile(req, path, locale); 093 } 094}