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 w800'>
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: <c>Object resource, Method javaMethod</c>.
063 *       <br>This gives access to the servlet/resource and Java method it's applied to.
064 * </ul>
065 *
066 * <ul class='seealso'>
067 *    <li class='link'>{@doc RestmMatchers}
068 * </ul>
069 */
070public abstract class RestMatcher {
071
072   /**
073    * Returns <jk>true</jk> if the specified request matches this matcher.
074    *
075    * @param req The servlet request.
076    * @return <jk>true</jk> if the specified request matches this matcher.
077    */
078   public abstract boolean matches(RestRequest req);
079
080   /**
081    * Returns <jk>true</jk> if this matcher is required to match in order for the method to be invoked.
082    *
083    * <p>
084    * If <jk>false</jk>, then only one of the matchers must match.
085    *
086    * @return <jk>true</jk> if this matcher is required to match in order for the method to be invoked.
087    */
088   public boolean mustMatch() {
089      return false;
090   }
091}