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