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