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