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