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