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