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