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