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