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 org.apache.juneau.*;
020import org.apache.juneau.commons.reflect.*;
021import org.apache.juneau.http.annotation.*;
022import org.apache.juneau.httppart.bean.*;
023import org.apache.juneau.rest.*;
024import org.apache.juneau.rest.annotation.*;
025
026/**
027 * Resolves method parameters annotated with {@link Request} 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#getRequest(RequestBeanMeta) getRequest}(<jv>meta</jv>);
035 * </p>
036 *
037 * <p>
038 * {@link RequestBeanMeta meta} is derived from the {@link Request} annotation and context configuration.
039 *
040 * <h5 class='section'>See Also:</h5><ul>
041 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RequestBeans">Request Beans</a>
042 * </ul>
043 */
044public class RequestBeanArg implements RestOpArg {
045
046   private static final AnnotationProvider AP = AnnotationProvider.INSTANCE;
047
048   /**
049    * Static creator.
050    *
051    * @param paramInfo The Java method parameter being resolved.
052    * @param annotations The annotations to apply to any new part parsers.
053    * @return A new {@link RequestBeanArg}, or <jk>null</jk> if the parameter is not annotated with {@link Request}.
054    */
055   public static RequestBeanArg create(ParameterInfo paramInfo, AnnotationWorkList annotations) {
056      if (AP.has(Request.class, paramInfo))
057         return new RequestBeanArg(paramInfo, annotations);
058      return null;
059   }
060
061   private final RequestBeanMeta meta;
062
063   /**
064    * Constructor.
065    *
066    * @param paramInfo The Java method parameter being resolved.
067    * @param annotations The annotations to apply to any new part parsers.
068    */
069   protected RequestBeanArg(ParameterInfo paramInfo, AnnotationWorkList annotations) {
070      this.meta = RequestBeanMeta.create(paramInfo, annotations);
071   }
072
073   @Override /* Overridden from RestOpArg */
074   public Object resolve(RestOpSession opSession) throws Exception {
075      return opSession.getRequest().getRequest(meta);
076   }
077}