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 static org.apache.juneau.internal.StringUtils.*;
016import static org.apache.juneau.internal.ClassUtils.*;
017
018import org.apache.juneau.internal.*;
019import org.apache.juneau.rest.annotation.*;
020
021/**
022 * Specialized matcher for matching client versions.
023 *
024 * <h5 class='section'>See Also:</h5>
025 * <ul>
026 *    <li class='link'>{@doc juneau-rest-server.ClientVersioning}
027 * </ul>
028 */
029public class ClientVersionMatcher extends RestMatcher {
030
031   private final String clientVersionHeader;
032   private final VersionRange range;
033
034   /**
035    * Constructor.
036    *
037    * @param clientVersionHeader
038    *    The HTTP request header name containing the client version.
039    *    If <jk>null</jk> or an empty string, uses <js>"X-Client-Version"</js>
040    * @param javaMethod The version string that the client version must match.
041    */
042   protected ClientVersionMatcher(String clientVersionHeader, java.lang.reflect.Method javaMethod) {
043      this.clientVersionHeader = isEmpty(clientVersionHeader) ? "X-Client-Version" : clientVersionHeader;
044      RestMethod m = getAnnotation(RestMethod.class, javaMethod);
045      range = new VersionRange(m.clientVersion());
046   }
047
048   @Override /* RestMatcher */
049   public boolean matches(RestRequest req) {
050      return range.matches(req.getHeader(clientVersionHeader));
051   }
052
053   @Override /* RestMatcher */
054   public boolean mustMatch() {
055      return true;
056   }
057}