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 HasFormData} 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#getFormParams() getFormParams}()
038 *       .{@link RequestFormParams#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 HasFormDataArg 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 HasFormDataArg}, or <jk>null</jk> if the parameter is not annotated with {@link HasFormData}.
057    */
058   public static HasFormDataArg create(ParameterInfo paramInfo) {
059      if (AP.has(HasFormData.class, paramInfo))
060         return new HasFormDataArg(paramInfo);
061      return null;
062   }
063
064   private static String getName(HasFormData x) {
065      return firstNonEmpty(x.name(), x.value());
066   }
067
068   private static boolean hasName(HasFormData x) {
069      return isAnyNotBlank(x.name(), x.value());
070   }
071
072   private final String name;
073   private final Type type;
074
075   /**
076    * Constructor.
077    *
078    * @param pi The Java method parameter being resolved.
079    */
080   protected HasFormDataArg(ParameterInfo pi) {
081      // @formatter:off
082      this.name = AP.find(HasFormData.class, pi)
083         .stream()
084         .map(AnnotationInfo::inner)
085         .filter(HasFormDataArg::hasName)
086         .findFirst()
087         .map(HasFormDataArg::getName)
088         .orElseThrow(() -> new ArgException(pi, "@HasFormData 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.getBeanSession();
097      return bs.convertToType(req.getFormParams().contains(name), bs.getClassMeta(type));
098   }
099}