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.arg; 018 019import static org.apache.juneau.commons.utils.StringUtils.*; 020 021import java.lang.reflect.*; 022 023import org.apache.juneau.commons.reflect.*; 024import org.apache.juneau.http.annotation.*; 025import org.apache.juneau.rest.*; 026import org.apache.juneau.rest.annotation.*; 027import org.apache.juneau.rest.httppart.*; 028 029/** 030 * Resolves method parameters annotated with {@link HasQuery} on {@link RestOp}-annotated Java methods. 031 * 032 * <p> 033 * The parameter value is resolved using: 034 * <p class='bjava'> 035 * <jv>opSession</jv> 036 * .{@link RestOpSession#getRequest() getRequest}() 037 * .{@link RestRequest#getQueryParams() getQueryParams}() 038 * .{@link RequestQueryParams#contains(String) contains}(<jv>name</jv>); 039 * </p> 040 * 041 * <p> 042 * The parameter type can be a <jk>boolean</jk> or anything convertible from a <jk>boolean</jk>. 043 * 044 * <h5 class='section'>See Also:</h5><ul> 045 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JavaMethodParameters">Java Method Parameters</a> 046 * </ul> 047 */ 048public class HasQueryArg implements RestOpArg { 049 050 private static final AnnotationProvider AP = AnnotationProvider.INSTANCE; 051 052 /** 053 * Static creator. 054 * 055 * @param paramInfo The Java method parameter being resolved. 056 * @return A new {@link HasQueryArg}, or <jk>null</jk> if the parameter is not annotated with {@link HasQuery}. 057 */ 058 public static HasQueryArg create(ParameterInfo paramInfo) { 059 if (AP.has(HasQuery.class, paramInfo)) 060 return new HasQueryArg(paramInfo); 061 return null; 062 } 063 064 private static String getName(HasQuery x) { 065 return firstNonEmpty(x.name(), x.value()); 066 } 067 068 private static boolean hasName(HasQuery x) { 069 return isAnyNotBlank(x.name(), x.value()); 070 } 071 072 private final String name; 073 074 private final Type type; 075 076 /** 077 * Constructor. 078 * 079 * @param pi The Java method parameter being resolved. 080 */ 081 protected HasQueryArg(ParameterInfo pi) { 082 // @formatter:off 083 this.name = AP.find(HasQuery.class, pi) 084 .stream() 085 .map(AnnotationInfo::inner) 086 .filter(HasQueryArg::hasName) 087 .findFirst() 088 .map(HasQueryArg::getName) 089 .orElseThrow(() -> new ArgException(pi, "@HasQuery used without name or value")); 090 // @formatter:on 091 this.type = pi.getParameterType().innerType(); 092 } 093 094 @Override /* Overridden from RestOpArg */ 095 public Object resolve(RestOpSession opSession) throws Exception { 096 var req = opSession.getRequest(); 097 var bs = req.getBeanSession(); 098 return bs.convertToType(req.getQueryParams().contains(name), bs.getClassMeta(type)); 099 } 100}