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