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