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.commons.utils.StringUtils.*; 020 021import org.apache.juneau.commons.reflect.*; 022import org.apache.juneau.rest.*; 023import org.apache.juneau.rest.annotation.*; 024import org.apache.juneau.rest.httppart.*; 025 026/** 027 * Resolves method parameters and parameter types annotated with {@link Attr} on {@link RestOp}-annotated Java methods. 028 * 029 * <p> 030 * The parameter value is resolved using: 031 * <p class='bjava'> 032 * <jv>opSession</jv> 033 * .{@link RestOpSession#getRequest() getRequest}() 034 * .{@link RestRequest#getAttributes() getAttributes}() 035 * .{@link RequestAttributes#get(String) get}(<jv>name</jv>) 036 * .{@link RequestAttribute#as(Class) as}(<jv>type</jv>); 037 * </p> 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 AttributeArg implements RestOpArg { 044 045 private static final AnnotationProvider AP = AnnotationProvider.INSTANCE; 046 047 /** 048 * Static creator. 049 * 050 * @param paramInfo The Java method parameter being resolved. 051 * @return A new {@link AttributeArg}, or <jk>null</jk> if the parameter is not annotated with {@link Attr}. 052 */ 053 public static AttributeArg create(ParameterInfo paramInfo) { 054 if (AP.has(Attr.class, paramInfo)) 055 return new AttributeArg(paramInfo); 056 return null; 057 } 058 059 private final String name; 060 061 private final Class<?> type; 062 063 /** 064 * Constructor. 065 * 066 * @param paramInfo The Java method parameter being resolved. 067 */ 068 protected AttributeArg(ParameterInfo paramInfo) { 069 this.name = getName(paramInfo); 070 this.type = paramInfo.getParameterType().inner(); 071 } 072 073 @Override /* Overridden from RestOpArg */ 074 public Object resolve(RestOpSession opSession) throws Exception { 075 return opSession.getRequest().getAttribute(name).as(type).orElse(null); 076 } 077 078 private static String getName(ParameterInfo paramInfo) { 079 // @formatter:off 080 return AP.find(Attr.class, paramInfo) 081 .stream() 082 .map(AnnotationInfo::inner) 083 .filter(x -> isAnyNotBlank(x.value(), x.name())) 084 .findFirst() 085 .map(x -> firstNonBlank(x.name(), x.value())) 086 .orElseThrow(() -> new ArgException(paramInfo, "@Attr used without name or value")); 087 // @formatter:on 088 } 089}