|
213 | 213 | import com.google.inject.Key; |
214 | 214 | import com.google.inject.TypeLiteral; |
215 | 215 | import static java.util.Objects.requireNonNull; |
| 216 | +import org.jooby.funzy.Throwing; |
| 217 | +import org.jooby.handlers.AssetHandler; |
216 | 218 | import org.jooby.internal.RouteImpl; |
217 | 219 | import org.jooby.internal.RouteMatcher; |
218 | 220 | import org.jooby.internal.RoutePattern; |
219 | 221 | import org.jooby.internal.RouteSourceImpl; |
220 | 222 | import org.jooby.internal.SourceProvider; |
221 | | -import org.jooby.funzy.Throwing; |
222 | 223 |
|
223 | 224 | import javax.annotation.Nonnull; |
224 | 225 | import javax.annotation.Nullable; |
225 | 226 | import java.lang.reflect.Array; |
226 | 227 | import java.lang.reflect.Method; |
| 228 | +import java.time.Duration; |
227 | 229 | import java.util.ArrayList; |
228 | 230 | import java.util.Arrays; |
229 | 231 | import java.util.Collections; |
@@ -2050,7 +2052,136 @@ public interface Filter { |
2050 | 2052 | * @throws Throwable If something goes wrong. |
2051 | 2053 | */ |
2052 | 2054 | void handle(Request req, Response rsp, Route.Chain chain) throws Throwable; |
| 2055 | + } |
| 2056 | + |
| 2057 | + /** |
| 2058 | + * Allow to customize an asset handler. |
| 2059 | + * |
| 2060 | + * @author edgar |
| 2061 | + */ |
| 2062 | + class AssetDefinition extends Definition { |
| 2063 | + |
| 2064 | + private Boolean etag; |
| 2065 | + |
| 2066 | + private String cdn; |
| 2067 | + |
| 2068 | + private Object maxAge; |
| 2069 | + |
| 2070 | + private Boolean lastModifiedSince; |
| 2071 | + |
| 2072 | + private Integer statusCode; |
| 2073 | + |
| 2074 | + /** |
| 2075 | + * Creates a new route definition. |
| 2076 | + * |
| 2077 | + * @param method A HTTP verb or <code>*</code>. |
| 2078 | + * @param pattern A path pattern. |
| 2079 | + * @param handler A callback to execute. |
| 2080 | + * @param caseSensitiveRouting Configure case for routing algorithm. |
| 2081 | + */ |
| 2082 | + public AssetDefinition(final String method, final String pattern, |
| 2083 | + final Route.Filter handler, boolean caseSensitiveRouting) { |
| 2084 | + super(method, pattern, handler, caseSensitiveRouting); |
| 2085 | + } |
| 2086 | + |
| 2087 | + @Nonnull |
| 2088 | + @Override |
| 2089 | + public AssetHandler filter() { |
| 2090 | + return (AssetHandler) super.filter(); |
| 2091 | + } |
| 2092 | + |
| 2093 | + /** |
| 2094 | + * Indicates what to do when an asset is missing (not resolved). Default action is to resolve them |
| 2095 | + * as <code>404 (NOT FOUND)</code> request. |
| 2096 | + * |
| 2097 | + * If you specify a status code <= 0, missing assets are ignored and the next handler on pipeline |
| 2098 | + * will be executed. |
| 2099 | + * |
| 2100 | + * @param statusCode HTTP code or 0. |
| 2101 | + * @return This route definition. |
| 2102 | + */ |
| 2103 | + public AssetDefinition onMissing(final int statusCode) { |
| 2104 | + if (this.statusCode == null) { |
| 2105 | + filter().onMissing(statusCode); |
| 2106 | + this.statusCode = statusCode; |
| 2107 | + } |
| 2108 | + return this; |
| 2109 | + } |
| 2110 | + |
| 2111 | + /** |
| 2112 | + * @param etag Turn on/off etag support. |
| 2113 | + * @return This route definition. |
| 2114 | + */ |
| 2115 | + public AssetDefinition etag(final boolean etag) { |
| 2116 | + if (this.etag == null) { |
| 2117 | + filter().etag(etag); |
| 2118 | + this.etag = etag; |
| 2119 | + } |
| 2120 | + return this; |
| 2121 | + } |
| 2122 | + |
| 2123 | + /** |
| 2124 | + * @param enabled Turn on/off last modified support. |
| 2125 | + * @return This route definition. |
| 2126 | + */ |
| 2127 | + public AssetDefinition lastModified(final boolean enabled) { |
| 2128 | + if (this.lastModifiedSince == null) { |
| 2129 | + filter().lastModified(enabled); |
| 2130 | + this.lastModifiedSince = enabled; |
| 2131 | + } |
| 2132 | + return this; |
| 2133 | + } |
2053 | 2134 |
|
| 2135 | + /** |
| 2136 | + * @param cdn If set, every resolved asset will be serve from it. |
| 2137 | + * @return This route definition. |
| 2138 | + */ |
| 2139 | + public AssetDefinition cdn(final String cdn) { |
| 2140 | + if (this.cdn == null) { |
| 2141 | + filter().cdn(cdn); |
| 2142 | + this.cdn = cdn; |
| 2143 | + } |
| 2144 | + return this; |
| 2145 | + } |
| 2146 | + |
| 2147 | + /** |
| 2148 | + * @param maxAge Set the cache header max-age value. |
| 2149 | + * @return This route definition. |
| 2150 | + */ |
| 2151 | + public AssetDefinition maxAge(final Duration maxAge) { |
| 2152 | + if (this.maxAge == null) { |
| 2153 | + filter().maxAge(maxAge); |
| 2154 | + this.maxAge = maxAge; |
| 2155 | + } |
| 2156 | + return this; |
| 2157 | + } |
| 2158 | + |
| 2159 | + /** |
| 2160 | + * @param maxAge Set the cache header max-age value in seconds. |
| 2161 | + * @return This route definition. |
| 2162 | + */ |
| 2163 | + public AssetDefinition maxAge(final long maxAge) { |
| 2164 | + if (this.maxAge == null) { |
| 2165 | + filter().maxAge(maxAge); |
| 2166 | + this.maxAge = maxAge; |
| 2167 | + } |
| 2168 | + return this; |
| 2169 | + } |
| 2170 | + |
| 2171 | + /** |
| 2172 | + * Parse value as {@link Duration}. If the value is already a number then it uses as seconds. |
| 2173 | + * Otherwise, it parse expressions like: 8m, 1h, 365d, etc... |
| 2174 | + * |
| 2175 | + * @param maxAge Set the cache header max-age value in seconds. |
| 2176 | + * @return This route definition. |
| 2177 | + */ |
| 2178 | + public AssetDefinition maxAge(final String maxAge) { |
| 2179 | + if (this.maxAge == null) { |
| 2180 | + filter().maxAge(maxAge); |
| 2181 | + this.maxAge = maxAge; |
| 2182 | + } |
| 2183 | + return this; |
| 2184 | + } |
2054 | 2185 | } |
2055 | 2186 |
|
2056 | 2187 | /** |
|
0 commit comments