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