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 static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.rest.annotation.RestOpAnnotation.*;
017
018import jakarta.servlet.http.*;
019
020import org.apache.juneau.*;
021import org.apache.juneau.internal.*;
022import org.apache.juneau.reflect.*;
023
024/**
025 * Specialized matcher for matching client versions.
026 *
027 * <h5 class='section'>See Also:</h5><ul>
028 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.ClientVersioning">Client Versioning</a>
029 * </ul>
030 */
031public class ClientVersionMatcher extends RestMatcher {
032
033   private final String clientVersionHeader;
034   private final VersionRange range;
035
036   /**
037    * Constructor.
038    *
039    * @param clientVersionHeader
040    *    The HTTP request header name containing the client version.
041    *    If <jk>null</jk> or an empty string, uses <js>"Client-Version"</js>
042    * @param mi The version string that the client version must match.
043    */
044   public ClientVersionMatcher(String clientVersionHeader, MethodInfo mi) {
045      this.clientVersionHeader = isEmpty(clientVersionHeader) ? "Client-Version" : clientVersionHeader;
046      Value<String> clientVersion = Value.empty();
047      mi.getAnnotationList(REST_OP_GROUP).forEachValue(String.class, "clientVersion", NOT_EMPTY, x -> clientVersion.set(x));
048      range = new VersionRange(clientVersion.orElse(null));
049   }
050
051   @Override /* RestMatcher */
052   public boolean matches(HttpServletRequest req) {
053      return range.matches(req.getHeader(clientVersionHeader));
054   }
055
056   @Override /* RestMatcher */
057   public boolean required() {
058      return true;
059   }
060}