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.security.*;
020
021import org.apache.juneau.reflect.*;
022import org.apache.juneau.rest.annotation.*;
023import org.apache.juneau.utils.*;
024
025import jakarta.servlet.*;
026import jakarta.servlet.http.*;
027
028/**
029 * Resolves method parameters on {@link RestOp}-annotated Java methods of types found on the {@link HttpServletRequest} object.
030 *
031 * <ul class='javatree'>
032 *    <li class='jc'>{@link AsyncContext}
033 *    <li class='jc'><c>{@link Cookie}[]</c>
034 *    <li class='jc'>{@link DispatcherType}
035 *    <li class='jc'>{@link HttpServletRequest}
036 *    <li class='jc'>{@link Principal}
037 * </ul>
038 *
039 * <h5 class='section'>See Also:</h5><ul>
040 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JavaMethodParameters">Java Method Parameters</a>
041 * </ul>
042 */
043public class HttpServletRequestArgs extends SimpleRestOperationArg {
044
045   /**
046    * Static creator.
047    *
048    * @param paramInfo The Java method parameter being resolved.
049    * @return A new arg, or <jk>null</jk> if the parameter type is not one of the supported types.
050    */
051   public static HttpServletRequestArgs create(ParamInfo paramInfo) {
052      if (paramInfo.isType(AsyncContext.class))
053         return new HttpServletRequestArgs(HttpServletRequest::getAsyncContext);
054      if (paramInfo.isType(CookieList.class))
055         return new HttpServletRequestArgs(x->CookieList.of(x.getCookies()));
056      if (paramInfo.isType(DispatcherType.class))
057         return new HttpServletRequestArgs(HttpServletRequest::getDispatcherType);
058      if (paramInfo.isType(HttpServletRequest.class))
059         return new HttpServletRequestArgs(x->x);
060      if (paramInfo.isType(Principal.class))
061         return new HttpServletRequestArgs(HttpServletRequest::getUserPrincipal);
062      return null;
063   }
064
065   /**
066    * Constructor.
067    *
068    * @param <T> The function return type.
069    * @param function The function for finding the arg.
070    */
071   protected <T> HttpServletRequestArgs(ThrowingFunction<HttpServletRequest,T> function) {
072      super(session -> function.apply(session.getRequest().getHttpServletRequest()));
073   }
074}