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.lang.reflect.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.http.annotation.*;
023import org.apache.juneau.httppart.bean.*;
024import org.apache.juneau.reflect.*;
025import org.apache.juneau.rest.*;
026import org.apache.juneau.rest.annotation.*;
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 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) setOutput}(<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   final ResponseBeanMeta meta;
045   private final Type type;
046
047   /**
048    * Static creator.
049    *
050    * @param paramInfo The Java method parameter being resolved.
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(ParamInfo paramInfo, AnnotationWorkList annotations) {
055      if (paramInfo.hasAnnotation(Response.class) || paramInfo.getParameterType().hasAnnotation(Response.class))
056         return new ResponseBeanArg(paramInfo, annotations);
057      return null;
058   }
059
060   /**
061    * Constructor.
062    *
063    * @param paramInfo The Java method parameter being resolved.
064    * @param annotations The annotations to apply to any new part parsers.
065    */
066   protected ResponseBeanArg(ParamInfo paramInfo, AnnotationWorkList annotations) {
067      this.type = paramInfo.getParameterType().innerType();
068      this.meta = ResponseBeanMeta.create(paramInfo, annotations);
069      Class<?> c = type instanceof Class ? (Class<?>)type : type instanceof ParameterizedType ? (Class<?>)((ParameterizedType)type).getRawType() : null;
070      if (c != Value.class)
071         throw new ArgException(paramInfo, "Type must be Value<?> on parameter annotated with @Response annotation");
072   }
073
074   @SuppressWarnings({ "unchecked", "rawtypes" })
075   @Override /* RestOpArg */
076   public Object resolve(final RestOpSession opSession) throws Exception {
077      Value<Object> v = new Value();
078      v.listener(o -> {
079         RestRequest req = opSession.getRequest();
080         RestResponse res = opSession.getResponse();
081         ResponseBeanMeta meta = req.getOpContext().getResponseBeanMeta(o);
082         if (meta == null)
083            meta = ResponseBeanArg.this.meta;
084         res.setResponseBeanMeta(meta);
085         res.setContent(o);
086        });
087      return v;
088   }
089}