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