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