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.Shorts.*;
020
021import org.apache.juneau.commons.inject.*;
022import org.apache.juneau.commons.reflect.*;
023import org.apache.juneau.rest.server.*;
024
025/**
026 * Resolves method parameters on {@link RestOp}-annotated Java methods by retrieving them by type from the REST object bean store.
027 *
028 * <p>
029 * The parameter value is resolved using:
030 * <p class='bjava'>
031 *    <jv>opSession</jv>
032 *       .{@link RestOpSession#getBeanStore() getBeanStore}()
033 *       .{@link BeanStore#getBean(Class) getBean}(<jv>type</jv>);
034 * </p>
035 *
036 * <p>
037 * This is the default parameter resolver if no other applicable parameter resolvers could be found.
038 *
039 * <h5 class='section'>See Also:</h5><ul>
040 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JavaMethodParameters">Java Method Parameters</a>
041 * </ul>
042 */
043public class DefaultArg implements RestOpArg {
044
045   /**
046    * Static creator.
047    *
048    * @param paramInfo The Java method parameter being resolved. Must not be <jk>null</jk>.
049    * @return A new {@link DefaultArg}, never <jk>null</jk>.
050    */
051   public static DefaultArg create(ParameterInfo paramInfo) {
052      return new DefaultArg(paramInfo);
053   }
054
055   private final Class<?> type;
056   private final String qualifier;
057
058   private final ParameterInfo paramInfo;
059
060   /**
061    * Constructor.
062    *
063    * @param paramInfo The Java method parameter being resolved. Must not be <jk>null</jk>.
064    */
065   protected DefaultArg(ParameterInfo paramInfo) {
066      this.type = paramInfo.getParameterType().inner();
067      this.paramInfo = paramInfo;
068      this.qualifier = paramInfo.getResolvedQualifier();
069   }
070
071   @Override /* Overridden from RestOpArg */
072   @SuppressWarnings({
073      "resource" // Bean values may be AutoCloseable but lifecycle is owned by the container, not this resolver.
074   })
075   public Object resolve(RestOpSession opSession) throws Exception {
076      return opSession.getBeanStore().getBean(type, qualifier).orElseThrow(() -> new ArgException(paramInfo, "Could not resolve bean type %s", cn(type)));
077   }
078}