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 static org.apache.juneau.http.annotation.HeaderAnnotation.*; 016import java.lang.reflect.*; 017 018import org.apache.juneau.*; 019import org.apache.juneau.http.annotation.*; 020import org.apache.juneau.http.header.*; 021import org.apache.juneau.httppart.*; 022import org.apache.juneau.reflect.*; 023import org.apache.juneau.rest.*; 024import org.apache.juneau.rest.annotation.*; 025import org.apache.juneau.rest.httppart.*; 026 027/** 028 * Resolves method parameters annotated with {@link Header} of type {@link Value} representing response headers 029 * 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#setHeader(String,String) setOutput}(<jv>name</jv>,<jv>value</jv>); 037 * </p> 038 * 039 * <h5 class='section'>See Also:</h5><ul> 040 * <li class='link'><a class="doclink" href="../../../../../index.html#jrs.JavaMethodParameters">Java Method Parameters</a> 041 * </ul> 042 */ 043public class ResponseHeaderArg implements RestOpArg { 044 final ResponsePartMeta meta; 045 final String name; 046 private final Type type; 047 048 /** 049 * Static creator. 050 * 051 * @param paramInfo The Java method parameter being resolved. 052 * @param annotations The annotations to apply to any new part parsers. 053 * @return A new {@link ResponseHeaderArg}, or <jk>null</jk> if the parameter is not annotated with {@link Header}. 054 */ 055 public static ResponseHeaderArg create(ParamInfo paramInfo, AnnotationWorkList annotations) { 056 if (paramInfo.getParameterType().is(Value.class) && (paramInfo.hasAnnotation(Header.class) || paramInfo.getParameterType().hasAnnotation(Header.class))) 057 return new ResponseHeaderArg(paramInfo, annotations); 058 return null; 059 } 060 061 /** 062 * Constructor. 063 * 064 * @param pi The Java method parameter being resolved. 065 * @param annotations The annotations to apply to any new part parsers. 066 */ 067 protected ResponseHeaderArg(ParamInfo pi, AnnotationWorkList annotations) { 068 this.name = findName(pi).orElseThrow(() -> new ArgException(pi, "@Header used without name or value")); 069 this.type = pi.getParameterType().innerType(); 070 HttpPartSchema schema = HttpPartSchema.create(Header.class, pi); 071 072 Class<? extends HttpPartSerializer> ps = schema.getSerializer(); 073 this.meta = new ResponsePartMeta(HttpPartType.HEADER, schema, ps != null ? HttpPartSerializer.creator().type(ps).apply(annotations).create() : null); 074 075 Class<?> c = type instanceof Class ? (Class<?>)type : type instanceof ParameterizedType ? (Class<?>)((ParameterizedType)type).getRawType() : null; 076 if (c != Value.class) 077 throw new ArgException(pi, "Type must be Value<?> on parameter annotated with @Header annotation"); 078 } 079 080 @SuppressWarnings({ "unchecked", "rawtypes" }) 081 @Override /* RestOpArg */ 082 public Object resolve(final RestOpSession opSession) throws Exception { 083 Value<Object> v = new Value(); 084 v.listener(new ValueListener() { 085 @Override 086 public void onSet(Object o) { 087 RestRequest req = opSession.getRequest(); 088 RestResponse res = opSession.getResponse(); 089 ResponsePartMeta rpm = req.getOpContext().getResponseHeaderMeta(o); 090 if (rpm == null) 091 rpm = ResponseHeaderArg.this.meta; 092 HttpPartSerializerSession pss = rpm.getSerializer() == null ? req.getPartSerializerSession() : rpm.getSerializer().getPartSession(); 093 res.setHeader(new SerializedHeader(name, o, pss, rpm.getSchema(), false)); 094 } 095 }); 096 return v; 097 } 098}