All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.rest.RestMatcher Maven / Gradle / Ivy

There is a newer version: 9.0.1
Show newest version
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
// * with the License.  You may obtain a copy of the License at                                                              *
// *                                                                                                                         *
// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
// *                                                                                                                         *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
// * specific language governing permissions and limitations under the License.                                              *
// ***************************************************************************************************************************
package org.apache.juneau.rest;

import org.apache.juneau.rest.annotation.*;

/**
 * Class used for defining method-level matchers using the {@link RestMethod#matchers() @RestMethod(matchers)} annotation.
 *
 * 

* Matchers are used to allow multiple Java methods to handle requests assigned to the same URL path pattern, but * differing based on some request attribute, such as a specific header value. * For example, matchers can be used to provide two different methods for handling requests from two different client * versions. * *

* Java methods with matchers associated with them are always attempted before Java methods without matchers. * This allows a 'default' method to be defined to handle requests where no matchers match. * *

* When multiple matchers are specified on a method, only one matcher is required to match. * This is opposite from the {@link RestMethod#guards() @RestMethod(guards)} annotation, where all guards are required to match in order to * execute the method. * *

Example:
*

* public class MyResource extends RestServlet { * * @RestMethod(name=GET, path="/foo", matchers=IsDNT.class) * public Object doGetWithDNT() { * // Handle request with Do-Not-Track specified * } * * @RestMethod(name=GET, path="/foo") * public Object doGetWithoutDNT() { * // Handle request without Do-Not-Track specified * } * } * * public class IsDNT extends RestMatcher { * @Override * public boolean matches(RestRequest req) { * return req.getHeader(int.class, "DNT", 0) == 1; * } * } *

* *

* Instances must provide one of the following public constructors: *

    *
  • No-args. *
  • The following args: Object resource, Method javaMethod. *
    This gives access to the servlet/resource and Java method it's applied to. *
* *
See Also:
*
    *
*/ public abstract class RestMatcher { /** * Returns true if the specified request matches this matcher. * * @param req The servlet request. * @return true if the specified request matches this matcher. */ public abstract boolean matches(RestRequest req); /** * Returns true if this matcher is required to match in order for the method to be invoked. * *

* If false, then only one of the matchers must match. * * @return true if this matcher is required to match in order for the method to be invoked. */ public boolean mustMatch() { return false; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy