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 org.apache.juneau.*;
016import org.apache.juneau.http.annotation.*;
017import org.apache.juneau.httppart.bean.*;
018import org.apache.juneau.reflect.*;
019import org.apache.juneau.rest.*;
020import org.apache.juneau.rest.annotation.*;
021
022/**
023 * Resolves method parameters annotated with {@link Request} on {@link RestOp}-annotated Java methods.
024 *
025 * <p>
026 * The parameter value is resolved using:
027 * <p class='bjava'>
028 *    <jv>opSession</jv>
029 *       .{@link RestOpSession#getRequest() getRequest}()
030 *       .{@link RestRequest#getRequest(RequestBeanMeta) getRequest}(<jv>meta</jv>);
031 * </p>
032 *
033 * <p>
034 * {@link RequestBeanMeta meta} is derived from the {@link Request} annotation and context configuration.
035 *
036 * <h5 class='section'>See Also:</h5><ul>
037 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.RequestBeans">@Request Beans</a>
038 * </ul>
039 */
040public class RequestBeanArg implements RestOpArg {
041   private final RequestBeanMeta meta;
042
043   /**
044    * Static creator.
045    *
046    * @param paramInfo The Java method parameter being resolved.
047    * @param annotations The annotations to apply to any new part parsers.
048    * @return A new {@link RequestBeanArg}, or <jk>null</jk> if the parameter is not annotated with {@link Request}.
049    */
050   public static RequestBeanArg create(ParamInfo paramInfo, AnnotationWorkList annotations) {
051      if (paramInfo.hasAnnotation(Request.class))
052         return new RequestBeanArg(paramInfo, annotations);
053      return null;
054   }
055
056   /**
057    * Constructor.
058    *
059    * @param paramInfo The Java method parameter being resolved.
060    * @param annotations The annotations to apply to any new part parsers.
061    */
062   protected RequestBeanArg(ParamInfo paramInfo, AnnotationWorkList annotations) {
063      this.meta = RequestBeanMeta.create(paramInfo, annotations);
064   }
065
066   @Override /* RestOpArg */
067   public Object resolve(RestOpSession opSession) throws Exception {
068      return opSession.getRequest().getRequest(meta);
069   }
070}