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.Utils.*; 020 021import org.apache.juneau.*; 022import org.apache.juneau.reflect.*; 023import org.apache.juneau.rest.*; 024import org.apache.juneau.rest.annotation.*; 025import org.apache.juneau.rest.httppart.*; 026 027/** 028 * Resolves method parameters and parameter types annotated with {@link Attr} on {@link RestOp}-annotated Java methods. 029 * 030 * <p> 031 * The parameter value is resolved using: 032 * <p class='bjava'> 033 * <jv>opSession</jv> 034 * .{@link RestOpSession#getRequest() getRequest}() 035 * .{@link RestRequest#getAttributes() getAttributes}() 036 * .{@link RequestAttributes#get(String) get}(<jv>name</jv>) 037 * .{@link RequestAttribute#as(Class) as}(<jv>type</jv>); 038 * </p> 039 * 040 * <h5 class='section'>See Also:</h5><ul> 041 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JavaMethodParameters">Java Method Parameters</a> 042 * </ul> 043 */ 044public class AttributeArg implements RestOpArg { 045 046 private final String name; 047 private final Class<?> type; 048 049 /** 050 * Static creator. 051 * 052 * @param paramInfo The Java method parameter being resolved. 053 * @return A new {@link AttributeArg}, or <jk>null</jk> if the parameter is not annotated with {@link Attr}. 054 */ 055 public static AttributeArg create(ParamInfo paramInfo) { 056 if (paramInfo.hasAnnotation(Attr.class) || paramInfo.getParameterType().hasAnnotation(Attr.class)) 057 return new AttributeArg(paramInfo); 058 return null; 059 } 060 061 /** 062 * Constructor. 063 * 064 * @param paramInfo The Java method parameter being resolved. 065 */ 066 protected AttributeArg(ParamInfo paramInfo) { 067 this.name = getName(paramInfo); 068 this.type = paramInfo.getParameterType().inner(); 069 } 070 071 private String getName(ParamInfo paramInfo) { 072 Value<String> n = Value.empty(); 073 paramInfo.forEachAnnotation(Attr.class, x -> isNotEmpty(x.name()), x -> n.set(x.name())); 074 paramInfo.forEachAnnotation(Attr.class, x -> isNotEmpty(x.value()), x -> n.set(x.value())); 075 if (n.isEmpty()) 076 throw new ArgException(paramInfo, "@Attr used without name or value"); 077 return n.get(); 078 } 079 080 @Override /* RestOpArg */ 081 public Object resolve(RestOpSession opSession) throws Exception { 082 return opSession.getRequest().getAttribute(name).as(type).orElse(null); 083 } 084}