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.server.matcher; 018 019import static org.apache.juneau.commons.utils.StringUtils.*; 020import static org.apache.juneau.rest.server.RestOpAnnotation.*; 021 022import org.apache.juneau.commons.lang.*; 023import org.apache.juneau.commons.reflect.*; 024 025import jakarta.servlet.http.*; 026 027/** 028 * Specialized matcher for matching client versions. 029 * 030 * <h5 class='section'>See Also:</h5><ul> 031 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ClientVersioning">Client Versioning</a> 032 * </ul> 033 */ 034public class ClientVersionMatcher extends RestMatcher { 035 036 private static final AnnotationProvider AP = AnnotationProvider.INSTANCE; 037 038 private final String clientVersionHeader; 039 private final VersionRange range; 040 041 /** 042 * Constructor. 043 * 044 * @param clientVersionHeader 045 * The HTTP request header name containing the client version. 046 * If <jk>null</jk> or an empty string, uses <js>"Client-Version"</js> 047 * @param mi The Java method info that this matcher is being applied to (used to resolve the 048 * {@code @RestOp(clientVersion)} annotation value). 049 */ 050 public ClientVersionMatcher(String clientVersionHeader, MethodInfo mi) { 051 this.clientVersionHeader = isEmpty(clientVersionHeader) ? "Client-Version" : clientVersionHeader; 052 // @formatter:off 053 var clientVersion = AP.find(mi) 054 .stream() 055 .filter(REST_OP_GROUP) 056 .flatMap(ai -> ai.getValue(String.class, "clientVersion").stream()) 057 .filter(NOT_EMPTY) 058 .findFirst() 059 .orElse(null); 060 // @formatter:on 061 range = new VersionRange(clientVersion); 062 } 063 064 @Override /* Overridden from RestMatcher */ 065 public boolean matches(HttpServletRequest req) { 066 return range.matches(req.getHeader(clientVersionHeader)); 067 } 068 069 @Override /* Overridden from RestMatcher */ 070 public boolean required() { 071 return true; 072 } 073}