001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.matcher; 018 019import static org.apache.juneau.commons.utils.StringUtils.*; 020import static org.apache.juneau.rest.annotation.RestOpAnnotation.*; 021 022import org.apache.juneau.commons.lang.*; 023import org.apache.juneau.commons.reflect.*; 024import jakarta.servlet.http.*; 025 026/** 027 * Specialized matcher for matching client versions. 028 * 029 * <h5 class='section'>See Also:</h5><ul> 030 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ClientVersioning">Client Versioning</a> 031 * </ul> 032 */ 033public class ClientVersionMatcher extends RestMatcher { 034 035 private static final AnnotationProvider AP = AnnotationProvider.INSTANCE; 036 037 private final String clientVersionHeader; 038 private final VersionRange range; 039 040 /** 041 * Constructor. 042 * 043 * @param clientVersionHeader 044 * The HTTP request header name containing the client version. 045 * If <jk>null</jk> or an empty string, uses <js>"Client-Version"</js> 046 * @param mi The version string that the client version must match. 047 */ 048 public ClientVersionMatcher(String clientVersionHeader, MethodInfo mi) { 049 this.clientVersionHeader = isEmpty(clientVersionHeader) ? "Client-Version" : clientVersionHeader; 050 // @formatter:off 051 var clientVersion = AP.find(mi) 052 .stream() 053 .filter(REST_OP_GROUP) 054 .flatMap(ai -> ai.getValue(String.class, "clientVersion").stream()) 055 .filter(NOT_EMPTY) 056 .findFirst() 057 .orElse(null); 058 // @formatter:on 059 range = new VersionRange(clientVersion); 060 } 061 062 @Override /* Overridden from RestMatcher */ 063 public boolean matches(HttpServletRequest req) { 064 return range.matches(req.getHeader(clientVersionHeader)); 065 } 066 067 @Override /* Overridden from RestMatcher */ 068 public boolean required() { 069 return true; 070 } 071}