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.io.*;
020import java.util.*;
021
022import org.apache.juneau.bean.swagger.Swagger;
023import org.apache.juneau.commons.function.*;
024import org.apache.juneau.commons.reflect.ParameterInfo;
025import org.apache.juneau.commons.svl.*;
026import org.apache.juneau.marshall.*;
027import org.apache.juneau.marshall.cp.*;
028import org.apache.juneau.marshall.httppart.*;
029import org.apache.juneau.rest.server.*;
030import org.apache.juneau.rest.server.httppart.*;
031
032import jakarta.servlet.*;
033import jakarta.servlet.http.*;
034
035/**
036 * Resolves method parameters on {@link RestOp}-annotated Java methods of types found on the {@link RestRequest} object.
037 *
038 * <ul class='javatree'>
039 *    <li class='jc'>{@link HttpServletRequest}
040 *    <li class='jc'>{@link HttpPartParserSession}
041 *    <li class='jc'>{@link HttpPartSerializerSession}
042 *    <li class='jc'>{@link InputStream}
043 *    <li class='jc'>{@link Locale}
044 *    <li class='jc'>{@link Messages}
045 *    <li class='jc'>{@link Reader}
046 *    <li class='jc'>{@link RequestAttributes}
047 *    <li class='jc'>{@link RequestContent}
048 *    <li class='jc'>{@link RequestFormParamList}
049 *    <li class='jc'>{@link RequestHeaderList}
050 *    <li class='jc'>{@link RequestPathParamList}
051 *    <li class='jc'>{@link RequestQueryParamList}
052 *    <li class='jc'>{@link ResourceBundle}
053 *    <li class='jc'>{@link RestRequest}
054 *    <li class='jc'>{@link ServletInputStream}
055 *    <li class='jc'>{@link Swagger}
056 *    <li class='jc'>{@link TimeZone}
057 *    <li class='jc'>{@link UriContext}
058 *    <li class='jc'>{@link UriResolver}
059 *    <li class='jc'>{@link VarResolverSession}
060 * </ul>
061 *
062 * <h5 class='section'>See Also:</h5><ul>
063 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JavaMethodParameters">Java Method Parameters</a>
064 * </ul>
065 */
066public class RestRequestArgs extends SimpleRestOperationArg {
067
068   /**
069    * Static creator.
070    *
071    * @param paramInfo The Java method parameter being resolved. Must not be <jk>null</jk>.
072    * @return A new arg, or <jk>null</jk> if the parameter type is not one of the supported types.
073    */
074   @SuppressWarnings({
075      "java:S3776" // Cognitive complexity acceptable for REST request argument creation
076   })
077   public static RestRequestArgs create(ParameterInfo paramInfo) {
078      if (paramInfo.isType(HttpPartParserSession.class))
079         return new RestRequestArgs(RestRequest::getPartParserSession);
080      if (paramInfo.isType(HttpPartSerializerSession.class))
081         return new RestRequestArgs(RestRequest::getPartSerializerSession);
082      if (paramInfo.isType(InputStream.class))
083         return new RestRequestArgs(RestRequest::getInputStream);
084      if (paramInfo.isType(Locale.class))
085         return new RestRequestArgs(RestRequest::getLocale);
086      if (paramInfo.isType(Messages.class))
087         return new RestRequestArgs(RestRequest::getMessages);
088      if (paramInfo.isType(Reader.class))
089         return new RestRequestArgs(RestRequest::getReader);
090      if (paramInfo.isType(RequestAttributes.class))
091         return new RestRequestArgs(RestRequest::getAttributes);
092      if (paramInfo.isType(RequestContent.class))
093         return new RestRequestArgs(RestRequest::getContent);
094      if (paramInfo.isType(RequestFormParamList.class))
095         return new RestRequestArgs(RestRequest::getFormParams);
096      if (paramInfo.isType(RequestHeaderList.class))
097         return new RestRequestArgs(RestRequest::getHeaders);
098      if (paramInfo.isType(RequestPathParamList.class))
099         return new RestRequestArgs(RestRequest::getPathParams);
100      if (paramInfo.isType(RequestQueryParamList.class))
101         return new RestRequestArgs(RestRequest::getQueryParams);
102      if (paramInfo.isType(ResourceBundle.class))
103         return new RestRequestArgs(RestRequest::getMessages);
104      if (paramInfo.isType(RestRequest.class))
105         return new RestRequestArgs(x -> x);
106      if (paramInfo.isType(ServletInputStream.class))
107         return new RestRequestArgs(RestRequest::getInputStream);
108      if (paramInfo.isType(Swagger.class))
109         return new RestRequestArgs(x -> x.getSwagger().orElse(null));
110      if (paramInfo.isType(TimeZone.class))
111         return new RestRequestArgs(x -> x.getTimeZone().orElse(null));
112      if (paramInfo.isType(UriContext.class))
113         return new RestRequestArgs(RestRequest::getUriContext);
114      if (paramInfo.isType(UriResolver.class))
115         return new RestRequestArgs(RestRequest::getUriResolver);
116      if (paramInfo.isType(VarResolverSession.class))
117         return new RestRequestArgs(RestRequest::getVarResolverSession);
118      return null;
119   }
120
121   /**
122    * Constructor.
123    *
124    * @param <T> The function return type.
125    * @param function The function for finding the arg.
126    */
127   protected <T> RestRequestArgs(ThrowingFunction<RestRequest,T> function) {
128      super(session -> function.apply(session.getRequest()));
129   }
130}