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 org.apache.juneau.commons.reflect.*; 020import org.apache.juneau.http.*; 021import org.apache.juneau.httppart.bean.*; 022import org.apache.juneau.marshall.*; 023import org.apache.juneau.rest.server.*; 024import org.apache.juneau.rest.server.validation.*; 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,RequestBeanSetters) getRequest}(<jv>meta</jv>, <jv>setters</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. Must not be <jk>null</jk>. 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 * Pre-computed flag — {@code true} iff this parameter carries a Jakarta Bean Validation 065 * {@code @Valid} (or equivalent) marker. Off-by-default opt-in — see {@link BeanValidator}. 066 */ 067 private final boolean validate; 068 069 /** 070 * The resolved constructor and setters for a concrete {@link Request @Request} bean class, or {@code null} for 071 * an interface bean (proxied; never needs a setter). Resolved eagerly here, not lazily on the first matching 072 * request, so a bean with a missing setter or constructor fails {@code @RestOp} registration (servlet init). 073 */ 074 private final RequestBeanSetters setters; 075 076 /** 077 * Constructor. 078 * 079 * @param paramInfo The Java method parameter being resolved. Must not be <jk>null</jk>. 080 * @param annotations The annotations to apply to any new part parsers. 081 * @throws IllegalArgumentException If the bean is a concrete class with no public no-arg constructor, or with an 082 * annotated getter that has no matching setter (see {@link RequestBeanSetters#resolve}). 083 */ 084 protected RequestBeanArg(ParameterInfo paramInfo, AnnotationWorkList annotations) { 085 this.meta = RequestBeanMeta.create(paramInfo, annotations); 086 this.validate = BeanValidator.isValidationRequested(paramInfo); 087 var beanClass = meta.getBeanInfo().inner(); 088 this.setters = beanClass.isInterface() ? null : RequestBeanSetters.resolve(beanClass, meta); 089 } 090 091 @SuppressWarnings({ 092 "resource" // getRequest(meta, setters) may back a property with a request content stream; that stream is container-managed and handed to the invoked op method, not owned/closed here. 093 }) 094 @Override /* Overridden from RestOpArg */ 095 public Object resolve(RestOpSession opSession) throws Exception { 096 var bean = opSession.getRequest().getRequest(meta, setters); 097 return validate ? BeanValidator.validate(bean, opSession.getBeanStore()) : bean; 098 } 099}