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