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.util.logging.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.config.*;
023import org.apache.juneau.cp.*;
024import org.apache.juneau.encoders.*;
025import org.apache.juneau.reflect.*;
026import org.apache.juneau.rest.*;
027import org.apache.juneau.rest.annotation.*;
028import org.apache.juneau.rest.debug.*;
029import org.apache.juneau.rest.logger.*;
030import org.apache.juneau.rest.staticfile.*;
031import org.apache.juneau.rest.stats.*;
032import org.apache.juneau.utils.*;
033
034/**
035 * Resolves method parameters on {@link RestOp}-annotated Java methods of types found on the {@link RestContext} object.
036 *
037 * <ul class='javatree'>
038 *    <li class='jc'>{@link BeanContext}
039 *    <li class='jc'>{@link Config}
040 *    <li class='jc'>{@link DebugEnablement}
041 *    <li class='jc'>{@link EncoderSet}
042 *    <li class='jc'>{@link FileFinder}
043 *    <li class='jc'>{@link Logger}
044 *    <li class='jc'>{@link MethodExecStore}
045 *    <li class='jc'>{@link RestChildren}
046 *    <li class='jc'>{@link RestContext}
047 *    <li class='jc'>{@link RestContextStats}
048 *    <li class='jc'>{@link CallLogger}
049 *    <li class='jc'>{@link RestOperations}
050 *    <li class='jc'>{@link StaticFiles}
051 *    <li class='jc'>{@link ThrownStore}
052 * </ul>
053 *
054 * <h5 class='section'>See Also:</h5><ul>
055 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JavaMethodParameters">Java Method Parameters</a>
056 * </ul>
057 */
058public class RestContextArgs extends SimpleRestOperationArg {
059
060   /**
061    * Static creator.
062    *
063    * @param paramInfo The Java method parameter being resolved.
064    * @return A new arg, or <jk>null</jk> if the parameter type is not one of the supported types.
065    */
066   public static RestContextArgs create(ParamInfo paramInfo) {
067      if (paramInfo.isType(BeanContext.class))
068         return new RestContextArgs(RestContext::getBeanContext);
069      if (paramInfo.isType(Config.class))
070         return new RestContextArgs(RestContext::getConfig);
071      if (paramInfo.isType(DebugEnablement.class))
072         return new RestContextArgs(RestContext::getDebugEnablement);
073      if (paramInfo.isType(EncoderSet.class))
074         return new RestContextArgs(RestContext::getEncoders);
075      if (paramInfo.isType(Logger.class))
076         return new RestContextArgs(RestContext::getLogger);
077      if (paramInfo.isType(MethodExecStore.class))
078         return new RestContextArgs(RestContext::getMethodExecStore);
079      if (paramInfo.isType(RestChildren.class))
080         return new RestContextArgs(RestContext::getRestChildren);
081      if (paramInfo.isType(RestContext.class))
082         return new RestContextArgs(x->x);
083      if (paramInfo.isType(RestContextStats.class))
084         return new RestContextArgs(RestContext::getStats);
085      if (paramInfo.isType(CallLogger.class))
086         return new RestContextArgs(RestContext::getCallLogger);
087      if (paramInfo.isType(RestOperations.class))
088         return new RestContextArgs(RestContext::getRestOperations);
089      if (paramInfo.isType(StaticFiles.class))
090         return new RestContextArgs(RestContext::getStaticFiles);
091      if (paramInfo.isType(ThrownStore.class))
092         return new RestContextArgs(RestContext::getThrownStore);
093      return null;
094   }
095
096   /**
097    * Constructor.
098    *
099    * @param <T> The function return type.
100    * @param function The function for finding the arg.
101    */
102   protected <T> RestContextArgs(ThrowingFunction<RestContext,T> function) {
103      super(session -> function.apply(session.getRestContext()));
104   }
105}