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