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 HasFormData} 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#getFormParams() getFormParams}()
037 *       .{@link RequestFormParamList#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 HasFormDataArg 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 HasFormDataArg}, or <jk>null</jk> if the parameter is not annotated with {@link HasFormData}.
056    */
057   public static HasFormDataArg create(ParameterInfo paramInfo) {
058      if (AP.has(HasFormData.class, paramInfo))
059         return new HasFormDataArg(paramInfo);
060      return null;
061   }
062
063   private static String getName(HasFormData x) {
064      return firstNonEmpty(x.name(), x.value());
065   }
066
067   private static boolean hasName(HasFormData x) {
068      return isAnyNotBlank(x.name(), x.value());
069   }
070
071   private final String name;
072   private final Type type;
073
074   /**
075    * Constructor.
076    *
077    * @param pi The Java method parameter being resolved. Must not be <jk>null</jk>.
078    */
079   protected HasFormDataArg(ParameterInfo pi) {
080      // @formatter:off
081      this.name = AP.find(HasFormData.class, pi)
082         .stream()
083         .map(AnnotationInfo::inner)
084         .filter(HasFormDataArg::hasName)
085         .findFirst()
086         .map(HasFormDataArg::getName)
087         .orElseThrow(() -> new ArgException(pi, "@HasFormData used without name or value"));
088      // @formatter:on
089      this.type = pi.getParameterType().innerType();
090   }
091
092   @Override /* Overridden from RestOpArg */
093   public Object resolve(RestOpSession opSession) throws Exception {
094      var req = opSession.getRequest();
095      var bs = req.getMarshallingSession();
096      return bs.convertToType(req.getFormParams().contains(name), bs.getClassMeta(type));
097   }
098}