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