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