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.reflect.*;
024import org.apache.juneau.rest.*;
025import org.apache.juneau.rest.annotation.*;
026
027/**
028 * Resolves method parameters annotated with {@link StatusCode} on {@link RestOp}-annotated Java methods.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JavaMethodParameters">Java Method Parameters</a>
032 * </ul>
033 */
034public class ResponseCodeArg implements RestOpArg {
035
036   private final Type type;
037
038   /**
039    * Static creator.
040    *
041    * @param paramInfo The Java method parameter being resolved.
042    * @return A new {@link ResponseCodeArg}, or <jk>null</jk> if the parameter is not annotated with {@link StatusCode}.
043    */
044   public static ResponseCodeArg create(ParamInfo paramInfo) {
045      if (paramInfo.hasAnnotation(StatusCode.class) || paramInfo.getParameterType().hasAnnotation(StatusCode.class))
046         return new ResponseCodeArg(paramInfo);
047      return null;
048   }
049
050   /**
051    * Constructor.
052    *
053    * @param paramInfo The Java method parameter being resolved.
054    */
055   protected ResponseCodeArg(ParamInfo paramInfo) {
056      this.type = paramInfo.getParameterType().innerType();
057      Class<?> c = type instanceof Class ? (Class<?>)type : type instanceof ParameterizedType ? (Class<?>)((ParameterizedType)type).getRawType() : null;
058      if (c != Value.class || Value.getParameterType(type) != Integer.class)
059         throw new ArgException(paramInfo, "Type must be Value<Integer> on parameter annotated with @StatusCode annotation");
060   }
061
062   @SuppressWarnings({ "unchecked", "rawtypes" })
063   @Override /* RestOpArg */
064   public Object resolve(final RestOpSession opSession) throws Exception {
065      Value<Object> v = new Value();
066      v.listener(o -> opSession.getResponse().setStatus(Integer.parseInt(o.toString())));
067      return v;
068   }
069}