001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest; 014 015import static org.apache.juneau.internal.CollectionUtils.*; 016 017import java.util.*; 018 019import org.apache.juneau.*; 020import org.apache.juneau.internal.*; 021import org.apache.juneau.parser.*; 022import org.apache.juneau.rest.annotation.*; 023import org.apache.juneau.utils.*; 024 025/** 026 * Static file mapping. 027 * 028 * <p> 029 * Used to define paths and locations of statically-served files such as images or HTML documents. 030 * 031 * <p> 032 * An example where this class is used is in the {@link RestResource#staticFiles} annotation: 033 * <p class='bcode w800'> 034 * <jk>package</jk> com.foo.mypackage; 035 * 036 * <ja>@RestResource</ja>( 037 * path=<js>"/myresource"</js>, 038 * staticFiles={<js>"htdocs:docs"</js>} 039 * ) 040 * <jk>public class</jk> MyResource <jk>extends</jk> BasicRestServlet {...} 041 * </p> 042 * 043 * <p> 044 * Static files are found by using the {@link ClasspathResourceFinder} defined on the resource. 045 * 046 * <p> 047 * In the example above, given a GET request to <l>/myresource/htdocs/foobar.html</l>, the servlet will attempt to find 048 * the <l>foobar.html</l> file in the following ordered locations: 049 * <ol> 050 * <li><l>com.foo.mypackage.docs</l> package. 051 * <li><l>org.apache.juneau.rest.docs</l> package (since <l>BasicRestServlet</l> is in <l>org.apache.juneau.rest</l>). 052 * <li><l>[working-dir]/docs</l> directory. 053 * </ol> 054 * 055 * <ul class='notes'> 056 * <li> 057 * Mappings are cumulative from parent to child. Child resources can override mappings made on parent resources. 058 * <li> 059 * The media type on the response is determined by the {@link org.apache.juneau.rest.RestContext#getMediaTypeForName(String)} method. 060 * </ul> 061 * 062 * <ul class='seealso'> 063 * <li class='link'>{@doc juneau-rest-server.StaticFiles} 064 * </ul> 065 */ 066public class StaticFileMapping { 067 068 final Class<?> resourceClass; 069 final String path, location; 070 final Map<String,Object> responseHeaders; 071 072 /** 073 * Constructor. 074 * 075 * @param resourceClass 076 * The resource/servlet class which serves as the base location of the location below. 077 * @param path 078 * The mapped URI path. 079 * <br>Leading and trailing slashes are trimmed. 080 * @param location 081 * The location relative to the resource class. 082 * <br>Leading and trailing slashes are trimmed. 083 * @param responseHeaders 084 * The response headers. 085 * Can be <jk>null</jk>. 086 */ 087 public StaticFileMapping(Class<?> resourceClass, String path, String location, Map<String,Object> responseHeaders) { 088 this.resourceClass = resourceClass; 089 this.path = StringUtils.trimSlashes(path); 090 this.location = StringUtils.trimSlashes(location); 091 this.responseHeaders = immutableMap(responseHeaders); 092 } 093 094 /** 095 * Constructor using a mapping string to represent a path/location pairing. 096 * 097 * <p> 098 * Mapping string must be one of these formats: 099 * <ul> 100 * <li><js>"path:location"</js> (e.g. <js>"foodocs:docs/foo"</js>) 101 * <li><js>"path:location:headers-json"</js> (e.g. <js>"foodocs:docs/foo:{'Cache-Control':'max-age=86400, public'}"</js>) 102 * </ul> 103 * 104 * @param resourceClass 105 * The resource/servlet class which serves as the base location of the location below. 106 * @param mappingString 107 * The mapping string that represents the path/location mapping. 108 * <br>Leading and trailing slashes and whitespace are trimmed from path and location. 109 */ 110 public StaticFileMapping(Class<?> resourceClass, String mappingString) { 111 this.resourceClass = resourceClass; 112 String[] parts = StringUtils.split(mappingString, ':', 3); 113 if (parts == null || parts.length <= 1) 114 throw new FormattedRuntimeException("Invalid mapping string format: ''{0}'' on resource class ''{1}''", mappingString, resourceClass.getName()); 115 this.path = StringUtils.trimSlashes(parts[0]); 116 this.location = StringUtils.trimSlashes(parts[1]); 117 if (parts.length == 3) { 118 try { 119 responseHeaders = unmodifiableMap(new ObjectMap(parts[2])); 120 } catch (ParseException e) { 121 throw new FormattedRuntimeException(e, "Invalid mapping string format: ''{0}'' on resource class ''{1}''", mappingString, resourceClass.getName()); 122 } 123 } else { 124 responseHeaders = null; 125 } 126 } 127}