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