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 org.apache.juneau.rest.annotation.*; 016 017/** 018 * Class used for defining method-level matchers using the {@link RestMethod#matchers() @RestMethod.matchers()} annotation. 019 * 020 * <p> 021 * Matchers are used to allow multiple Java methods to handle requests assigned to the same URL path pattern, but 022 * differing based on some request attribute, such as a specific header value. 023 * For example, matchers can be used to provide two different methods for handling requests from two different client 024 * versions. 025 * 026 * <p> 027 * Java methods with matchers associated with them are always attempted before Java methods without matchers. 028 * This allows a 'default' method to be defined to handle requests where no matchers match. 029 * 030 * <p> 031 * When multiple matchers are specified on a method, only one matcher is required to match. 032 * This is opposite from the {@link RestMethod#guards() @RestMethod.guards()} annotation, where all guards are required to match in order to 033 * execute the method. 034 * 035 * <h5 class='section'>Example:</h5> 036 * <p class='bcode'> 037 * <jk>public class</jk> MyResource <jk>extends</jk> RestServlet { 038 * 039 * <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, path=<js>"/foo"</js>, matchers=IsDNT.<jk>class</jk>) 040 * <jk>public</jk> Object doGetWithDNT() { 041 * <jc>// Handle request with Do-Not-Track specified</jc> 042 * } 043 * 044 * <ja>@RestMethod</ja>(name=<jsf>GET</jsf>, path=<js>"/foo"</js>) 045 * <jk>public</jk> Object doGetWithoutDNT() { 046 * <jc>// Handle request without Do-Not-Track specified</jc> 047 * } 048 * } 049 * 050 * <jk>public class</jk> IsDNT <jk>extends</jk> RestMatcher { 051 * <ja>@Override</ja> 052 * <jk>public boolean</jk> matches(RestRequest req) { 053 * <jk>return</jk> req.getHeader(<jk>int</jk>.<jk>class</jk>, <js>"DNT"</js>, 0) == 1; 054 * } 055 * } 056 * </p> 057 * 058 * <p> 059 * Instances must provide one of the following public constructors: 060 * <ul> 061 * <li>No-args. 062 * <li>The following args: <code>Object resource, Method javaMethod</code>. 063 * <br>This gives access to the servlet/resource and Java method it's applied to. 064 * </ul> 065 * 066 * <h5 class='section'>See Also:</h5> 067 * <ul> 068 * <li class='link'><a class="doclink" href="../../../../overview-summary.html#juneau-rest-server.RestMethodMatchers">Overview > juneau-rest-server > @RestMethod.matchers()</a> 069 * </ul> 070 */ 071public abstract class RestMatcher { 072 073 /** 074 * Returns <jk>true</jk> if the specified request matches this matcher. 075 * 076 * @param req The servlet request. 077 * @return <jk>true</jk> if the specified request matches this matcher. 078 */ 079 public abstract boolean matches(RestRequest req); 080 081 /** 082 * Returns <jk>true</jk> if this matcher is required to match in order for the method to be invoked. 083 * 084 * <p> 085 * If <jk>false</jk>, then only one of the matchers must match. 086 * 087 * @return <jk>true</jk> if this matcher is required to match in order for the method to be invoked. 088 */ 089 public boolean mustMatch() { 090 return false; 091 } 092}