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