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.server.arg;
018
019import java.lang.reflect.*;
020
021import org.apache.juneau.commons.reflect.*;
022import org.apache.juneau.http.*;
023import org.apache.juneau.marshall.httppart.*;
024import org.apache.juneau.rest.server.*;
025import org.apache.juneau.rest.server.httppart.*;
026import org.apache.juneau.rest.server.validation.*;
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 static final AnnotationProvider AP = AnnotationProvider.INSTANCE;
051
052   /**
053    * Static creator.
054    *
055    * @param paramInfo The Java method parameter being resolved. Must not be <jk>null</jk>.
056    * @return A new {@link ContentArg}, or <jk>null</jk> if the parameter is not annotated with {@link Content}.
057    */
058   public static ContentArg create(ParameterInfo paramInfo) {
059      if (AP.has(Content.class, paramInfo))
060         return new ContentArg(paramInfo);
061      return null;
062   }
063
064   private final HttpPartSchema schema;
065
066   private final Type type;
067
068   /**
069    * Pre-computed flag &mdash; {@code true} iff this parameter carries a Jakarta Bean Validation
070    * {@code @Valid} (or equivalent) marker. Validation is off by default; only parameters that opt in
071    * pay the per-request {@link BeanValidator#validate(Object, org.apache.juneau.commons.inject.BeanStore)}
072    * cost.
073    */
074   private final boolean validate;
075
076   /**
077    * Constructor.
078    *
079    * @param paramInfo The Java method parameter being resolved. Must not be <jk>null</jk>.
080    */
081   protected ContentArg(ParameterInfo paramInfo) {
082      this.type = paramInfo.getParameterType().innerType();
083      this.schema = HttpPartSchema.create(Content.class, paramInfo);
084      this.validate = BeanValidator.isValidationRequested(paramInfo);
085   }
086
087   @SuppressWarnings({
088      "resource" // as(Type,Type...) is generic and its inferred type is conservatively treated as Closeable; the returned content (e.g. an InputStream) is container-managed and handed to the invoked op method, not owned/closed here.
089   })
090   @Override /* Overridden from RestOpArg */
091   public Object resolve(RestOpSession opSession) throws Exception {
092      var bean = opSession.getRequest().getContent().setSchema(schema).as(type);
093      return validate ? BeanValidator.validate(bean, opSession.getBeanStore()) : bean;
094   }
095}