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.annotation.*; 018 019import org.apache.juneau.cp.*; 020import org.apache.juneau.reflect.*; 021import org.apache.juneau.rest.*; 022import org.apache.juneau.rest.annotation.*; 023 024/** 025 * Resolves method parameters on {@link RestOp}-annotated Java methods by retrieving them by type from the REST object bean store. 026 * 027 * <p> 028 * The parameter value is resolved using: 029 * <p class='bjava'> 030 * <jv>opSession</jv> 031 * .{@link RestOpSession#getBeanStore() getBeanStore}() 032 * .{@link BeanStore#getBean(Class) getBean}(<jv>type</jv>); 033 * </p> 034 * 035 * <p> 036 * This is the default parameter resolver if no other applicable parameter resolvers could be found. 037 * 038 * <h5 class='section'>See Also:</h5><ul> 039 * <li class='link'><a class="doclink" href="../../../../../index.html#jrs.JavaMethodParameters">Java Method Parameters</a> 040 * </ul> 041 */ 042public class DefaultArg implements RestOpArg { 043 044 private final Class<?> type; 045 private final String name; 046 private final ParamInfo paramInfo; 047 048 /** 049 * Static creator. 050 * 051 * @param paramInfo The Java method parameter being resolved. 052 * @return A new {@link DefaultArg}, never <jk>null</jk>. 053 */ 054 public static DefaultArg create(ParamInfo paramInfo) { 055 return new DefaultArg(paramInfo); 056 } 057 058 /** 059 * Constructor. 060 * 061 * @param paramInfo The Java method parameter being resolved. 062 */ 063 protected DefaultArg(ParamInfo paramInfo) { 064 this.type = paramInfo.getParameterType().inner(); 065 this.paramInfo = paramInfo; 066 this.name = findBeanName(paramInfo); 067 } 068 069 //----------------------------------------------------------------------------------------------------------------- 070 // Helper methods 071 //----------------------------------------------------------------------------------------------------------------- 072 private String findBeanName(ParamInfo pi) { 073 Annotation n = pi.getAnnotation(Annotation.class, x -> x.annotationType().getSimpleName().equals("Named")); 074 if (n != null) 075 return AnnotationInfo.of((ClassInfo)null, n).getValue(String.class, "value", NOT_EMPTY).orElse(null); 076 return null; 077 } 078 079 @Override /* RestOpArg */ 080 public Object resolve(RestOpSession opSession) throws Exception { 081 return opSession.getBeanStore().getBean(type, name).orElseThrow(()->new ArgException(paramInfo, "Could not resolve bean type {0}", type.getName())); 082 } 083}