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.lang.reflect.*;
020
021import org.apache.juneau.commons.lang.*;
022import org.apache.juneau.commons.reflect.*;
023import org.apache.juneau.http.*;
024import org.apache.juneau.httppart.bean.*;
025import org.apache.juneau.marshall.*;
026import org.apache.juneau.rest.server.*;
027
028/**
029 * Resolves method parameters and parameter types annotated with {@link Response} on {@link RestOp}-annotated Java methods.
030 *
031 * <p>
032 * The parameter value must be of type {@link org.apache.juneau.commons.inject.Value} that accepts a value that is then set via:
033 * <p class='bjava'>
034 *    <jv>opSession</jv>
035 *       .{@link RestOpSession#getResponse() getResponse}()
036 *       .{@link RestResponse#setContent(Object) setContent}(<jv>value</jv>);
037 * </p>
038 *
039 * <h5 class='section'>See Also:</h5><ul>
040 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/ResponseBeans">@Response Beans</a>
041 * </ul>
042 */
043public class ResponseBeanArg implements RestOpArg {
044
045   private static final AnnotationProvider AP = AnnotationProvider.INSTANCE;
046
047   /**
048    * Static creator.
049    *
050    * @param paramInfo The Java method parameter being resolved. Must not be <jk>null</jk>.
051    * @param annotations The annotations to apply to any new part parsers.
052    * @return A new {@link ResponseBeanArg}, or <jk>null</jk> if the parameter is not annotated with {@link Response}.
053    */
054   public static ResponseBeanArg create(ParameterInfo paramInfo, AnnotationWorkList annotations) {
055      if (AP.has(Response.class, paramInfo))
056         return new ResponseBeanArg(paramInfo, annotations);
057      return null;
058   }
059
060   final ResponseBeanMeta meta;
061
062   private final Type type;
063
064   /**
065    * Constructor.
066    *
067    * @param paramInfo The Java method parameter being resolved. Must not be <jk>null</jk>.
068    * @param annotations The annotations to apply to any new part parsers.
069    */
070   protected ResponseBeanArg(ParameterInfo paramInfo, AnnotationWorkList annotations) {
071      this.type = paramInfo.getParameterType().innerType();
072      this.meta = ResponseBeanMeta.create(paramInfo, annotations);
073      Class<?> c;
074      if (type instanceof Class) {
075         c = (Class<?>)type;
076      } else if (type instanceof ParameterizedType) {
077         c = (Class<?>)((ParameterizedType)type).getRawType();
078      } else {
079         c = null;
080      }
081      if (c != Holder.class)
082         throw new ArgException(paramInfo, "Type must be Holder<?> on parameter annotated with @Response annotation");
083   }
084
085   @Override /* Overridden from RestOpArg */
086   public Object resolve(RestOpSession opSession) throws Exception {
087      var v = new Holder<>();
088      v.listener(o -> {
089         RestRequest req = opSession.getRequest();
090         RestResponse res = opSession.getResponse();
091         ResponseBeanMeta meta2 = req.getOpContext().getResponseBeanMeta(o);
092         if (meta2 == null)
093            meta2 = meta;
094         res.setResponseBeanMeta(meta2);
095         res.setContent(o);
096      });
097      return v;
098   }
099}