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 java.lang.reflect.*;
020
021import org.apache.juneau.http.annotation.*;
022import org.apache.juneau.httppart.*;
023import org.apache.juneau.reflect.*;
024import org.apache.juneau.rest.*;
025import org.apache.juneau.rest.annotation.*;
026import org.apache.juneau.rest.httppart.*;
027
028/**
029 * Resolves method parameters and parameter types annotated with {@link Content} on {@link RestOp}-annotated Java methods.
030 *
031 * <p>
032 * The parameter value is resolved using:
033 * <p class='bjava'>
034 *    <jv>opSession</jv>
035 *       .{@link RestOpSession#getRequest() getRequest}()
036 *       .{@link RestRequest#getContent() getContent}()
037 *       .{@link RequestContent#setSchema(HttpPartSchema) setSchema}(<jv>schema</jv>)
038 *       .{@link RequestContent#as(Type,Type...) as}(<jv>type</jv>);
039 * </p>
040 *
041 * <p>
042 * {@link HttpPartSchema schema} is derived from the {@link Content} annotation.
043 *
044 * <h5 class='section'>See Also:</h5><ul>
045 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JavaMethodParameters">Java Method Parameters</a>
046 * </ul>
047 */
048public class ContentArg implements RestOpArg {
049
050   private final HttpPartSchema schema;
051   private final Type type;
052
053   /**
054    * Static creator.
055    *
056    * @param paramInfo The Java method parameter being resolved.
057    * @return A new {@link ContentArg}, or <jk>null</jk> if the parameter is not annotated with {@link Content}.
058    */
059   public static ContentArg create(ParamInfo paramInfo) {
060      if (paramInfo.hasAnnotation(Content.class) || paramInfo.getParameterType().hasAnnotation(Content.class))
061         return new ContentArg(paramInfo);
062      return null;
063   }
064
065   /**
066    * Constructor.
067    *
068    * @param paramInfo The Java method parameter being resolved.
069    */
070   protected ContentArg(ParamInfo paramInfo) {
071      this.type = paramInfo.getParameterType().innerType();
072      this.schema = HttpPartSchema.create(Content.class, paramInfo);
073   }
074
075   @Override /* RestOpArg */
076   public Object resolve(RestOpSession opSession) throws Exception {
077      return opSession.getRequest().getContent().setSchema(schema).as(type);
078   }
079}