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.arg; 014 015import static org.apache.juneau.common.internal.StringUtils.*; 016 017import java.lang.reflect.*; 018 019import org.apache.juneau.*; 020import org.apache.juneau.http.annotation.*; 021import org.apache.juneau.reflect.*; 022import org.apache.juneau.rest.*; 023import org.apache.juneau.rest.annotation.*; 024import org.apache.juneau.rest.httppart.*; 025 026/** 027 * Resolves method parameters annotated with {@link HasQuery} on {@link RestOp}-annotated Java methods. 028 * 029 * <p> 030 * The parameter value is resolved using: 031 * <p class='bjava'> 032 * <jv>opSession</jv> 033 * .{@link RestOpSession#getRequest() getRequest}() 034 * .{@link RestRequest#getQueryParams() getQueryParams}() 035 * .{@link RequestQueryParams#contains(String) contains}(<jv>name</jv>); 036 * </p> 037 * 038 * <p> 039 * The parameter type can be a <jk>boolean</jk> or anything convertible from a <jk>boolean</jk>. 040 * 041 * <h5 class='section'>See Also:</h5><ul> 042 * <li class='link'><a class="doclink" href="../../../../../index.html#jrs.JavaMethodParameters">Java Method Parameters</a> 043 * </ul> 044 */ 045public class HasQueryArg implements RestOpArg { 046 047 private final String name; 048 private final Type type; 049 050 /** 051 * Static creator. 052 * 053 * @param paramInfo The Java method parameter being resolved. 054 * @return A new {@link HasQueryArg}, or <jk>null</jk> if the parameter is not annotated with {@link HasQuery}. 055 */ 056 public static HasQueryArg create(ParamInfo paramInfo) { 057 if (paramInfo.hasAnnotation(HasQuery.class)) 058 return new HasQueryArg(paramInfo); 059 return null; 060 } 061 062 /** 063 * Constructor. 064 * 065 * @param pi The Java method parameter being resolved. 066 */ 067 protected HasQueryArg(ParamInfo pi) { 068 Value<String> _name = Value.empty(); 069 pi.forEachAnnotation(HasQuery.class, x -> hasName(x), x -> _name.set(getName(x))); 070 this.name = _name.orElseThrow(() -> new ArgException(pi, "@HasQuery used without name or value")); 071 this.type = pi.getParameterType().innerType(); 072 } 073 074 private static boolean hasName(HasQuery x) { 075 return isNotEmpty(x.name()) || isNotEmpty(x.value()); 076 } 077 078 private static String getName(HasQuery x) { 079 return firstNonEmpty(x.name(), x.value()); 080 } 081 082 @Override /* RestOpArg */ 083 public Object resolve(RestOpSession opSession) throws Exception { 084 RestRequest req = opSession.getRequest(); 085 BeanSession bs = req.getBeanSession(); 086 return bs.convertToType(req.getQueryParams().contains(name), bs.getClassMeta(type)); 087 } 088}